The 10th offline real-time writing reference problem. Implementation example by Python.

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

The 10th offline real-time writing reference problem. Implementation example by Python.
The 11th offline real-time writing reference problem. Implementation example by python.
The twelfth offline real-time writing reference problem. Implementation by python
The 14th offline real-time writing reference problem with Python
The 18th offline real-time writing problem in Python
The 19th offline real-time writing problem in Python
The 15th offline real-time how to write reference problem in Python
The 14th offline real-time how to write reference problem in python
The 18th offline real-time how to write reference problem in Python
The 17th offline real-time how to write reference problem implemented in Python
The 19th offline real-time how to write reference problem to solve with Python
The 17th Offline Real-time How to Solve Writing Problems in Python
Offline real-time how to write Python implementation example of E15 problem
The 16th offline real-time how to write problem was solved with Python
The 15th offline real-time how to write problem was solved with python
The 15th offline real-time I tried to solve the problem of how to write with python
13th Offline Real-time How to Solve Writing Problems in Python
Offline real-time how to write E11 ruby and python implementation example
Answer to "Offline real-time writing F04 problem"
Answer to "Offline real-time writing F05 problem"
Answer to "Offline Real-Time Writing E12 Problem"
[Implementation example] Read the file line by line with Cython (Python) from the last line
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
20th Offline Real-time How to Write Problems in Python
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
An example of the answer to the reference question of the study session. In python.