avatar

One-line Loto Simulation in Python


19-05-2015 09:45 by depado

So I had this little challenge. Write a one-line loto simulation. Actually it looked pretty interesting at first and them someone pointed me out that there is a random.sample function. Anyway, here are some pretty solutions that have been sent by some members of the #python chan on freenode. First solution I worked on using lambdas :

import random
lst = list(range(0, 100))
[print(y) for x, y in [(lambda x: [lst.remove(x), x])(random.choice(lst)) for x in range(0,10)]]

Second solution when using random.sample as suggested by TFKyle

import random
[print(x) for x in random.sample(range(100), 10)]

|Zz|'s first solution

import random
lst = [(lambda x,y: print(y) or x)(lst,y) for x, y, lst in [(lambda x: (lst.remove(x), x, lst))(random.choice(lst)) for x in range(0,10) for lst in [list(range(100))]]][-1]

|Zz|'s second solution

import random
lst = [(lambda lst,x: print(x) or lst)(lst,x) for _, x, lst in [(lambda x: (lst.remove(x), x, lst))(random.choice(lst)) for i in range(0,10) for lst in [list(range(100))]]][-1]

(That guy is crazy o.o)

tech2's solution

import random
print('\n'.join(map(str, random.sample(range(100), 10))))

I mainly worked on that to learn more about the uses of lambda because it has always been a bit obscure to me. My primary goal (what |Zz| fixed) was that I didn't know how to define the list (range(100)) inside the lambda.