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

Robot Problem + Test Files #23

Merged
merged 5 commits into from
Jan 6, 2025
Merged

Robot Problem + Test Files #23

merged 5 commits into from
Jan 6, 2025

Conversation

reunicorn1
Copy link


Adding robot.py file to the solutions directory with its corresponding test

Behavior

There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.

Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.

Files

solutions/robot.py

  • [O] The file name describes the function's behavior
  • [O] There is a module docstring in the function file
  • [O] The test file's name matches the function file name -
    /tests/test_file_name.py
  • [O] There is a module docstring in the tests file

Unit Tests

solutions/tests/test_robot.py

  • [O] The test class has a helpful name in PascalCase
  • [O] The test class has a docstring

@reunicorn1 reunicorn1 linked an issue Dec 30, 2024 that may be closed by this pull request
@reunicorn1
Copy link
Author

robot.py file, failed the py format test, as the test marks the second parameter of the function as unused. This might pass at this stage as the function itself isn't written yet. However, I may need better naming for the parameters. Currently I'm using m and n as the problem description states, but I will be using cols and rows to state that I'm referring to the columns and rows of the grid.

@reunicorn1
Copy link
Author

There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time.Given the two integers m and n, return the number of possible unique paths that the robot can take to reach the bottom-right corner.The test cases are generated so that the answer will be less than or equal to 2 * 109.Parameters:

m: The number of rows in the grid.

n: The number of columns in the grid.

Return: The number of unique paths from the top-left corner to the bottom-right corner.

IMG_3919

To solve this problem I used dynamic programming as an approach. The trick is to divide the problem into smaller problems and then reuse your solutions to solve the bigger ones. I used a helper function and recursion to keep track of the movement process. The answer depends on the basis that there's either one road to take (if x == n -1 or y == m - 1 ) or two roads to take otherwise, you save the answers in a memoization dictionary using the grid current location tuple as a key, and utilize these answers without the need to repeat your steps when necessary.

Test cases created also tried to cover the general cases that might be used with the function. Since memoization is used the efficiency of retrieving answers is quite high with an optimum time complexity.

@reunicorn1 reunicorn1 changed the title adding robot.py file as a problem with its corresponding test Robot Problem + Test Files Jan 4, 2025
@reunicorn1 reunicorn1 linked an issue Jan 4, 2025 that may be closed by this pull request
Copy link

@Safi222 Safi222 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is well-structured, and the logic is easy to follow , The tests are comprehensive.
Great job!

self.assertEqual(robot(20, 15), 818809200)

# Defensive Cases
def test_invalid_row(self):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job on handling edge cases


# use the helper function robot_move to determine the number of paths
memo = {}
return robot_move((0, 0), rows, cols, memo) # (0, 0) is the initial spot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good use of a helper function!

class TestRobot(unittest.TestCase):
"""Test the robot function"""

# Standard Cases
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basic case for Single-Cell Grid 1x1 could be added

(cell[0], cell[1] + 1), rows, cols, memo
)
# we save the answer in the memo for future referance and return it
return memo[cell]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clear Logic

"""
# assert the values of rows and clos and make sure they are ints and
# greater than 1
assert isinstance(rows, int) and rows > 0, "Rows variables has to \
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertion is clear, but here's a suggestion to use ValueError instead of AssertionError to handle argument validation, it's a clearer signal that the provided argument value is incorrect.

corner.

Constraints: The test cases are generated so that the answer will
be less than or equal to 2 * 109.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code should include the author's name, and the creation date!

@Safi222
Copy link

Safi222 commented Jan 6, 2025

Well-structured!
clear logic!

@Safi222 Safi222 merged commit 489fa6f into main Jan 6, 2025
10 checks passed
@Omnia-Agabani Omnia-Agabani added the writing while writing the initial code label Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
writing while writing the initial code
Projects
Development

Successfully merging this pull request may close these issues.

Robot Problem [Creating Files] Robot Problem
4 participants