Pass the ball game: NxN matrix in python

Carvia Tech | May 18, 2019 at 06:57 PM | 6 views | Python Coding Problem


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 Problem:
  1. Write a program to check if the given word is Isogram & Pair isogram in python
  2. Write a program to find if a number is Lychrel Number in Python
  3. Write a program for Hailstone Sequence in Python
  4. Write a python program to find Largest Substring that occurs more than once
  5. Find perfect abundant or deficient factors in python
  6. Count the number of open lockers in school
See all articles in Python Coding Problem
Top articles in this category:
  1. Top 100 interview questions on Data Science & Machine Learning
  2. Introduction to Python 3.6 & Jupyter Notebook
  3. Creating custom Keras callbacks in python
  4. Write a program for Hailstone Sequence in Python
  5. Creating AWS Lambda using python 3.6
  6. Write a program to check if the given word is Isogram & Pair isogram in python
  7. python problem 1: find the runner-up score


Find more on this topic:
Machine Learning image
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:
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Spring Microservices Interviews for Java Developers
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Java Interviews v3.5 by Munish Chandel

This website uses cookies to ensure you get the best experience on our website. more info