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.

0

2 Answers 2

1

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

return recurs_func(alpha, length-1, new_list)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.