-2

Can somebody point out why am I getting infinite loop in this? I mean it shows error for maximum recursion depth reached? For value of '1' it shows correct output.

def beautiful(n):
    new=str(n+1)
    new.rstrip('0')
    return int(new)
def check(n):
    if n==1:
       temp.extend(list(range(1,10)))
       return
    else:
       temp.append(n)
       return check(beautiful(n))
    
n=int(input())
temp=[]
check(n)
print(len(set(temp)))
  • 2
    How do you expect to ever reach 1 with this procedure? Try it with pen and paper and see what happens. Also, there's a limit to the recursion depth, you should rather convert that to a loop. – Thierry Lathuille Feb 25 '22 at 18:01
  • 1
    Could you perhaps explain what you're trying to achieve with this code? – DarkKnight Feb 25 '22 at 18:14
  • @OlvinRoght I needed to write code for a problem where a input number is given and 1 is added to it. If result contains trailing zeroes it is removed. For example F(9) will give 10 which will eventually give 1. Hence the output. Lets say we take F(1) so it will give 2, and subsequently 2 will result in 3, and so on upto 9. –  Feb 25 '22 at 18:26
  • 1
    Then that defines your infinite loop. You would go from 1 to 9 OK. But then add 1 to 9 (10) remove the trailing 0 and you're back to 1 – DarkKnight Feb 25 '22 at 18:30
  • @OlvinRoght yeah got that. can u suggest a way to get rid of that infinite loop? –  Feb 25 '22 at 18:32
  • 2
    @ABHISHEKSINGH As I said, your stated requirement effectively specifies an infinite loop. Maybe you need to redefine the requirement – DarkKnight Feb 25 '22 at 18:39
  • 2
    check my iterative solution that works. – gilf0yle Feb 25 '22 at 18:54

3 Answers3

1

Unless I've completely misunderstood this, it has been over-complicated in the extreme. It's as simple as this:

Note: no recursion, no string manipulation

def check(n):
    result = []
    while n > 1:
        if (n := n + 1) % 10 == 0:
            n //= 10
        result.append(n)
    return result
print(check(17))

Output:

[18, 19, 2, 3, 4, 5, 6, 7, 8, 9, 1]
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
0
def beautiful(n):
    new=str(n+1)
    new.rstrip('0')
    return int(new)
def check(n):
    if n==1:
       temp.extend(list(range(1,10)))
       return
    else:
       temp.append(n)
       return check(beautiful(n))

this is your code where check is returning a value when n==1 if not check(beautiful(n)) but beautiful(n) returns value which is which is either 1 greater than n or removes 0 from end if exists.

for example if n=5 is passed it gets returned when value of n reaches 10 cause 0 is removed from end and at n==1 it gets returned. But as u mentioned n ranges up-to 10^9 if at all a number is something like 1234567 then it recursion stack gets piled up if at all n value reaches to 1 before recursion depth limit is exceeded it works fine if not u get the error recursion depth reached. And recursive depth has different default values eg:-2000

if you want to change ur recursive depth limit follow this

in this case using interative solution is recommended.

iterative solution!!:

n=11
temp=[]
while 1:
    if n==1:
        temp.extend(list(range(1,10)))
        break
    else:
        temp.append(n)
        n=str(n+1)
        n=n.rstrip('0')
        n=int(n)
        
print(len(temp))
gilf0yle
  • 1,092
  • 3
  • 9
0

I think you are assuming that new.rstrip('0') modifies the value of new. That is not the case:

str.rstrip([chars]) Return a copy of the string with trailing characters removed.

You can fix your issue by changing this line to new=new.rstrip('0').

You may still get recursion-depth errors for sufficiently large values.

Dave Costa
  • 47,262
  • 8
  • 56
  • 72