Search⌘ K
AI Features

Challenge: Shuffle Integers

In this lesson, we will solve a challenge on shuffling a list of 2n integers without using extra space.

Shuffle integers

Suppose you have a list of size 2n2^n (size of the list is a multiple of 2) arranged as:

In the context of this problem, we will call this array shuffled if its elements were to get re-arranged like this:

Problem statement:

Given a list of integers of size 2n2^n and taking all the boundary cases in consideration, shuffle the list, without using extra space.

Input

  • lst (list of integers)

Output

  • lst (shuffled list if the size of the list is in 2n2^n, otherwise the same list)

Sample input

lst = [1, 2, 3, 4, 5, 6, 7, 8]

Sample output

lst = [1, 5, 2, 6, 3, 7, 4, 8]

Coding challenge

First, take a close look at this problem and design a step-by-step algorithm before jumping to the implementation. This problem is designed for your practice, so try to solve it on your own first. If you get stuck, you can always refer to the solution provided in the solution section. Good luck!

Python 3.5
import math
def shuffle_list(lst):
"""
Shuffles the list
:param lst: List of integers
"""
# Write your code here!
pass