1

Hi I have a question with regards to python programming for my assignment

The task is to replace the occurrence of a number in a given value in a recursive manner, and the final output must be in integer

i.e. digit_swap(521, 1, 3) --> 523 where 1 is swapped out for 3

Below is my code and it works well for s = 0 - 9 if the final answer is outputted as string

def digit_swap(n, d, s):
    result = ""
    if len(str(n)) == 1:
        if str(n) == str(d):
            return str(s)
        else:
            return str(n)
    elif str(n)[0] == str(d):
        result = result + str(s) + str(digit_swap(str(n)[1:], d, s))
        return result
    else:
        result = result + str(n)[0] + str(digit_swap(str(n)[1:], d, s))
        return result

However, I have trouble making the final output as Integer The code breaks down when s = 0 i.e. digit_swap(65132, 1, 0) --> 6532 instead of 65032

Is there any fix to my code?

def digit_swap(n, d, s):
    result = ""
    if len(str(n)) == 1:
        if str(n) == str(d):
            return str(s)
        else:
            return str(n)
    elif str(n)[0] == str(d):
        result = result + str(s) + str(digit_swap(str(n)[1:], d, s))
        return int(result) # Changes
    else:
        result = result + str(n)[0] + str(digit_swap(str(n)[1:], d, s))
        return int(result) # Changes
3
  • This is very unclear to me, if I understand your question correctly, the second example should be 6500 and not 651. Commented Feb 27, 2019 at 8:33
  • @IMCoins - That's where the code breaks down, the unwanted behavior produced by the given code. Commented Feb 27, 2019 at 8:35
  • @IMCoins I apologize and I have edited the phrasing more clearly, if I used the second code for digit_swap(65132, 1, 0) , I will get 6532 instead of 65032 as the recursive method will make "032" an integer, resulting in 32 I'm not too sure if there are other ways to convert from string to integer for the final output for a recursive method Commented Feb 27, 2019 at 8:41

3 Answers 3

1

Conversion to string is unnecessary, this can be implemented much easier

def digit_swap(n, d, s):
    if n == 0:
        return 0
    lower_n  = (s if (n % 10) == d else (n % 10))
    higher_n = digit_swap(n // 10, d, s) * 10
return higher_n + lower_n

assert digit_swap(521, 1, 3) == 523
assert digit_swap(65132, 1, 0) == 65032
Sign up to request clarification or add additional context in comments.

Comments

0

do not return int from method, instead convert it to int from where you are calling method. Problem lies where your code trying to convert string to int return int(result). So if result is 03 then function will return int('03') i.e 3. call your method like this print int(digit_swap(65132, 1, 0)) so you will get integer at the end.

Comments

0

For example int(00) is casted to 0. Therefore a zero is discarded. I suggest not to cast, instead leave it as a string. If you have to give back an int, you should not cast until you return the number. However, you still discard 0s at the beginning. So all in all, I would suggest just return strings instead of ints:

return str(result) # instead of return int(result)

And call it:

int(digit_swap(n,d,s))

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.