Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python

http://qiita.com/Nabetani/items/cbc3af152ee3f50a822f

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)

Recommended Posts

Part 1 I wrote the answer to the reference problem of how to write offline in real time 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 15th offline real-time I tried to solve the problem of how to write with python
I wrote the code to write the code of Brainf * ck in python
How to write offline real time Solve E04 problems in Python
How to write offline real time I tried to solve E11 with python
The 16th offline real-time how to write reference problem to solve with Python
The 19th offline real-time how to write reference problem to solve with Python
How to write offline real time I tried to solve E12 with python
Offline real-time how to write Python implementation example of E15 problem
How to write offline real time Solve F01 problems with Python
Answer to "Offline real-time how to write F02 problem"
Answer to "Offline Real-time How to Write F01 Problem"
The 15th offline real-time how to write problem was solved with python
Answer to "Offline Real-time How to Write E13 Problem"
An example of the answer to the reference question of the study session. In python.
20th Offline Real-time How to Write Problems in Python
How to get the number of digits in Python
I want to write in Python! (3) Utilize the mock
Offline real-time how to write Python implementation example of E14
I wrote the queue in Python
I wrote the stack in Python
[Question] How to get data of textarea data in real time using Python web framework bottle
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I tried to describe the traffic in real time with WebSocket
I compared the calculation time of the moving average written in Python
How to know the internal structure of an object in Python
The 17th Offline Real-time How to Solve Writing Problems in Python
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to write Ruby to_s in Python
How to crop the lower right part of the image with Python OpenCV
How to write the correct shebang in Perl, Python and Ruby scripts
How to get the date and time difference in seconds with python
I want to batch convert the result of "string" .split () in Python
I want to explain the abstract class (ABCmeta) of Python in detail.
I want to color a part of an Excel string in Python
I made a program to check the size of a file in Python
Python: I want to measure the processing time of a function neatly
How to use the C library in Python
Summary of how to import files in Python 3
The 18th offline real-time writing problem in Python
How to measure execution time with Python Part 1
How to get the files in the [Python] folder
The 19th offline real-time writing problem in Python
I want to display the progress in Python!
How to measure execution time with Python Part 2
How to pass the execution result of a shell command in a list in Python
[Fundamental Information Technology Engineer Examination] I wrote the algorithm of Euclidean algorithm in Python.
I want to use Python in the environment of pyenv + pipenv on Windows 10
How to find the coefficient of the trendline that passes through the vertices in Python
I want to store the result of% time, %% time, etc. in an object (variable)
How to get a list of files in the same directory with python
(Diary 1) How to create, reference, and register data in the SQL database of Microsoft Azure service with python
I want to write in Python! (1) Code format check
How to retrieve the nth largest value in Python
I tried to summarize how to use matplotlib of python
How to write string concatenation in multiple lines in Python