1
3 3
1 1
3 3
Pass the ball game: NxN matrix in python
Carvia Tech  May 18, 2019  3 min read  61 views  Python Coding Problems
One of the game in Raj’s summer camp is passing the ball. All the participants in the game are seated on ground which are arranged in R rows and C columns. Raj is sitting at position X, Y, where X is the row number and Y is the column. The game starts when the coach gives the ball to a participant sitting in any of the corner (1,1) or (1,C) or (R,1) or (R,C) . Participant should pass the ball to the next participant sitting to left, right, front or back.
Participant should pass the ball with below priority:

If there is a participant in the Right and was not passed the ball earlier, pass it to right.

If there is a participant in the Left and was not passed the ball earlier, pass it to left.

If there is a participant in the Front and was not passed the ball then pass it front.

If there is a participant in the Back and was not passed the ball earlier then pass it to back.

Participant should say "Over", if all the four (left, right, front, back) have passed the ball.
Raj is very curious and does not want to wait till the ball comes to him to find out which direction he has to pass the ball.
Write a program to find out where Raj has to pass the ball or should he say "Over"
Input
First line consists of a single integer, T, denoting the number of test cases.
Each test case consists of three lines.
First line is two integers denoting R and C.
Second line is two integers SX and SY, denoting the starting corner of distribution
Third line is two integers X and Y deoniting where Raj sits.
Output
For each case, print the output as the direction in which he needs to pass the ball. It could be (Left, Right, Front, Back or "All Done")
Sample Output 1
Over
2
2 3
1 3
2 2
3 3
1 3
2 3
Solution
The below program in python will calculate the next move based on logic given above.
import numpy as np
def move_next(empty_matrix, current_position, size):
if (current_position[1] + 1 < size[1]) and (empty_matrix[current_position[0], current_position[1] + 1] == 0): ## Move Right
current_position[1] = current_position[1] + 1
empty_matrix[current_position[0], current_position[1]] = 1
return "Move Right"
if (current_position[1]  1 >= 0) and (empty_matrix[current_position[0], current_position[1]  1] == 0): ## Move Left
current_position[1] = current_position[1]  1
empty_matrix[current_position[0], current_position[1]] = 1
return "Move Left"
if (current_position[0] + 1 < size[0]) and (empty_matrix[current_position[0] + 1, current_position[1]] == 0): ## Move front
current_position[0] = current_position[0] + 1
empty_matrix[current_position[0], current_position[1]] = 1
return "Move Front"
if (current_position[0]  1 > 0) and (empty_matrix[current_position[0]  1, current_position[1]] == 0): ## Move Back
current_position[0] = current_position[0]  1
empty_matrix[current_position[0], current_position[1]] = 1
return "Move Back"
return "Over"
if __name__ == "__main__":
# t = int(input())
for _ in range(int(input())):
size = [int(i) for i in input().split()]
starting_corner = [int(i) for i in input().split()]
raj_post = [int(i) for i in input().split()]
empty_matrix = np.zeros(tuple(size))
print(_, size, starting_corner, raj_post)
# print(empty_matrix)
raj_post = [raj_post[0]1, raj_post[1]1]
current_position = [starting_corner[0]1, starting_corner[0]1]
empty_matrix[current_position[0], current_position[1]] = 1
print(empty_matrix)
move = move_next(empty_matrix, current_position, size)
lastMove = False
while move != "Over":
print(empty_matrix)
if current_position == raj_post:
lastMove = True
move = move_next(empty_matrix, current_position, size)
if lastMove:
print(move)
Python Coding Problems:
 Spaced Out?
 Sequence of Differences in Python
 Find if credit card number is valid or not
 Find extra long factorials in python
 Python coding challenges for interviews
 Write a program to check if the given word is Isogram & Pair isogram in python
 Count the number of open lockers in school
Top articles in this category:
 Python coding challenges for interviews
 Python Flask Interview Questions
 Top 100 interview questions on Data Science & Machine Learning
 Google Data Scientist interview questions with answers
 Sequence of Differences in Python
 Find extra long factorials in python
 Send email with attachment in Python
Find more on this topic:
Machine Learning
Data science, machine learning, python, R, big data, spark, the Jupyter notebook, and much more
Last updated 1 week ago
Recommended books for interview preparation:
Similar Posts
 Installing PySpark with Jupyter notebook on Ubuntu 18.04 LTS
 Send email with attachment in Python
 Send rich text multimedia email in Python
 Blueprints in Flask API Development
 Singleton Design Pattern in Python
 SVM after LSTM deep learning model for text classification
 RuntimeError: get_session is not available when using TensorFlow 2.0
 Deploying Keras Model in Production with TensorFlow 2.0
 Python coding challenges for interviews
 Python Flask Interview Questions
Enter your email address to subscribe to this blog and receive notifications of new posts by email.