Events: http://atnd.org/events/38678 Problem: http://nabetani.sakura.ne.jp/hena/ord10pokarest/ Answer links: http://qiita.com/items/d819d1e5f2378317511e
I wrote it in Python. Confirmed to work with both 2.7.2 and 3.3.
ord10pokarest.py
#coding:utf-8
import re
def isroyal( hand ) :
ranks=sorted( [ r[0] for r in hand ] )
return ranks==[1,10,11,12,13]
def isstraight( hand ) :
ranks=sorted( [ r[0] for r in hand ] )
return (
ranks==list( range( ranks[0], ranks[-1]+1) ) or
ranks==[1]+list( range( ranks[1], 14) ) )
def isflash( hand ) :
return len(set( [ r[1] for r in hand ] ))==1
def score( hand ):
return (( 1 if isroyal( hand) else 0 ) +
( 2 if isstraight( hand) else 0 ) +
( 4 if isflash( hand) else 0 ) )
def rank( c ):
if re.match( "\d", c ):
return int(c)
else:
return { "J":11, "Q":12, "K":13, "A":1 }[c]
def solve( src ):
hand = [ ( rank(c[0]), c[1] ) for c in re.findall( "([^shdc]+)([shdc])", src ) ]
return (
{ 0:False, 7:"RF", 6:"SF", 4:"FL", 2:"ST", 3:"ST" }[
score(hand)
] or { 0:False, 6:"4SF", 4:"4F", 2:"4S"}[
max( [ score(h) for h in (
[ hand[0:i]+hand[i+1:5] for i in range(0,5) ]
) ] )
] or "-" )
def test( samples ) :
for line in samples.splitlines():
a=re.split( "\s+", line ) # num, input, expected
if len(a) != 3:
continue
actual = solve( a[1] )
ok=actual==a[2]
print( [ "ok" if ok else "***NG***", a, actual ] )
test( """
0 Qs9s3dJd10h 4S
1 KdAdJd10dQd RF
52 10dKdQdAdJd RF""" )
Most of the test data is omitted.
The ruby version (http://qiita.com/items/c6ebf2c1a9c750568e97) was ported as it is.
When porting from ruby, it is very fresh to have to write return.
I wanted to use the else of the for loop, but I didn't have a chance to use it. Sorry.
I'm not used to python at all, so I usually don't write this! I would be grateful if you could give me information like> python people
Recommended Posts