stupid python tricks

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

4 earlier thoughts


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

import random;print(min(
  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)],
              for j in [random.randint(0,k)]])]
  for deal in [lambda n,xs:resv(xs[:n],xs[n:],n)]

1 later thought