- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions

# Program to find out the position of a ball after n reversals in Python

Suppose, there are n number of balls. The balls are ordered in a fashion 1,2,3,4,...,n. Now the balls are reversed in order, or ordered in a way n, n-1, n-2, ......, 2, 1. The balls are again reversed in order, this time they are reversed from position 1 to n, or now the order becomes n, 1, 2,....., n-1. This reversing process is repeated n times and each time the starting position is moved 1 place to the right. We now have to find out the position of a ball initially in position 'index' after the reversals.

So, if the input is like balls = 5, index = 2, then the output will be 4 The balls initially are: 1, 2, 3, 4, 5

Then,

5,4,3,2,1 5,1,2,3,4 5,1,4,3,2 5,1,4,2,3

The ball in position 2 is currently at position 4.

To solve this, we will follow these steps −

- if index < floor value of (balls / 2), then
- return 2 * index + 1

- otherwise,
- return 2 *(balls - index - 1)

## Example

Let us see the following implementation to get better understanding −

def solve(balls, index): if index < balls // 2: return 2 * index + 1 else: return 2 * (balls - index - 1) print(solve(5, 2))

## Input

5, 2

## Output

4

- Related Articles
- Program to find out the sum of the maximum subarray after a operation in Python
- Python Program to find out the price of a product after a number of days
- Find the position of box which occupies the given ball in Python
- Check if Matrix remains unchanged after row reversals in Python
- Program to find next board position after sliding the given direction once in Python
- Program to find maximize score after n operations in Python
- Program to find out the minimum number of moves for a chess piece to reach every position in Python
- Program to find out if the strings supplied differ by a character in the same position in Python
- Program to Find Out the Probability of Having n or Fewer Points in Python
- Program to find number of items left after selling n items in python
- Program to find out is a point is reachable from the current position through given points in Python
- Program to find remainder after dividing n number of 1s by m in Python
- Program to find the number of possible position in a line in Python
- Program to find where the ball lands in a grid box in Python
- Program to find out the value of a given equation in Python