Comment detail

ポーカーの役判定 (Nested Flatten)

	
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import sys

def f(c):
  d = {'A': '1', 'T': 'a', 'J': 'b', 'Q': 'c', 'K': 'd'}
  s = len(set([c[i] for i in range(0,10,2)])) == 1
  r = ''.join(sorted([d[c[i]] if c[i] in d else c[i] for i in range(1,11,2)]))
  l = len(set(r))
  if l == 2:
    print 'Four of a kind' if r.count(r[0]) in [1,4] else 'Full house'
  elif l == 5:
    if '123456789abcd 1abcd'.find(r) >= 0:
      print ('Royal flush 'if r == '1abcd' else 'Straight flush') if s else 'Straight'
    elif s:
      print 'Flush'
    else:
      print 'No pair'
  elif s:
    print 'Flush'
  elif l == 4:
    print 'One pair'
  elif l == 3:
    print 'Three of a kind' if [i for i in set(r) if r.count(i) >= 3] else 'Two pair'

f(sys.argv[1])
こういうときに便利なスライスの記法を思い出したのと
文字の置換も必要なかったので、ちょっと書き直しました。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import sys

def f(c):
  s = len(set(c[0::2])) == 1
  r = ''.join(sorted(c[1::2]))
  l = len(set(r))
  if l == 2:
    print 'Four of a kind' if r.count(r[0]) in [1,4] else 'Full house'
  elif l == 5:
    if '2345A23456789T789JT89JQT9JKQTAJKQT'.find(r) >= 0:
      print ('Royal flush' if r == 'AJKQT' else 'Straight flush') if s else 'Straight'
    elif s:
      print 'Flush'
    else:
      print 'No pair'
  elif s:
    print 'Flush'
  elif l == 3:
    print 'Three of a kind' if [i for i in set(r) if r.count(i) >= 3] else 'Two pair'
  else:
    print 'One pair'

f(sys.argv[1])

Index

Feed

Other

Link

Pathtraq

loading...