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
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.
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