I have a question a dynamic programming problem. I find it very difficult, so I hope I can get some help.
Given a list of words and the length n of a password, I would like to know the amount of possible combinations of passwords. Passwords can be either a single word of length n or a combination of words separated by underscores.
For example, passwords(5, ["house, "no"]) = 2 because the possible passwords are "house" and "no_no".
What I have tried so far:
def voc_to_list(vocabulary):
"""
produces a list lengths such that lengths[i] is the number of
words of length i in vocabulary
"""
max_len = max([len(w) for w in vocabulary])
lengths = [0] * (max_len + 1)
for w in vocabulary:
wordLength = len(w)
lengths[wordLength] += 1
return lengths
def passwords(L, vocabulary):
lengths = voc_to_list(vocabulary)
k = len(lengths)
tbl = [0] * (L + 1)
for i in range(L + 1):
if i < k:
tbl[i] = lengths[i]
for j in range(min(i, k)):
# This is where I am stuck
tbl[i] += ??
return tbl[L]
Inside tbl[i] I already have the words of length i. I am stuck in how to fill out the table to take into account the combinations of words.