blob: 40eccbe644f1f4861a3db3865e5927c4e0228179 (
plain)
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
|
#!/usr/bin/env python3
abbr = {}
input = [ "alphabet", "book", "carpet", "cadmium", "cadeau", "alpine" ]
# input = [ "alphabet", "car", "carboxy", "book", "carpet", "cadmium", "cadeau", "alpine" ]
instr = ':' + ':'.join(input)
max = sorted([ len(s) for s in input])[-1]
for length in range(1, max + 1):
for word in input:
if word in abbr:
continue
if len(word) == length:
abbr[word] = word
continue
sub = ':' + word[0:length]
found = instr.count(sub)
if found == 1:
abbr[word] = sub.lstrip(':')
output = [ abbr[o] for o in input]
print("Input {}".format(input))
print("Output {}".format(output))
|