stupid python tricks

6 thoughts
last posted April 25, 2016, 8:24 a.m.

4 earlier thoughts

0

Reservoir sampling, AKA deal. (in APL, at 110 baud, that main expression would have been something likeHAND 5?52)

import random;print(min(
  hand(deal(5,range(52)))
  for vals,suits in [("A23456789TJQK", "CDHS")]
  for hand in [lambda js: " ".join([
                vals[v]+suits[s] for j in js
                for s,v in [divmod(j,13)]])]
  for resv in [lambda rs,xs,k: rs if not xs else
      min([resv([r if i!=j else xs[0]
                  for i,r in enumerate(rs)],
                xs[1:],k+1)
              for j in [random.randint(0,k)]])]
  for deal in [lambda n,xs:resv(xs[:n],xs[n:],n)]
 ))

1 later thought