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

Click here for details of the problem. http://qiita.com/Nabetani/items/cbc3af152ee3f50a822f

I wrote it in python with reference to the implementation of ruby. Since the character string representing the rank is one character other than 10, first replace 10 with one character'T', and take out only the rank characters arranged every other character to make rank. Next, use the set function to extract the types of rank characters, and create a pair that checks how many ranks there are by count. Finally, sort the pairs, quantify them, and look up the dictionary you created in advance to find the answer.

def solve(text):
    rank = text.replace('10','T')[1::2]
    pair = [rank.count(x) for x in set(rank)]
    key = reduce(lambda a,b: a*10+b, sorted(pair))
    return {14:'4K', 23:'FH', 113:'3K', 122:'2P', 1112:'1P', 11111:'--'}[key]

def test(text, answer):
    result = solve(text)
    print 'OK' if result == answer else 'NG', result, answer, text

if __name__ == '__main__':
    test('D3C3C10D10S3', 'FH')
    test('S8D10HJS10CJ', '2P')
    test('DASAD10CAHA', '4K')
    test('S10HJDJCJSJ', '4K')
    test('S10HAD10DAC10', 'FH')
    test('HJDJC3SJS3', 'FH')
    test('S3S4H3D3DA', '3K')
    test('S2HADKCKSK', '3K')
    test('SASJDACJS10', '2P')
    test('S2S10H10HKD2', '2P')
    test('CKH10D10H3HJ', '1P')
    test('C3D3S10SKS2', '1P')
    test('S3SJDAC10SQ', '--')
    test('C3C9SAS10D2', '--')

Recommended Posts

Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
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
An example of the answer to the reference question of the study session. In python.
The 15th offline real-time I tried to solve the problem of how to write with python
Offline real-time how to write Python implementation example of E15 problem
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 E14
How to know the internal structure of an object in Python
How to write offline real time Solve F01 problems with Python
I want to color a part of an Excel string in Python
The 15th offline real-time how to write problem was solved with python
Answer to "Offline real-time how to write F02 problem"
Answer to "Offline Real-time How to Write F01 Problem"
Answer to "Offline Real-time How to Write E13 Problem"
I want to store the result of% time, %% time, etc. in an object (variable)
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
[Python] How to write an if statement in one sentence.
[Question] How to get data of textarea data in real time using Python web framework bottle
How to calculate the sum or average of time series csv data in an instant
I wrote a doctest in "I tried to simulate the probability of a bingo game with Python"
I made a program in Python that changes the 1-minute data of FX to an arbitrary time frame (1 hour frame, etc.)
I wrote the queue in Python
I wrote the stack in Python
I tried to describe the traffic in real time with WebSocket
I compared the calculation time of the moving average written in Python
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
The 17th Offline Real-time How to Solve Writing Problems in Python
How to check the memory size of a variable in Python
The 10th offline real-time writing reference problem. Implementation example by Python.
How to check the memory size of a dictionary in Python
[Python] Explains how to use the format function with an example
The 11th offline real-time writing reference problem. Implementation example by python.
"Book to train programming skills to fight in the world" Python code answer example --1.4 Permutation of sentences
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.
[Fundamental Information Technology Engineer Examination] I wrote an algorithm for the maximum value of an array in Python.
"A book to train programming skills to fight in the world" Python code answer example --1.9 Rotation of strings
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 leave an arbitrary command in the command history of Shell
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 write Ruby to_s in Python
(Diary 1) How to create, reference, and register data in the SQL database of Microsoft Azure service with python
I tried to explain how to get the article content with MediaWiki API in an easy-to-understand manner with examples (Python 3)
How to pass the execution result of a shell command in a list in Python
[Python] I want to know the variables in the function when an error occurs!
[Fundamental Information Technology Engineer Examination] I wrote the algorithm of Euclidean algorithm in Python.