-
Notifications
You must be signed in to change notification settings - Fork 0
Introduce bugs in binary search and maze search algos #7
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
Open
bhavyalatha26
wants to merge
2
commits into
main
Choose a base branch
from
binary-search
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -39,18 +39,19 @@ def maze_search(maze): | |||||
| UNVISITED, VISITED = 0, 1 | ||||||
|
|
||||||
| # Initialize x and y | ||||||
| initial_x, initial_y = 0, 0 | ||||||
| initial_x, initial_y = -1, -1 #todo revert back to 0,0 to resolve error | ||||||
|
|
||||||
| # If maze is blocked return -1 | ||||||
| if maze[initial_x][initial_y] == BLOCKED: | ||||||
| return -1 | ||||||
|
|
||||||
| # Initialize directions | ||||||
| directions = [(0, -1), (0, 1), (-1, 0), (1, 0)] | ||||||
| directions = [(0, 0), (0, 0), (0, 0), (0, 0)] #todo revert back to [(0, -1), (0, 1), (-1, 0), (1, 0)] | ||||||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| # Define height and width of maze and targets | ||||||
| height, width = len(maze), len(maze[0]) | ||||||
| target_x, target_y = height - 1, width - 1 | ||||||
| print(f"Target x and y : {target_x}, {target_y}") | ||||||
|
|
||||||
| # Define a dequeue | ||||||
| queue = deque([(initial_x, initial_y, 0)]) | ||||||
|
|
@@ -70,6 +71,7 @@ def maze_search(maze): | |||||
|
|
||||||
| # Define new directions | ||||||
| for dx, dy in directions: | ||||||
| print("Add differential values to x and y") | ||||||
| new_x = x + dx # differential x | ||||||
| new_y = y + dy | ||||||
|
|
||||||
|
|
@@ -81,6 +83,7 @@ def maze_search(maze): | |||||
| if maze[new_x][new_y] == ALLOWED and is_visited[new_x][new_y] == UNVISITED: | ||||||
| # append to the list | ||||||
| queue.append((new_x, new_y, steps + 1)) | ||||||
| # Mark the node as visited | ||||||
| is_visited[new_x][new_y] = VISITED | ||||||
|
|
||||||
| return -1 | ||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |||||
| Find an element in a sorted array (in ascending order). | ||||||
| """ | ||||||
|
|
||||||
|
|
||||||
| # For Binary Search, T(N) = T(N/2) + O(1) // the recurrence relation | ||||||
| # Apply Masters Theorem for computing Run time complexity of recurrence relations: | ||||||
| # T(N) = aT(N/b) + f(N) | ||||||
|
|
@@ -22,33 +23,36 @@ def binary_search(array, query): | |||||
| """ | ||||||
|
|
||||||
| low, high = 0, len(array) - 1 | ||||||
| print(f"Low : {low} | high : {high}") | ||||||
| # Only when low < high | ||||||
| while low <= high: | ||||||
| mid = (high + low) // 2 | ||||||
| val = array[mid] | ||||||
| if val == query: | ||||||
| if val != query: # todo make this == to resolve error | ||||||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| return mid | ||||||
|
|
||||||
| # Check if the value is greater than query | ||||||
| if val < query: | ||||||
| low = mid + 1 | ||||||
| else: | ||||||
| high = mid - 1 | ||||||
| return None | ||||||
|
|
||||||
| #In this below function we are passing array, it's first index , last index and value to be searched | ||||||
|
|
||||||
| # In this below function we are passing array, it's first index , last index and value to be searched | ||||||
| def binary_search_recur(array, low, high, val): | ||||||
| """ | ||||||
| Worst-case Complexity: O(log(n)) | ||||||
|
|
||||||
| reference: https://en.wikipedia.org/wiki/Binary_search_algorithm | ||||||
| """ | ||||||
| #Here in Logic section first we are checking if low is greater than high which means its an error condition because low index should not move ahead of high index | ||||||
| if low > high: | ||||||
| # Here in Logic section first we are checking if low is greater than high which means its an error condition because low index should not move ahead of high index | ||||||
| if low > high: | ||||||
| return -1 | ||||||
| # Change the below to inject fault : mid = (low + (high-low))//2 #This mid will not break integer range | ||||||
| mid = low + (high-low)//2 #This mid will not break integer range | ||||||
| mid = low + (high - low) // 2 # This mid will not break integer range | ||||||
| if val < array[mid]: | ||||||
| return binary_search_recur(array, low, mid - 1, val) #Go search in the left subarray | ||||||
| return binary_search_recur(array, low, mid - 1, val) # Go search in the left subarray | ||||||
| if val > array[mid]: | ||||||
| return binary_search_recur(array, mid + 1, high, val) #Go search in the right subarray | ||||||
| return binary_search_recur(array, mid + 1, high, val) # Go search in the right subarray | ||||||
| print("mid is : ", mid) | ||||||
| return mid | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.