-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbr.py
executable file
·58 lines (45 loc) · 1.27 KB
/
nbr.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def str2trits(s):
return [{'-': -1, '0': 0, '+': 1}[c] for c in s]
def trits2bits(trits):
bits = []
for trit in trits:
if trit == -1:
bits += [1, 1]
elif trit == 0:
bits += [0, 0]
elif trit == 1:
bits += [0, 1]
else:
assert False
return bits
def bits2str(bits):
return ''.join({0: '0', 1: '1'}[x] for x in bits)
def main():
from sys import argv
if len(argv) == 3:
in_f = open(argv[1], 'r')
out_f = open(argv[2], 'w')
elif len(argv) == 1:
from sys import stdin, stdout
in_f = stdin
out_f = stdout
else:
print(f'Usage: {argv[0]} [in_filename out_filename]')
exit(1)
with out_f, in_f:
for line in in_f:
line = line.strip()
if len(line) <= 0:
continue
assert len(line) >= 9
trits_str, comment = line[:9], line[9:]
assert comment.lstrip()[0] == '#'
trits = str2trits(trits_str)
bits = trits2bits(trits)
bits_str = bits2str(bits)
out_line = f'{bits_str}{comment}\n'
out_f.write(out_line)
if __name__ == '__main__':
main()