This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
62 lines (55 loc) · 1.39 KB
/
utils.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''
@author: sitdownkevin
@data: 2023.4.16
'''
# 判断命令是否合法
def isCMDLegal(msg):
if len(msg) != 10: return False
for letter in msg:
if not (letter == '0' or letter == '1'):
return False
return True
# Encode Process
def decimal_to_ternary(num):
if num == 0:
return '00000'
ternary = ''
while num != 0:
remainder = num % 3
ternary = str(remainder) + ternary
num //= 3
ternary = ternary.rjust(5, '0')
return ternary
# Encode Process
def string_to_binary(ternaryStr):
binary_dict = {
'0': '00',
'1': '01',
'2': '10'
}
binary = ''
for num in ternaryStr:
assert int(num) < 3
binary += binary_dict[num]
return binary
# Encode
def encode(num) -> str:
return string_to_binary(decimal_to_ternary(num))
def translate(command):
assert len(command) == 10
motion_dict = {
'00': '静止',
'01': '正转',
'10': '反转'
}
print(f'A: {motion_dict[command[0:2]]}\n' + \
f'B: {motion_dict[command[2:4]]}\n' + \
f'C: {motion_dict[command[4:6]]}\n' + \
f'D: {motion_dict[command[6:8]]}\n' + \
f'E: {motion_dict[command[8:10]]}\n'
)
if __name__ == "__main__":
for cmd in range(243):
cmd_ = encode(cmd)
print(f'{cmd} -> {cmd_}')
translate(cmd_)