-
Notifications
You must be signed in to change notification settings - Fork 3
/
shortest_prefix.py
39 lines (29 loc) · 930 Bytes
/
shortest_prefix.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
import sys
# look for lines with shortest prefix of len 4
PREFIX_SIZE = 2
if len(sys.argv) != 2:
print("expected input file as argument...")
input_file = sys.argv[1]
cur_prefix = '0000'
cur_possible_vals = set(range(255))
found_shortest_prefixes = []
with open(input_file) as f:
for i, line in enumerate(f):
# TODO base these on prefix size. don't hardcode
line = line.split(',')[0]
line = line.strip('\n')
line = line[2:]
prefix, val = line[0:4], line[4:6]
if prefix != cur_prefix:
cur_prefix = prefix
#print(cur_prefix)
if len(cur_possible_vals) != 0:
# found_shortest_prefixes += list(cur_possible_vals)
for item in list(cur_possible_vals):
item_str = hex(item)[2:]
if len(item_str) != 2:
item_str = '0' + item_str
print(cur_prefix + item_str)
cur_possible_vals = set(range(255))
if int(val, 16) in cur_possible_vals:
cur_possible_vals.remove(int(val, 16))