Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 A Python One-Line Challenge
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wtd




PostPosted: Fri Jul 29, 2005 4:44 am   Post subject: 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.

code:
>>> 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.

code:
>>> set([1,2]) & set([2,3])
set([2])


We can "or" sets and get any elements in either set.

code:
>>> 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.

code:
>>> set([1,2]) ^ set([2,3])
set([1, 3])


Now, given a list of sets called "foo":

code:
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. Smile
Sponsor
Sponsor
Sponsor
sponsor
lyam_kaskade




PostPosted: Fri Jul 29, 2005 4:56 am   Post subject: (No subject)

ah! I did it! Very Happy

code:

>>foo = [set([1,2,3]), set([4,5,6]), set([3,7,9,1])]


code:

>>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




PostPosted: Fri Jul 29, 2005 7:42 am   Post subject: (No subject)

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




PostPosted: Fri Jul 29, 2005 1:17 pm   Post subject: (No subject)

lyam_kaskade wrote:
ah! I did it! Very Happy

code:

>>foo = [set([1,2,3]), set([4,5,6]), set([3,7,9,1])]


code:

>>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".

lyam_kaskade wrote:
What's a lambda function?


It's a small nameless function. A very simple example:

Python:
lambda a, b: a + b
rizzix




PostPosted: Fri Jul 29, 2005 1:27 pm   Post subject: (No subject)

woah, lol. what just happened.. forum was messed up for a second here..
wtd




PostPosted: Tue Aug 23, 2005 4:52 pm   Post subject: (No subject)

The answer, finally. Smile

code:
$ 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




PostPosted: Mon Sep 05, 2005 8:31 pm   Post subject: (No subject)

Python programmers, heed this summons!

I challenge you to take an input file like:

code:
hello=42
world=12
foo=bar
wooble=ninja


And generate a dictionary like:

Python:
{'hello': 42, 'world': 12, 'foo': 'bar', 'wooble': 'ninja'}


Assigned to a variable called fileInfo, in one line of code. Smile

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




PostPosted: Tue Sep 06, 2005 6:24 am   Post subject: (No subject)

Python:
fileInfo = dict([(x.split('=')[0],x.split('=')[1].strip()) for x in file('info.txt')])

?
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Sep 06, 2005 10:55 am   Post subject: (No subject)

Close.

Python:
fileInfo = dict(line.split('=', 1) for line in file('info.txt'))


Ok, so three sets of parentheses. Smile
wtd




PostPosted: Tue Sep 06, 2005 1:59 pm   Post subject: (No subject)

Now... alter the code so it properly handles the case where the key can contain more than one equals sign. Smile

A line such as:

code:
hello=world=foo=bar


Would become:

Python:
{'hello=world=foo': 'bar'}
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: