decoding a string
Can't remember the exact name of this problem, but essentially involves counting the number of different ways a string of numbers representing letters of the alphabet can be interpreted.
1S = "26157"
2data = {'ways':-1}
3
4def count_number_of_ways(s):
5 data['ways'] += 1
6 if len(s) >= 2:
7 count_number_of_ways(s[1:])
8 if len(s) >= 2 and int(s[0:2]) <= 26:
9 count_number_of_ways(s[2:])
10 return data['ways']
11
12ways = count_number_of_ways(S)
13print(ways)
Output:
12 --> 6157
26 --> 157
31 --> 57
45 --> 7
515 --> 7
626 --> 157
71 --> 57
85 --> 7
915 --> 7
109