Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Programming Interview Challenges #30

Closed
wants to merge 10 commits into from
31 changes: 31 additions & 0 deletions programming_interview/easy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Easy Challenge: Snake's Next Position

## Introduction
In the classic snake game, the snake moves in a certain direction until that direction is changed by the player. Your task is to determine the next position of the snake's head given its current position and direction.

## Problem Statement
Write a function `next_position` that takes in two arguments:

1. A tuple representing the current position of the snake's head as `(x, y)`.
2. A string representing the current direction of the snake, which can be one of the following: "UP", "DOWN", "LEFT", "RIGHT".

The function should return a tuple representing the next position of the snake's head after moving one step in the given direction.

## Input-Output Specifications
- Input:
- A tuple `(x, y)` where `0 <= x, y < 100`.
- A string `direction` which is one of "UP", "DOWN", "LEFT", "RIGHT".
- Output:
- A tuple `(new_x, new_y)` representing the next position.

## Example Cases
1. Input: `(5, 5), "UP"`
Output: `(5, 4)`
2. Input: `(5, 5), "DOWN"`
Output: `(5, 6)`
3. Input: `(5, 5), "LEFT"`
Output: `(4, 5)`
4. Input: `(5, 5), "RIGHT"`
Output: `(6, 5)`

**Note**: Write your solution in the `solution.py` file in this directory.
15 changes: 15 additions & 0 deletions programming_interview/easy/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest
from solution import next_position

def test_next_position():
# Test for moving UP
assert next_position((5, 5), "UP") == (5, 4)

# Test for moving DOWN
assert next_position((5, 5), "DOWN") == (5, 6)

# Test for moving LEFT
assert next_position((5, 5), "LEFT") == (4, 5)

# Test for moving RIGHT
assert next_position((5, 5), "RIGHT") == (6, 5)
30 changes: 30 additions & 0 deletions programming_interview/hard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Hard Challenge: Snake Path Finder

## Introduction
In the classic snake game, finding the optimal path to the food without colliding with the boundaries or itself is a challenging task. Your task is to write a function that finds the shortest path for the snake to reach the food without colliding.

## Problem Statement
Write a function `find_path` that takes in three arguments:

1. A list of tuples representing the current segments of the snake, where each tuple is `(x, y)`.
2. A tuple representing the current position of the food as `(food_x, food_y)`.
3. An integer `grid_size` representing the size of the grid (e.g., `grid_size = 10` for a 10x10 grid).

The function should return a list of strings representing the shortest path to the food, where each string can be one of the following: "UP", "DOWN", "LEFT", "RIGHT". If no path is found, return an empty list.

## Input-Output Specifications
- Input:
- A list of tuples `segments` where each tuple is `(x, y)` representing the snake's segments.
- A tuple `(food_x, food_y)` representing the food's position.
- An integer `grid_size` where `1 <= grid_size <= 100`.
- Output:
- A list of strings representing the path to the food or an empty list if no path is found.

## Example Cases
1. Input: `segments = [(5, 5), (5, 6), (5, 7)], food = (5, 4), grid_size = 10`
Output: `["UP"]`

2. Input: `segments = [(5, 5), (5, 6), (5, 7)], food = (0, 0), grid_size = 10`
Output: `[]`

**Note**: Write your solution in the `solution.py` file in this directory.
13 changes: 13 additions & 0 deletions programming_interview/hard/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest
from solution import find_path

def test_find_path():
# Test based on the provided examples
assert find_path([(5, 5), (5, 6), (5, 7)], (5, 4), 10) == ["UP"]
assert find_path([(5, 5), (5, 6), (5, 7)], (0, 0), 10) == []

# Additional test cases (examples)
assert find_path([(5, 5), (5, 6), (5, 7), (6, 7)], (6, 6), 10) == ["RIGHT", "UP"]
assert find_path([(5, 5), (5, 6), (5, 7), (4, 7)], (4, 6), 10) == ["LEFT", "UP"]

# Note: The additional test cases have been uncommented for comprehensive testing.
28 changes: 28 additions & 0 deletions programming_interview/medium/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Medium Challenge: Snake Self-Collision Prediction

## Introduction
In the classic snake game, the snake moves in a certain direction and can collide with itself if it moves into a segment of its body. Your task is to predict if the snake will collide with itself given its current segments and a list of future moves.

## Problem Statement
Write a function `will_collide` that takes in two arguments:

1. A list of tuples representing the current segments of the snake, where each tuple is `(x, y)`.
2. A list of strings representing the future directions the snake will move in, which can be one of the following: "UP", "DOWN", "LEFT", "RIGHT".

The function should return a boolean value: `True` if the snake will collide with itself in the given moves, and `False` otherwise.

## Input-Output Specifications
- Input:
- A list of tuples `segments` where each tuple is `(x, y)` representing the snake's segments.
- A list of strings `directions` where each string is one of "UP", "DOWN", "LEFT", "RIGHT" representing the future moves of the snake.
- Output:
- A boolean value indicating if the snake will collide with itself.

## Example Cases
1. Input: `segments = [(5, 5), (5, 6), (5, 7)], directions = ["UP", "UP", "RIGHT"]`
Output: `True`

2. Input: `segments = [(5, 5), (5, 6), (5, 7)], directions = ["DOWN", "RIGHT", "UP"]`
Output: `False`

**Note**: Write your solution in the `solution.py` file in this directory.
9 changes: 9 additions & 0 deletions programming_interview/medium/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pytest
from solution import will_collide

def test_will_collide():
# Test for collision
assert will_collide([(5, 5), (5, 6), (5, 7)], ["UP", "UP", "RIGHT"]) == True

# Test for no collision
assert will_collide([(5, 5), (5, 6), (5, 7)], ["DOWN", "RIGHT", "UP"]) == False