A recursive function that takes an integer and shuffles it. Shuffle one digit from the front and one digit from the back, then the second from the front and the second from the back, and so on until the shuffled number is the same as that original.
For example, 123456 would print as 162534 or 130 would print as 103. Any help would be appreciated.
On the string, it's easy, need the suggestion for integer.
A = '130'
def shuffle(A):
if len(A) <= 2:
return A
return (A[0] + A[-1]) + shuffle(A[1:-1])
Output: 103
convert it to a string?
A = 130
def shuffle(A):
A = str(A)
if len(A) <= 2:
return int(A)
return int((A[0] + A[-1]) + str(shuffle(A[1:-1])))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With