Skip to content

S1Q3 · Four Digit Shuffle

⚡ Quick Reference

Function: shuffle_digits(num: int) -> int

Core idea: extract the 4 digits, rearrange in order [d1, d3, d0, d2] (0-indexed), reassemble.

def shuffle_digits(num: int) -> int:
    s = str(num)
    return int(s[1] + s[3] + s[0] + s[2])

Shuffle rule (0-indexed): positions 0,1,2,3 → output order 1,3,0,2

Output pos Taken from input pos
1st 2nd (index 1)
2nd 4th (index 3)
3rd 1st (index 0)
4th 3rd (index 2)

Problem Statement

Problem

Write a function shuffle_digits(num) that takes a 4-digit integer and rearranges its digits in the order: 2nd, 4th, 1st, 3rd (1-indexed).

Examples:

Input
1234
Output
2413
Input
2413
Output
4321
Input
4321
Output
3142

Understanding the problem

The shuffle rule in 1-indexed terms: 2nd → 1st, 4th → 2nd, 1st → 3rd, 3rd → 4th

Converting to 0-indexed string positions:

Input:  d[0]  d[1]  d[2]  d[3]
         1     2     3     4      ← (1-indexed position)

Output: d[1]  d[3]  d[0]  d[2]

For 1234:

d[0]=1, d[1]=2, d[2]=3, d[3]=4
Output: d[1] d[3] d[0] d[2] = 2 4 1 3 → 2413 

It's a permutation cycle

The examples show that applying the shuffle 4 times returns to the original: 1234 → 2413 → 4321 → 3142 → 1234. This is a 4-cycle permutation -a good way to verify your implementation is correct.


Tracing all examples

Input d[0] d[1] d[2] d[3] Output = d[1]d[3]d[0]d[2]
1234 1 2 3 4 2413
2413 2 4 1 3 4321
4321 4 3 2 1 3142
3142 3 1 4 2 1234

Solution approaches

def shuffle_digits(num: int) -> int:
    s = str(num)
    return int(s[1] + s[3] + s[0] + s[2])

Convert to string, pick digits by index, reassemble.

def shuffle_digits(num: int) -> int:
    s = str(num)
    d0, d1, d2, d3 = s[0], s[1], s[2], s[3]
    return int(d1 + d3 + d0 + d2)

Name each digit explicitly -makes the shuffle rule immediately readable.

def shuffle_digits(num: int) -> int:
    d0 = num // 1000          # thousands
    d1 = (num // 100) % 10   # hundreds
    d2 = (num // 10) % 10    # tens
    d3 = num % 10            # units
    return d1 * 1000 + d3 * 100 + d0 * 10 + d2

Extract digits with integer division and modulo, then reassemble using place values. No string conversion at all.

def shuffle_digits(num: int) -> int:
    digits = list(str(num))
    order = [1, 3, 0, 2]
    return int("".join(digits[i] for i in order))

The order list encodes the permutation explicitly -[1, 3, 0, 2] means "take index 1 first, then 3, then 0, then 2". Easy to modify if the shuffle rule changes.

shuffle_digits = lambda num: int((s := str(num))[1] + s[3] + s[0] + s[2])

Uses a walrus operator (:=) to assign s = str(num) inside the lambda expression, avoiding repeated conversion. Compact but requires Python 3.8+.


Key takeaways

01

str → rearrange → int

The standard pattern for digit rearrangement: convert to string for easy indexing, reorder the characters, convert back to int. Works for any digit permutation.

02

Encode the permutation as a list

order = [1, 3, 0, 2] makes the shuffle rule data rather than hardcoded logic. Change the list to change the shuffle -no rewriting the function body.

03

Verify with the cycle property

Applying the same shuffle 4 times returns the original number. Run shuffle_digits(shuffle_digits(shuffle_digits(shuffle_digits(1234)))) -if you get 1234, the implementation is correct.