-
Notifications
You must be signed in to change notification settings - Fork 2
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
Conversation
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. |
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. 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. |
There was a problem hiding this 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): |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clear Logic
solutions/robot.py
Outdated
""" | ||
# 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 \ |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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!
Well-structured! |
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
/tests/test_file_name.py
Unit Tests
solutions/tests/test_robot.py