1
3 3
1 1
3 3
Pass the ball game: NxN matrix in python
Carvia Tech  May 18, 2019  3 min read  32 views  Python Coding Problem
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 Problem:
 Write a program to check if the given word is Isogram & Pair isogram in python
 Write a python program to find Largest Substring that occurs more than once
 Count the number of open lockers in school
 Write a program for Hailstone Sequence in Python
 Find perfect abundant or deficient factors in python
 Write a program to find if a number is Lychrel Number in Python
Top articles in this category:
 Top 100 interview questions on Data Science & Machine Learning
 Google Data Scientist interview questions with answers
 Introduction to Python 3.6 & Jupyter Notebook
 Creating custom Keras callbacks in python
 Write a program for Hailstone Sequence in Python
 Sum of the Diagonals
 Creating AWS Lambda using python 3.6
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 month ago
Recommended books for interview preparation:
Facebook Page
Similar Posts
 Closest Points
 Sum of the Diagonals
 How to design a customer satisfaction survey
 When using Gaussian mixture model, how do you know it is applicable
 Why use feature selection in machine learning
 What is the difference between Kmean and EM? How to decide k
 Explain a probability distribution that is not normal and how to apply that
 Google Data Scientist interview questions with answers
 Count the number of open lockers in school
 Introduction to Sorting Algorithms
Enter your email address to subscribe to this blog and receive notifications of new posts by email.