index | submit | rank | book

set-ui – Set union and intersection

Write a program that is able to compute the union and intersection of sets.

Set union and intersection

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.

Input and output

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.

Example input

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

Example output

{1,2,3,4}
{2,3}
{2,3,5,7,8,9,10}
{}

Scoring

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.

Hints

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

try next: set-calc set-size

index | submit | rank | book

Copyright © 2020-2021 Rudy Matela
All rights reserved