
-----------------------------------
wtd
Fri Jul 29, 2005 4:44 am

A Python One-Line Challenge
-----------------------------------
Sets are an unordered collection of data.  They have the useful property that they only contain any given value once.

>>> set([1,2,3,2,1,4])
set([1, 2, 3, 4])

We can do fancy manipulations with sets.  We can "and" two sets, and get back only the elements contained in both sets.

>>> set([1,2]) & set([2,3])
set([2])

We can "or" sets and get any elements in either set.

>>> set([1,2]) | set([2,3])
set([1, 2, 3])

And, we can "xor" (exclusive or) two sets.  This gives us only elements which are not shared by the two sets.

>>> set([1,2]) ^ set([2,3])
set([1, 3])

Now, given a list of sets called "foo":

foo = [set([1,2,3]), set([4,5,6]), set([3,7,9,1])]

Write a single line of code which generates a new set containing all of the elements in all of the sets in "foo".  

No semi-colons are allowed, and the code should be readable.  

Lambda functions are allowed.

Questions on how sets work are quite welcome.  :)

-----------------------------------
lyam_kaskade
Fri Jul 29, 2005 4:56 am


-----------------------------------
ah! I did it!  :D 


>>foo = [set([1,2,3]), set([4,5,6]), set([3,7,9,1])]



>>foo [0] | foo[1] | foo[3]
set ([1, 2, 3, 4, 5, 6, 7, 9])


Unless I misunderstood the question :eh: 
What's a lambda function?

-----------------------------------
MysticVegeta
Fri Jul 29, 2005 7:42 am


-----------------------------------
Isnt "lambda" a greek alphabet, i think it comes after "kappa". lol i was bored so i memorized all of them last year in grade 8

-----------------------------------
wtd
Fri Jul 29, 2005 1:17 pm


-----------------------------------
ah! I did it!  :D 


>>foo = [set([1,2,3]), set([4,5,6]), set([3,7,9,1])]



>>foo [0] | foo[1] | foo[3]
set ([1, 2, 3, 4, 5, 6, 7, 9])


Unless I misunderstood the question :eh: 

A little bit.  I may not have been clear enough, though.

The one line of code has to work for any number of sets in "foo".

What's a lambda function?

It's a small nameless function.  A very simple example:

lambda a, b: a + b

-----------------------------------
rizzix
Fri Jul 29, 2005 1:27 pm


-----------------------------------
woah, lol. what just happened.. forum was messed up for a second here..

-----------------------------------
wtd
Tue Aug 23, 2005 4:52 pm


-----------------------------------
The answer, finally.  :)

$ python
Python 2.4.1 (#2, Mar 30 2005, 21:51:10)
[GCC 3.3.5 (Debian 1:3.3.5-8ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> foo = [set([1,2,3]), set([4,5,6]), set([3,7,9,1])]
>>> reduce(lambda a, b: a | b, foo, set())
set([1, 2, 3, 4, 5, 6, 7, 9])
>>> reduce(set.__or__, foo, set())
set([1, 2, 3, 4, 5, 6, 7, 9])
>>>

-----------------------------------
wtd
Mon Sep 05, 2005 8:31 pm


-----------------------------------
Python programmers, heed this summons!

I challenge you to take an input file like:

hello=42
world=12
foo=bar
wooble=ninja

And generate a dictionary like:

{'hello': 42, 'world': 12, 'foo': 'bar', 'wooble': 'ninja'}

Assigned to a variable called fileInfo, in one line of code.  :)

Oh, and no semi-colons.  No square brackets either.  In an ideal solution you should use only two sets of parentheses, and no curly braces.

Be wary.  The keys in a line of the file may contain more than one equals sign.

-----------------------------------
Hikaru79
Tue Sep 06, 2005 6:24 am


-----------------------------------
fileInfo = dict(
?

-----------------------------------
wtd
Tue Sep 06, 2005 10:55 am


-----------------------------------
Close.

fileInfo = dict(line.split('=', 1) for line in file('info.txt'))

Ok, so three sets of parentheses.  :)

-----------------------------------
wtd
Tue Sep 06, 2005 1:59 pm


-----------------------------------
Now... alter the code so it properly handles the case where the key can contain more than one equals sign.  :)

A line such as:

hello=world=foo=bar

Would become:

{'hello=world=foo': 'bar'}
