Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shuffle a integer using recursive function

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

like image 658
data_nerd Avatar asked Jul 09 '18 21:07

data_nerd


1 Answers

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])))
like image 110
nosklo Avatar answered Sep 28 '22 09:09

nosklo