Événement: http://atnd.org/events/38678 Problème: http://nabetani.sakura.ne.jp/hena/ord10pokarest/ Liens de réponse: http://qiita.com/items/d819d1e5f2378317511e
Je l'ai écrit en Python. Confirmé pour fonctionner avec les versions 2.7.2 et 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"""    )
La plupart des données de test sont omises.
La version ruby (http://qiita.com/items/c6ebf2c1a9c750568e97) a été portée telle quelle.
Lors du portage depuis ruby, il est très frais d'avoir à écrire return.
Je voulais utiliser else for loop, mais je n'ai pas eu la chance de l'utiliser. Pardon.
Je n'ai pas du tout l'habitude de python, donc je n'écris généralement pas ça! Je vous serais reconnaissant si vous pouviez me donner des informations comme> python people
Recommended Posts