-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.py
More file actions
69 lines (56 loc) · 1.72 KB
/
Copy pathdecoder.py
File metadata and controls
69 lines (56 loc) · 1.72 KB
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
63
64
65
66
67
68
69
#!/usr/bin/env python3
"""Decoder -- Program 13 from "Marvel Super Heroes Computer Fun".
Faithful recreation of the type-in BASIC listing: inverts the Program 12
encoder. Strip blanks from input, swap '*' back to space, then for I=1..5,
J=0..MM-1 read MM$[I+J*5] -- walks the 5-char column blocks back into rows.
Type one coded line at a time (uppercase); type STOP to finish.
python decoder.py
"""
import os
import sys
GREEN = "\033[32m"
RESET = "\033[0m"
CLEAR = "\033[2J\033[H"
def enable_ansi():
if os.name == "nt":
os.system("")
def decode(M): # lines 170-300
MM = ""
for ch in M: # 170-220
if ch == " ":
continue # 190
MM += " " if ch == "*" else ch # 200
mm = len(MM) // 5 # 230
A = ""
for I in range(1, 6): # 240
for J in range(0, mm): # 250
A += MM[I + J * 5 - 1] # 260 (1-based MID$ -> 0-based index)
return A
def play():
enable_ansi()
sys.stdout.write(CLEAR + GREEN)
print("ENTER MESSAGE")
print()
while True:
try:
line = input("? ").strip().upper()
except (EOFError, KeyboardInterrupt):
break
if line == "STOP": # 160
break
if not line:
continue
print()
print("DECODED MESSAGE:")
print()
print(decode(line))
print()
print()
sys.stdout.write(RESET)
def main():
play()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.stdout.write(RESET + "\n")