12
1
Given multiple sets, e.g. s1={2,3,7}
, s2={1,2,4,7,8}
and s3={4,7}
, a Venn diagram visualizes each set by a closed curve and set elements which are either inside or outside the curve's perimeter, depending on whether they are element of the set or not. Because all set elements appear only once in the Venn digram, the curves representing each set need to overlap if an element is present in more than one set. We call each such overlapping a cell of the Venn diagram.
This explanation might be a bit confusing, so let's have a look at an example.
Example
A Venn diagram for sets s1
, s2
and s3
could look like this:
The cells of this Venn diagram are (read from top to bottom, left to right) {1,8}
, {2}
, {7}
, {4}
, {3}
, {}
and {}
.
In practice, one commonly encounters only Venn diagrams of two or three sets, because the representation of Venn diagrams of four or more sets is not very clear. However they do exist, e.g. for six sets:
CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=1472309
The Task
Given a non-empty set of sets of positive integers in any reasonable representation, return the set of cells of the input sets' Venn diagram. Specifically, no graphical representation is needed.
- You may write a full program or a function.
- You may return as many empty sets as there are empty cells (i.e. a list of all cells) instead of just one empty set (i.e. the set of cells).
- Some reasonable ways of input for the above example include but are not limited to
{{2,3,7},{1,2,4,7,8},{4,7}}
,[[2,3,7],[1,2,4,7,8],[4,7]]
,"2,3,7;1,2,4,7,8;4,7"
or"2 3 7\n1 2 4 7 8\n4 7"
. If in doubt whether your chosen input format is acceptable, feel free to ask in a comment. - Your output format should match your input format, if possible. Note that this rule requires your format to be able to unambiguously display empty sets.
- This is code-golf, so try to use as few bytes as possible in the language of your choice. In order to encourage competition per language instead of between languages, I won't accept an answer.
Test Cases
Here are some inputs along with possible outputs:
input -> output
{{2,3,7},{1,2,4,7,8},{4,7}} -> {{1,8},{2},{7},{4},{3},{}} (or {{1,8},{2},{7},{4},{3},{},{}})
{{1,2,3},{4,5,6},{7,8,9}} -> {{1,2,3},{4,5,6},{7,8,9},{}}
{{}} -> {{}}
{{1,2,3},{1,2}} -> {{1,2},{3},{}}
{{4,3,8},{1,2,9,3},{14,7,8,5},{6,11,3,8},{10},{9,4,3,7,10}} -> {{6,11},{10},{4},{3},{8},{5,14},{1,2},{9},{7},{}}
{{2,3,4,7},{},{1,3,7,5,6},{2,3,7,5},{7,2,4,3,6},{1,4,5}} -> {{},{4},{2},{7,3},{1},{6},{5}}
{{1,2,3,4},{1,2,5,6},{1,3,5,7}} -> {{4},{3},{2},{1},{6},{5},{7}}
I'm assuming this is true because of the definition of a set, but may we assume that there will be no duplicates within one of the subsets? – HyperNeutrino – 2017-05-07T21:08:21.050
@Hyper Neutrino Yes, you can assume all sets are duplicate free. – Laikoni – 2017-05-07T21:49:29.187
Perhaps you could add a test case where none of the cells are empty. E.g. {{1,2,3,4},{1,2,5,6},{1,3,5,7}}. – Ørjan Johansen – 2017-05-08T03:17:04.093
How does the second one not give
{{1,2,3},{4,5,6},{7,8,9},{},{},{},{}}
? – Leaky Nun – 2017-05-08T04:36:05.493@Leaky Nun You are allowed to return either the set of cells or the list of cells. Appart from the first example, the test cases give only the set version, so all empty sets collapse into a single empty set. This means that your output is acceptable, too. – Laikoni – 2017-05-08T05:27:58.653
Do I understand right that "you may return as many empty sets as there are empty cells" will pad the output to
2^n-1
entries, wheren
is the numbers of sets in the input? An the Venn diagram exterior is not a cell? – xnor – 2017-05-08T05:45:01.773@ØrjanJohansen Thanks, added the tescase. – Laikoni – 2017-05-08T07:21:52.003
@xnor Yes, exactly. – Laikoni – 2017-05-08T09:07:35.807
I always saw a 6-set Venn Diagram like this: http://4.bp.blogspot.com/-VEml0lMUxcM/UvwDoZ1bAZI/AAAAAAAAHPQ/ozJI18BlkLI/s1600/Large_venn.jpg
– Magic Octopus Urn – 2017-05-08T16:35:50.9301@carusocomputing Upon closer inspection you will find that this is not a true Venn diagramm because some possible overlappings are missing. – Laikoni – 2017-05-08T21:46:51.573