3 ms·
This is the most obscure piece of Python code I could write to do the same thing : def birthday(people=23): import random days = 365 sample_size
by 3KWA 19y ago
This is the most obscure piece of Python code I could write to do the same thing :
def birthday(people=23):
import random
days = 365
sample_size = 1000
return reduce(lambda x, y : x + y, (len(s) != len(set(s)) for s in (map(lambda x: random.choice(range(x)), (days, )*people) for i in range(sample_size))))
I am sure someone can beat it ;) let me know http://www.3kwa.com/Logbook/20070708 http://www.3kwa.com/Logbook/20070708
How do I get the code to indent properly?
- Jd 19y agodef birthday(people=23): import random days = 365 sample_size = 1000 return reduce(lambda x, y : x + y, (len(s) != len(set(s)) for s in (map(lambda x: random.choice(range(x)), (days, )*people) for i in range(sample_size)))) (two empty lines, then one space in front of every code line)
- dhouston 19y agodef birthday(people=23): from random import randint as rand; return sum([len(set([rand(1,365) for x in range(people)])) != people for i in range(1000)]) (constructs fewer inner lists, otherwise changes are completely cosmetic)
- 3KWA 19y agoah yes indeed much better! I tried to follow the K structure too much, but your code is too readable :D