-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleft_right.py
46 lines (29 loc) · 1020 Bytes
/
left_right.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import itertools
# State 0: L = 0, R = 1
#
# Command "L": L = 2 * L - R
# Command "R": R = 2 * R - L
#
# Input:
# Output: shortest command sequence to get this number equal to L or R
def compute_number_according_to_sequence(sequence, L=0, R=1):
for command in sequence:
if command == "L":
L = 2 * L - R
elif command == "R":
R = 2 * R - L
return L, R
def generate_possible_commands(max_len, options):
return map(list, [p for p in itertools.product(options, repeat=max_len)])
def find_command_sequence(target_num):
init_cnt = 1
while True:
sequence_with_length = generate_possible_commands(init_cnt, ["L", "R"])
for commands in sequence_with_length:
L, R = compute_number_according_to_sequence(commands, 0, 1)
if L == target_num or R == target_num:
return commands
init_cnt += 1
target_num = int(raw_input('Input target number '))
res = find_command_sequence(target_num)
print res