Hey. I know this is not a 'refactor my code' site but I made this little piece of code which works perfectly fine with moderately sized input but it's problematic with string of size, say, over 2000.
What it does - it takes a string of numbers as a parameter, and it returns the number of ways it can be interpreted as a string of letters, where each letter in the English alphabet is assigned a numeric value according to its lexical position: A -> 1, B-> 2, Z-> 26 etc.
Since some letters are represented as two numbers the suffix tree is not unique so there can be multiple interpretations. For example '111' could be both 'AAA', 'KA' and 'AK'.
This is my code. It's fairly readable and straightforward but it's problematic because:
- It has to copy part of the string every time to call it as argument to the recursive part.
- It has to store huge strings in the cache so it's very expensive, memory-wise.
- ... it's recursive.
Help much appreciated :)
cache = dict()
def alpha_code(numbers):
"""
Returns the number of ways a string of numbers
can be interpreted as an alphabetic sequence.
"""
global cache
if numbers in cache: return cache[numbers]
## check the basic cases
if numbers.startswith('0'): return 0
if len(numbers) <= 1: return 1
## dynamic programming part
## obviously we can treat the first (non-zero)
## digit as a single letter and continue -
## '342...' -> C + '42...'
total = alpha_code(numbers[1:])
## the first two digits make for a legal letter
## iff this condition holds
## '2511...' -> Y + '11...'
## '3711...' -> illegal
if numbers[:2] <= '26':
total += alpha_code(numbers[2:])
cache[numbers] = total
return total