Created
February 24, 2015 17:14
-
-
Save aminamid/bee0e8371e6d6e8ad0dd to your computer and use it in GitHub Desktop.
第一回 オフラインリアルタイムどう書くの参考問題の解答をPythonで書いてみた ref: http://qiita.com/aminamid/items/bfaf2ae1cdbeb48fa2ef
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import itertools | |
SPEC = { | |
"suit": ["S","H","D","C"], | |
"rank": ["2","3","4","5","6","7","8","9","10","J","Q","K","A"], | |
"hand": ( | |
("4K", lambda x: max(snds(uniq_cnt(snds(x)))) == 4), | |
("FH", lambda x: sorted(snds(uniq_cnt(snds(x)))) == sorted((2,3))), | |
("3K", lambda x: max(snds(uniq_cnt(snds(x)))) == 3), | |
("2P", lambda x: sorted(snds(uniq_cnt(snds(x)))) == sorted((2,2,1))), | |
("1P", lambda x: sorted(snds(uniq_cnt(snds(x)))) == sorted((2,1,1,1))), | |
("--", lambda x: True), | |
) | |
} | |
def snds(l): | |
return [v for (k,v) in l] | |
def uniq_cnt(l): | |
return [(x[0],len(list(x[1]))) for x in itertools.groupby(sorted(l))] | |
def parser(suit,rank,**kwargs): | |
return re.compile("([{suits}]+)([{ranks}]+)".format(suits=''.join(suit),ranks=''.join(rank))) | |
def main(arg): | |
ls = parser(**arg['spec']).findall(arg['input']) | |
print list( (k for (k,f) in arg['spec']['hand'] if f(ls)) )[0] | |
if __name__ == "__main__": | |
import sys | |
arg={ | |
"spec": SPEC, | |
"input": sys.argv[1], | |
} | |
main(arg) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment