The 18th offline real-time how to write reference problem in Python

Click here for the problem http://nabetani.sakura.ne.jp/hena/ord18mafovafo/ Click here for execution results http://ideone.com/ZSJEH2 Click here for examples of answers from other people http://qiita.com/Nabetani/items/373105e7fafd12f5e9fd Click here for the event announcement of the "18th Offline Real-Time Writing" Yokohama Henachoko Programming Study Group http://atnd.org/events/47025 (held on Saturday, February 01, 2014)

#!/usr/bin/env python

# FOLD = { operation:(front side fold, back side fold), ... }
# '0':front side paper, '1':back side paper, 'm':mountain fold, 'V':valley fold
FOLD = {'L':('1V0','1m0'), 'J':('0V1','0m1'), 'Z':('0m1V0','1m0V1'), 'U':('1V0V1','0m1m0'), 'S':('0V1m0','1V0m1'), '':('','')}

solve = lambda operation, side=0: ''.join(solve(operation[1:], int(f)) if f in '01' else f for f in FOLD[operation[:1]][side])

def test(data, correct):
    answer = solve(data)
    print 'xo'[answer==correct], data, correct, answer

0, test( "JZ", "mVVmV" );
1, test( "J", "V" );
2, test( "L", "V" );
3, test( "Z", "mV" );
4, test( "U", "VV" );
5, test( "S", "Vm" );
6, test( "JL", "VVm" );
7, test( "JS", "VmVVm" );
8, test( "JU", "VVVmm" );
9, test( "LU", "mmVVV" );
10, test( "SL", "VVmmV" );
11, test( "SS", "VmVVmmVm" );
12, test( "SU", "VVVmmmVV" );
13, test( "SZ", "mVVmVmmV" );
14, test( "UL", "mVVVm" );
15, test( "UU", "mmVVVVmm" );
16, test( "UZ", "mVVmVVmV" );
17, test( "ZJ", "VmmVV" );
18, test( "ZS", "VmmVmVVm" );
19, test( "ZZ", "mVmmVVmV" );
20, test( "JJJ", "VVmVVmm" );
21, test( "JJZ", "mVVmVVmVmmV" );
22, test( "JSJ", "VVmmVVmVVmm" );
23, test( "JSS", "VmVVmmVmVVmVVmmVm" );
24, test( "JUS", "VmVVmVVmVVmmVmmVm" );
25, test( "JUU", "mmVVVVmmVVVmmmmVV" );
26, test( "JUZ", "mVVmVVmVVmVmmVmmV" );
27, test( "LJJ", "VmmVVVm" );
28, test( "LLS", "VmmVmVVmVVm" );
29, test( "LLU", "mmmVVVmmVVV" );
30, test( "LLZ", "mVmmVVmVVmV" );
31, test( "LSU", "mmVVVmmmVVVVmmmVV" );
32, test( "LSZ", "mVVmVmmVVmVVmVmmV" );
33, test( "LZL", "mmVVmVVmmVV" );
34, test( "LZS", "VmmVmVVmVVmmVmVVm" );
35, test( "LZU", "mmmVVVmmVVVmmmVVV" );
36, test( "SJL", "VVmVVmmmVVm" );
37, test( "SLU", "mmVVVVmmmVVmmmVVV" );
38, test( "SLZ", "mVVmVVmVmmVmmVVmV" );
39, test( "SSU", "VVVmmmVVVmmVVVmmmmVVVmmmVV" );
40, test( "SUJ", "mVVVmVVmmmVmmVVVm" );
41, test( "SUS", "VmVVmVVmVVmmVmmVmmVmVVmVVm" );
42, test( "SZZ", "mVmmVVmVVmVmmVVmVmmVmmVVmV" );
43, test( "UJJ", "VmmVVVmVVmm" );
44, test( "ULU", "mmmVVVmmVVVVmmmVV" );
45, test( "ULZ", "mVmmVVmVVmVVmVmmV" );
46, test( "UUU", "VVmmmmVVVmmVVVVmmVVVmmmmVV" );
47, test( "ZJU", "VVVmmmVVmmmVVVVmm" );
48, test( "ZLS", "VmVVmmVmmVmVVmVVm" );
49, test( "ZSJ", "VVmmVmmVVmmVVVmmV" );
50, test( "ZUJ", "mVVVmmVmmmVVmVVVm" );
51, test( "JJLJ", "mVVVmmVVmVVmmmV" );
52, test( "JLJJ", "VmmVVVmVVmmmVVm" );
53, test( "JLJL", "VmmVVVmVVmmmVVm" );
54, test( "LJJL", "VVmmVmmVVVmVVmm" );
55, test( "LLJJ", "VmmmVVmVVmmVVVm" );
56, test( "SZUS", "VmVVmVVmmVmmVmmVmVVmVVmVVmVVmmVmmVmmVmVVmVVmVVmmVmmVmmVmVVmVVmmVmmVmmVmVVmVVmVVm" );
57, test( "ULLS", "VmmVmmVmVVmVVmmVmVVmVVmVVmmVmmVmVVm" );
58, test( "JJJJZJ", "VmmVVVmmVVmVVmmVVmmmVVmVVmmVVVmmVVmmVmmVVmmmVVmVVmmVVVmmVVmVVmmVVmmmVVmmVmmVVVmmVVmmVmmVVmmmVVm" );
59, test( "JULLLJ", "mmVmmVVmmmVVmVVVmmVmmVVVmmVVmVVVmmVmmVVmmmVVmVVVmmVmmVVVmmVVmVVmmmVmmVVmmmVVmVVmmmVmmVVVmmVVmVV" );
60, test( "LJJJUL", "mVVVmVVmmmVVmVVVmmVmmmVmmVVVmVVmmmVmmVVVmmVmmmVVmVVVmVVmmmVVmVVVmmVmmmVVmVVVmVVmmmVmmVVVmmVmmmV" );
61, test( "LJSJJL", "VVmVVmmVVVmmVmmmVVmVVmmmVVmmVmmVVVmVVmmmVVmmVmmVVVmVVmmVVVmmVmmmVVmVVmmVVVmmVmmVVVmVVmmmVVmmVmm" );
62, test( "LZLLLJ", "mmVmmVVmmmVVmVVmmmVmmVVVmmVVmVVVmmVmmVVmmmVVmVVVmmVmmVVVmmVVmVVmmmVmmVVmmmVVmVVVmmVmmVVVmmVVmVV" );
63, test( "SJJJJL", "VVmVVmmVVVmmVmmVVVmVVmmmVVmmVmmVVVmVVmmVVVmmVmmmVVmVVmmmVVmmVmmmVVmVVmmVVVmmVmmVVVmVVmmmVVmmVmm" );
64, test( "ZLJLJL", "VmmVVVmmVmmmVVmVVmmVVVmVVmmmVVmmVmmVVVmmVmmmVVmmVmmVVVmVVmmmVVmVVmmVVVmmVmmmVVmVVmmVVVmVVmmmVVm" );

Commentary

doukaku_18.png

The front side of the paper is '0', the back side is '1', and the mountain fold is'm'. Let the valley fold be'V'. The L-fold result on the front side is (back, valley, front), so '1V0'. The L-fold result on the back side is (back, mountain, front), so '1m0'. Convert from L-fold to folding result to dictionary data FOLD = {'L': ('1V0', '1m0')}. The result of L folding on the front side can be retrieved as '1V0' with FOLD ['L'] [0]. The result of L folding on the back side is FOLD ['L'] [1], and '1m0' can be taken out. If there is only one L fold, the answer is to extract only the'V'of the crease data of '1V0'. In the case of LL folding, 1 and 0 representing the paper part of '1V0' can be replaced with the result of L folding, respectively. LL fold result = Further L fold operation for L fold result on the front side → Fold FOLD ['L'] [0] further L → Further L-fold operation of '1V0' (L-fold operation for 1 and 0) → FOLD['L'][1]+'V'+FOLD['L'][0] → '1m0'+'V'+'1V0' → '1m0V1V0' Then, collect only the crease characters and answer'mVV'. When this is made into a program, it becomes as follows.

  1. Extract the first operation character from the operation string
  2. Convert to operation characters Look up the dictionary and convert to the folding result
  3. If there is a subsequent operation, perform the second and subsequent operations recursively for the folding results 0 and 1.
  4. Finally, the work of removing 0s and 1s will be replaced with'' as an empty operation at the end. This will result in an error when there are no operation characters when the operation character is retrieved with operation [0], but it will be an empty string when there is no operation by writing operation [: 1], and the folding result for the empty character If you register as'': ('','') in the conversion dictionary, '0' and '1' will be replaced with empty strings when there are no more operation characters.
