1

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.

SCB
  • 5,821
  • 1
  • 34
  • 43
jaytee0409
  • 11
  • 1

2 Answers2

1

You forgot a return on the last line. It should be:

return recurs_func(alpha, length-1, new_list)
Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
0

In recurs_func, if length is not 2, the recursive call is made, but nothing is returned.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101