-1

Here is a python function I wrote to generate alphanumeric string and it prints the results correctly however it is not returning any values.

  • what am I missing?
import random
import string

def randalphn(nchar :int, nnums:int,result:str) -> str:
    if nchar == 0:
        if nnums == 0:
            print(result)
            return result
        else:
            randalphn(nchar,(nnums - 1), result+str(random.randrange(9)))
    else:
        randalphn(nchar-1, nnums, result+random.choice(string.ascii_uppercase))


#calling the function prints the result correctly however it does not return any value :(
randalphn(2,4,'')
skulk001
  • 64
  • 5
  • It seems that the function will only return the input value result `if nchar == 0` and `if nnums == 0`. Otherwise `None`. – Niel Godfrey Pablo Ponciano Sep 10 '21 at 00:22
  • @NielGodfreyPonciano That does not explain it, as the if-statement has to be True for the print-statement to run - and it does. – Bialomazur Sep 10 '21 at 00:27
  • 1
    `randalpha` has to return the result in the else cases as well. – Mark Tolonen Sep 10 '21 at 00:27
  • 1
    Actually - I resolved it. - I was not returning the function on both of the recursive calls: return randalphn(nchar,(nnums - 1), result+str(random.randrange(9))) – skulk001 Sep 10 '21 at 00:28
  • 2
    Does this answer your question? [Why does my recursive function return None?](https://stackoverflow.com/questions/17778372/why-does-my-recursive-function-return-none). Also [Recursive function returning none in Python](https://stackoverflow.com/questions/19215141/recursive-function-returning-none-in-python) and many others. – ggorlen Sep 10 '21 at 00:35
  • 3
    As an aside, [recursion is inappropriate](https://stackoverflow.com/questions/66842109/recursion-applied-to-lists-vs-trees-in-python/66846438#66846438) for this sort of linear algorithm. It's much easier, more elegant and safer (you won't blow the call stack on `n > 1000` in CPython) to use something like `"".join([chr(random.randint(97, 122)) for _ in range(n)])`, In addition to recursion, there's accidental quadratic complexity due to [Shlemiel the Painter's algorithm](https://en.wikichip.org/wiki/schlemiel_the_painter%27s_algorithm). – ggorlen Sep 10 '21 at 01:03
  • @ggorlen - maybe however this function is iterative - https://chrispenner.ca/posts/python-tail-recursion – skulk001 Sep 10 '21 at 01:09
  • Your function isn't. Python doesn't support tail recursion -- the post you linked shows how to hack tail recursion into a language that, by the creator's own admission, is definitely not functional and will never support tail recursion natively. Trying to apply this to your code at best saves the stack overflow, but it's still overengineered, more difficult to read than a simple list comprehension, unpythonic and slow (call stacks take a long time to create and tear down). Did you read the links I shared? – ggorlen Sep 10 '21 at 01:10
  • 100% over engineered - I agree with you - I was playing with this in python. Thank you for the links - I'll check it out. I do understand that python doesn't support tail recursion and that's why I added the accumulator – skulk001 Sep 10 '21 at 01:57

1 Answers1

2

You just need to add "return" when calling the function recursively as well. Notice the "else" blocks. If you dont add the return, then your function calls itself recursively but doesn't return anything -- which is what you're facing at the moment.

import random
import string
def randalphn(nchar :int, nnums:int,result:str) -> str:
    if nchar == 0:
        if nnums == 0:
            print(result)
            return result
        else:
            return randalphn(nchar,(nnums - 1), result+str(random.randrange(9)))
    else:
        return randalphn(nchar-1, nnums, result+random.choice(string.ascii_uppercase))
Shubham Periwal
  • 2,198
  • 2
  • 8
  • 26