solve = lambda operation, side=0: ...
    #solve function definition. Same as the function using def shown below
    # def solve(operation, side=0):
    #     return ...    
operation[:1]
    #Extract the first operation character in the operation string
    #If operation is an empty string''become
FOLD[operation[:1]][side]
    #Extract the folding string for the first operation string of the operation string
    #If operation is an empty string''become
for f in FOLD[operation[:1]][side]
    #For each character in the fold string
operation[1:]
    #Second and subsequent operation strings
solve(operation[1:], int(f)) if f in '01' else f
    # '0'Or'1'Then recursively perform the second and subsequent operations,'m'Or'V'Then that character
    #Remained in the last operation'0'When'1'Solve('', side)Call''become
''.join(...)
    #Combine the folded character strings of the partial parts without any gaps to make a character string

Recommended Posts

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 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
20th Offline Real-time How to Write Problems in Python
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 18th offline real-time writing problem in Python
The 19th offline real-time writing problem in Python
The 17th Offline Real-time How to Solve Writing Problems in Python
The 15th offline real-time I tried to solve the problem of how to write with python
The 14th offline real-time writing reference problem with Python
Part 1 I wrote the answer to the reference problem of how to write offline in real time in Python
13th Offline Real-time How to Solve Writing Problems in Python
The 10th offline real-time writing reference problem. Implementation example by Python.
Offline real-time how to write Python implementation example of E15 problem
The 11th offline real-time writing reference problem. Implementation example by 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"
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
Offline real-time how to write Python implementation example of E14
How to write offline real-time Solving E05 problems with Python
The twelfth offline real-time writing reference problem. Implementation by python
How to write Ruby to_s in Python
Offline real-time how to write E11 ruby and python implementation example
How to write offline real time I tried to solve the problem of F02 with Python
How to use the C library in Python
The trick to write flatten concisely in python
How to get the files in the [Python] folder
How to retrieve the nth largest value in Python
How to get the variable name itself in python
Basic information Write the 2018 fall algorithm problem in Python
How to get the number of digits in Python
How to write string concatenation in multiple lines in Python
How to know the current directory in Python in Blender
How to use the model learned in Lobe in Python
How to develop in Python
[Python] How to output the list values in order
[Note] How to write QR code and description in the same image with python
How to write custom validations in the Django REST Framework
[python] How to check if the Key exists in the dictionary
How to debug the Python standard library in Visual Studio
[Python] How to write an if statement in one sentence.
How to get the last (last) value in a list in Python
How to write a Python class
[Python] How to do PCA in Python
How to collect images in Python
How to use SQLite in Python
In the python command python points to python3.8
[Python] How to import the library
How to use Mysql in python
How to wrap C in Python
How to use PubChem in Python
How to handle Japanese in Python
How to determine the existence of a selenium element in Python
[Python] Solving the import problem due to the difference in entry points
How to know the internal structure of an object in Python
How to check the memory size of a variable in Python
How to judge that the cross key is input in Python3