Pass the ball game: NxN matrix in python

Upasana | May 18, 2019 | 3 min read | 878 views | Python Coding Problems


Pass the Ball Game

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:

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

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

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

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

  5. 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 Input 1
1
3 3
1 1
3 3

Sample Output 1

Over

Sample Input 2
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:
  1. AWS Lambda Interview Questions for Developers
  2. Python coding challenges for interviews
  3. Sequence of Differences in Python
  4. Spaced Out?
  5. Find extra long factorials in python
  6. Find if credit card number is valid or not
  7. Count the number of open lockers in school
See all articles in Python Coding Problems
Top articles in this category:
  1. Python coding challenges for interviews
  2. Connect to Cassandra with Python 3.x and get Pandas Dataframe
  3. Top 100 interview questions on Data Science & Machine Learning
  4. Creating custom Keras callbacks in python
  5. Flask Interview Questions
  6. Connect to Postgresql with Python 3.x and get Pandas Dataframe
  7. Connect to MySQL with Python 3.x and get Pandas Dataframe

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.