Shuffle a integer using recursive function
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])))
Without converting to a string:
def shuffle(x):
if x < 100:
return x
t = x
l = 0
while t > 0:
t //= 10
l += 1
a = x // 10 ** (l-1) * 10 ** (l-1)
b = (x % 10) * 10 ** (l-2)
return a + b + shuffle((x - a) // 10)
and the tests work:
>>> shuffle(123456)
162534
>>> shuffle(310)
301