forked from mooja/dailyprogrammer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge107easy.py
63 lines (45 loc) · 1.21 KB
/
challenge107easy.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
#!/usr/bin/env python
# encoding: utf-8
# Daily Programmer Challenge 107 Easy
#
# http://www.reddit.com/r/dailyprogrammer/comments/122c4t/10252012_challenge_107_easy_all_possible_decodings/
#
# May.11.2015
from string import ascii_lowercase
from itertools import combinations
def break_down(num):
digits = list(num)
ns = range(1, len(digits)) # n = 1..(n - 1)
for n in ns: # split into 2, 3, 4... n parst
for idxs in combinations(ns, n):
yield [''.join(digits[i:j]) for i, j in zip((0,) + idxs, idxs + (None,))]
def is_valid_combination(combination):
for num in combination:
if int(num) == 0 or int(num) > 27:
return False
return True
def num2alpha(numstr):
num = int(numstr)
return ascii_lowercase[num-1]
def get_decodings(num):
combs = []
for comb in break_down(num):
if is_valid_combination(comb):
combs.append(''.join(map(num2alpha, comb)))
return combs
if __name__ == '__main__':
print '\n'.join(get_decodings('85121215'))
# output:
# hello
# heablo
# heaubo
# heauue
# helabo
# helaue
# hellae
# heababo
# heabaue
# heablae
# heaubae
# helabae
# heababae