- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions

# Program to arrange cards so that they can be revealed in ascending order in Python

Suppose we have a list of cards, and we want to order the cards in a way so that they are revealed in ascending order. As we know, the cards are revealed in this manner: 1. The top most card is removed and revealed and then next card is gone to the back. 2. Step 1 is repeated until there's no more cards. We have to find an ordering of the cards such that they are revealed in ascending order.

So, if the input is like cards = [1, 2, 3, 4, 5, 6, 7, 8], then the output will be [1, 5, 2, 7, 3, 6, 4, 8], as 1 is removed and 5 is moved to the back, current situation [2, 7, 3, 6, 4, 8, 5]. 2 is removed and 7 is moved to the back, current situation [3, 6, 4, 8, 5, 7] 3 is removed and 6 is moved to the back, current situation [4, 8, 5, 7, 6] 4 is removed and 8 is moved to the back, current situation [5, 7, 6, 8] 5 is removed and 7 is moved to the back, current situation [6, 8, 7]. 6 is removed and 8 is moved to the back, current situation [7, 8]. 7 is removed and there is only one card [8]. Then remove [8]

To solve this, we will follow these steps −

- sort the list cards
- idx:= a list with elements 0 to length of cards
- order:= a new list
- q:= a queue and insert elements of idx
- while q is non-zero, do
- delete element from left of q and insert into order
- if q is non-zero, then

- ans:= make a list of size cards, and fill with 0
- for each element i from order and card from cards, do
- ans[i]:= card

- return ans

Let us see the following implementation to get better understanding −

## Example

from collections import deque class Solution: def solve(self, cards): cards.sort() idx=[i for i in range(len(cards))] order=[] q=deque(idx) while q: order.append(q.popleft()) if q: q.append(q.popleft()) ans=[0 for _ in cards] for i,card in zip(order,cards): ans[i]=card return ans ob = Solution() print(ob.solve([1, 2, 3, 4, 5, 6, 7, 8]))

## Input

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

## Output

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

- Related Articles
- Program to find number of ways to arrange n rooks so that they cannot attack each other in Python
- How to arrange the fractions in ascending order and descending order?
- Program to find the minimum cost to arrange the numbers in ascending or descending order in Python
- Arrange the following in ascending order.\( -9,4,5,-4,0 \)
- Arrange the following numbers in ascending order:$9801,\ 25751,\ 36501,\ 38802$
- Program to merge intervals and sort them in ascending order in Python
- Python program to sort out words of the sentence in ascending order
- Program to sort a given linked list into ascending order in python
- Python program to sort the elements of an array in ascending order
- Program to find overlapping intervals and return them in ascending order in Python
- Metals which are so soft that they can be cut with a knife are _________, ___________.
- 8085 Program to perform selection sort in ascending order
- 8085 Program to perform bubble sort in ascending order
- Sort index in ascending order – Python Pandas
- Program to get indices of a list after deleting elements in ascending order in Python