I'm writing code that returns a list containing all strings of the given length over the alphabet alpha in no particular order. For example:
all_strings({0, 1}, 3))
should return
['000', '001', '010', '011', '100', '101', '110', '111']
The two functions:
def all_strings(alpha, length):
alpha_list = [] # store in a list the elements in the set alpha
for symbol in alpha:
alpha_list.append(symbol)
if length == 1:
return alpha_list
else:
return recurs_func(alpha, length, alpha_list)
def recurs_func(alpha, length, update_list):
new_list = [] # list to store new strings
for j in alpha:
for k in update_list:
new_list.append(j+k)
if length == 2: # done creating strings of desired length
return new_list
else:
recurs_func(alpha, length-1, new_list)
The code works fine except if I choose length to be >= 3 in which case None is returned and wondering how to fix this.