Write a program that is able to compute the union and intersection of sets.
The union of two sets includes all elements that are in either of the two sets. The intersection of two sets includes all elements that are in both of the two sets.
Each line of input starts with either the word union
or intersection
;
followed by the word set
; followed by the elements of the first set;
followed by the word set
; followed by the elements of the second set.
For each line of input you should print the resulting set: the union or intersection of the two given sets. In the output, elements should be separated by commas and surrounded by curly brackets. Either give the elements in the same order given on the input or in ascending numeric order.
All input numbers will be in the range from 0 to 99 inclusive, i.e.: 0 ≤ x < 100. Input sets will not have repeated elements and may be empty. Input always begins with a set definition. Any leading zeroes on input numbers should be ignored.
union set 1 2 3 set 2 3 4
intersection set 1 2 3 set 2 3 4
union set 2 3 5 7 set 8 9 10
intersection set 2 3 5 7 set 8 9 10
{1,2,3,4}
{2,3}
{2,3,5,7,8,9,10}
{}
Performance is not in the scope of this exercise. Your program does not need to have optimal performance to get a full score. You can implement your solution in the simplest way possible.
Since performance is not in the scope of this exercise feel free to use any simple data structure to represent sets. An array of booleans or a list of members should be enough to get a full score.
try first: set-member
Copyright © 2020-2021 Rudy Matela
This text is available under the CC BY-SA 4.0 license.
Originally available on cscx.org/set-ui