1

I am trying to build a function(which uses function recursion) that scans a number, n, to look for a digit d, & if found, I would like to replace d with a specified number r, as shown in the code below. This code works fine but the output is in a string format. I have tried numerous ways to change it to output an integer but to no avail. Thanks for the help!

def replace_digit(n, d, r):
    number = str(n)
    i = 0
    if len(number) == 1:
        if number == str(d):
            return str(r)
        else:
            return number
    else:
        if number[i] == str(d):
            return number[:i] + str(r) + replace_digit(int(number[i+1:]),d,r)
        else:
            return number[i] + replace_digit(int(number[i+1:]),d ,r)
3
  • Is it an exercise? in other words, do you specifically required to use recursion? this is a very trivial task. Commented Aug 18, 2016 at 10:14
  • 2
    Just wrap the return value in a call to int() Commented Aug 18, 2016 at 10:14
  • Yeah, I have to specifically write in recursion, hence the problem. Thanks. Commented Aug 18, 2016 at 10:15

5 Answers 5

1
def replace_digit(number, digit, replacement):

    if number == 0:
        return number  # base case

    quotient, remainder = divmod(number, 10)

    if remainder == digit:
        remainder = replacement

    return replace_digit(quotient, digit, replacement) * 10 + remainder


print(replace_digit(961748941982451653, 9, 2))

OUTPUT

261748241282451653
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome. I was also thinking about a solution based purely on integer calculations, but I had the feeling that OP is actually struggling with understanding type conversions.
0

If you already have a working function why not just split the problem ?

def replace(n, d, r):
    def replace_digit(n, d, r): # doesn't change
        return ...
    return int(replace_digit(str(n), str(d), str(r))

1 Comment

Thanks a lot man! Was getting close to a solution of some sort like this. :)
0

The solution is to wrap the return values with int() as has been already been stated in the comments and other answers.

But here's a version that doesn't use string manipulation at all. Just for fun.

def replace_digit(n, d, r):
    rest = n // 10  # all but the rightmost digit
    digit = n - rest * 10  # only the rightmost digit
    digit = r if digit == d else digit
    if rest == 0:
        return digit

    return replace_digit(rest, d, r) * 10 + digit

Comments

0

It's quite simple but you need some type conversions between str and int:

def replace_digit(n, d, r):
    number = str(n)
    rest = str(replace_digit(int(number[1:]), d, r)) if len(number) > 1 else ""
    digit = number[0]
    digit = str(r) if digit == str(d) else digit
    return int(digit + rest)

There's also another possiblity, using a wrapper. This limits number of type conversions.

def replace_digit(n, d, r):
    def replace(n, d, r):
        rest = replace(n[1:], d, r) if len(n) > 1 else ""
        return r + rest if n[0] == d else n[0] + rest

    return int(replace(str(n), str(d), str(r)))

Comments

0

Instead of returning the concatenation try this:

if number[i] == str(d):
    new_number = number[:i] + str(r) + replace_digit(int(number[i+1:]),d,r)
else:
    new_number = number[i] + replace_digit(int(number[i+1:]),d ,r)

return int(new_number)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.