Created
May 12, 2019 19:43
-
-
Save plavjanik/317f0ad4e5ad21bbb03cbef25be53318 to your computer and use it in GitHub Desktop.
PySnooper Example with Binary Search
This file contains 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
import pysnooper | |
@pysnooper.snoop() | |
def binary_search(sorted_collection, item): | |
left = 0 | |
right = len(sorted_collection) - 1 | |
while left <= right: | |
midpoint = (left + right) // 2 | |
current_item = sorted_collection[midpoint] | |
if current_item == item: | |
return midpoint | |
else: | |
if item < current_item: | |
right = midpoint - 1 | |
else: | |
left = midpoint + 1 | |
return None | |
if __name__ == "__main__": | |
binary_search([0, 5, 7, 10, 15], 7) |
This file contains 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
Starting var:.. sorted_collection = [0, 5, 7, 10, 15] | |
Starting var:.. item = 7 | |
21:40:27.405259 call 4 def binary_search(sorted_collection, item): | |
21:40:27.405424 line 20 left = 0 | |
New var:....... left = 0 | |
21:40:27.405507 line 21 right = len(sorted_collection) - 1 | |
New var:....... right = 4 | |
21:40:27.405556 line 23 while left <= right: | |
21:40:27.405606 line 24 midpoint = (left + right) // 2 | |
New var:....... midpoint = 2 | |
21:40:27.405680 line 25 current_item = sorted_collection[midpoint] | |
New var:....... current_item = 7 | |
21:40:27.405756 line 26 if current_item == item: | |
21:40:27.405806 line 27 return midpoint | |
21:40:27.405857 return 27 return midpoint | |
Return value:.. 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment