8
1
Note: I have accepted an answer, but I'm willing to accept a better answer if it comes.
Write a program that computes operations on sets. You may take input either from standard input or from command line arguments. Your program must be able to compute unions, intersections, relative complements, and cartesian products. Output should be the result of these operations.
You may not use built-in functions to do the work for you.
Input Format
<A>
and <B>
represent sets. The format for sets can be anything that is convenient, as long as it actually represents a set (no taking advantage of this "looseness"). The elements of a set don't always have to be in the same order because sets aren't ordered. You define what kind of object these elements can be (integers, letters, ...).
- Union:
union <A> <B>
- Intersection:
intersect <A> <B>
- Relative complement (difference of sets):
complement <A> <B>
- Cartesian product:
product <A> <B>
(format for ordered pairs can be anything convenient -- don't take advantage of this "looseness" either)
For info on what these operations even are, see the Wikipedia page on it.
I'm actually not clear on "actually represents a set". How can one determine whether a proposed representation represents the given set? Does it have to be that a representation of a set is always character for character the same? Python fails the previous point because it lists elements in an arbitrary order. What can the elements of these sets be? (Sorry, I should have asked these all while the question was sandboxed). – xnor – 2014-07-11T02:50:34.650
Could you explain what some of these terms for those that are not so familiar with them? – Kyle Kanos – 2014-07-11T03:01:18.827
Can you clarify "You may not use built-in functions to do the work for you"? Does this mean no built-in functions whatsoever or no built-in functions specifically designed for handling sets? – Digital Trauma – 2014-07-11T15:44:39.127
1@DigitalTrauma I mean no built-ins specifically for computing unions, intersections, etc. You may, for example, use
eval()
to convert a string to your language's representation of a set. – golfer9338 – 2014-07-11T17:52:43.967So, I'm guessing T-SQL is out of the running here? – Michael B – 2014-07-11T21:02:25.730
@MichaelB I don't know that language, so ... I don't know. – golfer9338 – 2014-07-12T18:09:33.503
@golfer9338: SQL in general handles sets of records, which means pretty much everything in the language is geared towards set operations; you'd have a hard time avoiding them. – Joey – 2014-07-12T22:22:20.430
What do you mean by "any kind of object"? How am I supposed to parse STDIN in a way that can handle any kind of object? – Ypnypn – 2014-07-13T18:42:41.417
@Ypnypn I mean you define what kind of object your program can handle. Edited question to reflect this. – golfer9338 – 2014-07-13T20:48:26.950