The 15th offline real-time how to write problem was solved with python

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

Thanks to having tried some past problems in advance, I immediately thought that it would be good to make a dictionary, and set the black (lit) of the dot pattern of lightning letters to 1 and the white (off) to 0, and set the upper and lower rows I made a dictionary with the characters as the values and the keys as the keys. Converts the input hexadecimal character string to a 32-character binary character string, extracts several characters from the beginning of the upper and lower rows, and looks up the dictionary. However, I had to pay attention to the number of characters to be extracted. When there is a'U'dot pattern with 3 dots above and below, if you take out 2 dots above and below and look up the dictionary, it will match the dot pattern of'L'. So, first check if it is a 3-dot pattern, then check if it is a 2-dot pattern, and finally check if it is a delimiter 1-dot pattern (00) if none of them hit.

parts = {'1011':'L', '1110':'R', '0111':'J', '111010':'T', '101111':'U',
         '111101':'N', '011110':'S', '110011':'Z', '00':'' }

def solve(text):
    upper, lower = (format(int(t,16),'032b') for t in text.split('/'))
    answer = ''
    while len(upper) > 0:
        for n in xrange(3, 0, -1):
            part = upper[:n] + lower[:n]
            if part in parts:
                answer += parts[part]
                upper, lower = upper[n:], lower[n:]
                break
    return answer

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

if __name__ == '__main__':
    0, test( "2ed8aeed/34b0ea5b", "LTRSUNTSJ" );
    1, test( "00000200/00000300", "L" );
    2, test( "00018000/00010000", "R" );
    3, test( "00002000/00006000", "J" );
    4, test( "00000700/00000200", "T" );
    5, test( "01400000/01c00000", "U" );
    6, test( "00003800/00002800", "N" );
    7, test( "000c0000/00180000", "S" );
    8, test( "00003000/00001800", "Z" );
    9, test( "132eae6c/1a64eac6", "LRJTUNSZ" );
    10, test( "637572d0/36572698", "ZSNUTJRL" );
    11, test( "baddb607/d66b6c05", "LTJZTSSSN" );
    12, test( "db74cd75/6dac6b57", "ZZZTJZRJNU" );
    13, test( "3606c2e8/1b0d8358", "ZZSSLTJ" );
    14, test( "ad98c306/e6cc6183", "UZZZZZZ" );
    15, test( "4a4aaee3/db6eeaa6", "JJLLUUNNS" );
    16, test( "ecd9bbb6/598cd124", "TSSZZTTRR" );
    17, test( "e0000002/40000003", "TL" );
    18, test( "a0000007/e0000005", "UN" );
    19, test( "c0000003/80000006", "RS" );
    20, test( "40000006/c0000003", "JZ" );
    21, test( "01da94db/00b3b6b2", "TSUJLRSR" );
    22, test( "76eeaaea/24aaeeae", "TRNNUUNU" );
    23, test( "1dacaeee/1566e444", "NRJZUTTT" );
    24, test( "26c9ac60/6c6d66c0", "JSZLRJZS" );
    25, test( "6c977620/36da5360", "ZZLLTNZJ" );
    26, test( "069aeae6/0db34eac", "SJSLTUNS" );
    27, test( "06d53724/049da56c", "RRULRNJJ" );
    28, test( "069b58b0/04d66da0", "RLRSLZJR" );
    29, test( "1b6eced4/11b46a9c", "RZZTZNRU" );
    30, test( "522e8b80/db6ad900", "JLLJNLJT" );
    31, test( "6546cdd0/376c6898", "ZULSZRTL" );
    32, test( "4e6d5b70/6ad9d620", "LNSSURST" );
    33, test( "37367772/65635256", "SNSZNTNJ" );
    34, test( "25535d58/377669cc", "LUUSLTUZ" );
    35, test( "0ae6a55d/0eacedcb", "UNSUJUTJ" );
    36, test( "76762edc/23536a88", "TZNZJNRT" );

Recommended Posts

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 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
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 15th offline real-time I tried to solve the problem of how to write with python
The 17th offline real-time how to write reference problem implemented in Python
20th Offline Real-time How to Write Problems in Python
The 14th offline real-time writing reference problem with Python
How to write offline real-time Solving E05 problems 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
Answer to "Offline real-time how to write F02 problem"
The 18th offline real-time writing problem in Python
Answer to "Offline Real-time How to Write F01 Problem"
Answer to "Offline Real-time How to Write E13 Problem"
The 19th offline real-time writing problem in Python
How to write offline real time I tried to solve the problem of F02 with Python
Offline real-time how to write Python implementation example of E14
13th Offline Real-time How to Solve Writing Problems in Python
Offline real-time how to write E11 ruby and python implementation example
The 10th offline real-time writing reference problem. Implementation example by Python.
The 11th offline real-time writing reference problem. Implementation example by python.
How to write offline real time Solve F01 problems with Python
How to write offline real time I tried to solve E11 with python
[Introduction to Python] How to write a character string with the format function
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
How to write offline real time I tried to solve E12 with python
[Introduction to Python] How to iterate with the range function?
Try to solve the internship assignment problem with Python
The first algorithm to learn with Python: FizzBuzz problem
[Python] How to specify the download location with youtube-dl
I tried to solve the problem with Python Vol.1
[Python] How to rewrite the table style with python-pptx [python-pptx]
Write to csv with Python
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
[Note] How to write QR code and description in the same image with python
A story about how to deal with the CORS problem
How to get into the python development environment with Vagrant
[Introduction to Python] How to get data with the listdir function
The twelfth offline real-time writing reference problem. Implementation by python
How to write a Python class
Python: How to use async with
[Python] Write to csv file with Python
How to get the Python version
How to get started with Python
How to use FTP with Python
How to calculate date with python
[Introduction to Python] How to split a character string with the split function
How to write offline real time Solve E04 problems in Python
[Python] Explains how to use the format function with an example
I wanted to solve the ABC164 A ~ D problem with Python
How to send a request to the DMM (FANZA) API with python
How to deal with the problem that the current directory moves when Python is executed from Atom
Answer to "Offline real-time writing F04 problem"
How to solve the bin packing problem
How to write Python document comments (Docstrings)
How to work with BigQuery in Python
How to do portmanteau test with python