From 618c14a3e024c3be1aa6cfd7803bd5613ea578fd Mon Sep 17 00:00:00 2001 From: Pablo Osorio Lopez Date: Wed, 16 Sep 2020 11:44:26 -0500 Subject: [PATCH 1/4] Corrected name and include static types - The name of the file is now compliant with python naming conventions - Add static type as stated in contributing guidelines --- searches/simple-binary-search.py | 26 ------------------ searches/simple_binary_search.py | 45 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 26 deletions(-) delete mode 100644 searches/simple-binary-search.py create mode 100644 searches/simple_binary_search.py diff --git a/searches/simple-binary-search.py b/searches/simple-binary-search.py deleted file mode 100644 index 80e43ea346b2..000000000000 --- a/searches/simple-binary-search.py +++ /dev/null @@ -1,26 +0,0 @@ -# A binary search implementation to test if a number is in a list of elements - - -def binary_search(a_list, item): - """ - >>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] - >>> print(binary_search(test_list, 3)) - False - >>> print(binary_search(test_list, 13)) - True - """ - if len(a_list) == 0: - return False - midpoint = len(a_list) // 2 - if a_list[midpoint] == item: - return True - if item < a_list[midpoint]: - return binary_search(a_list[:midpoint], item) - else: - return binary_search(a_list[midpoint + 1 :], item) - - -if __name__ == "__main__": - import doctest - - doctest.testmod() diff --git a/searches/simple_binary_search.py b/searches/simple_binary_search.py new file mode 100644 index 000000000000..4821e8303c22 --- /dev/null +++ b/searches/simple_binary_search.py @@ -0,0 +1,45 @@ +""" +Pure Python implementation of the binary search algorithm. + +For doctests run following command: +python -m doctest -v simple_binary_search.py +or +python3 -m doctest -v simple_binary_search.py + +For manual testing run: +python simple_binary_search.py +""" +from typing import List + + +def binary_search(a_list: List[int], item: int) -> bool: + """ + Pure python implementation of a binary search of a number is in a list. + + >>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] + >>> print(binary_search(test_list, 3)) + False + >>> print(binary_search(test_list, 13)) + True + """ + if len(a_list) == 0: + return False + midpoint = len(a_list) // 2 + if a_list[midpoint] == item: + return True + if item < a_list[midpoint]: + return binary_search(a_list[:midpoint], item) + else: + return binary_search(a_list[midpoint + 1:], item) + + +if __name__ == "__main__": + user_input: str = input("Enter numbers separated by comma:\n").strip() + sequence: List[int] = [int(item.strip()) for item in user_input.split(",")] + target_str = input("Enter the number to be found in the list:\n").strip() + target: int = int(target_str) + result = binary_search(sequence, target) + if result: + print(f"{target} was found in {sequence}") + else: + print(f"{target} was not found in {sequence}") From 0801878fb23296fb21a085868ab1aac35fdfda24 Mon Sep 17 00:00:00 2001 From: poloso Date: Wed, 16 Sep 2020 17:46:21 -0500 Subject: [PATCH 2/4] Apply suggestions from code review - Delete documentation line to run doctests - Delete type hints for variables that comes from functions Co-authored-by: Christian Clauss --- searches/simple_binary_search.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/searches/simple_binary_search.py b/searches/simple_binary_search.py index 4821e8303c22..e75780eae8f0 100644 --- a/searches/simple_binary_search.py +++ b/searches/simple_binary_search.py @@ -2,19 +2,16 @@ Pure Python implementation of the binary search algorithm. For doctests run following command: -python -m doctest -v simple_binary_search.py -or python3 -m doctest -v simple_binary_search.py For manual testing run: -python simple_binary_search.py +python3 simple_binary_search.py """ from typing import List def binary_search(a_list: List[int], item: int) -> bool: """ - Pure python implementation of a binary search of a number is in a list. >>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] >>> print(binary_search(test_list, 3)) @@ -34,10 +31,10 @@ def binary_search(a_list: List[int], item: int) -> bool: if __name__ == "__main__": - user_input: str = input("Enter numbers separated by comma:\n").strip() - sequence: List[int] = [int(item.strip()) for item in user_input.split(",")] + user_input = input("Enter numbers separated by comma:\n").strip() + sequence = [int(item.strip()) for item in user_input.split(",")] target_str = input("Enter the number to be found in the list:\n").strip() - target: int = int(target_str) + target = int(target_str) result = binary_search(sequence, target) if result: print(f"{target} was found in {sequence}") From 5cf3e510e37ecf50ae70073f54ad87eeb78ed547 Mon Sep 17 00:00:00 2001 From: Pablo Osorio Lopez Date: Wed, 16 Sep 2020 18:06:09 -0500 Subject: [PATCH 3/4] Add edge cases tests. --- searches/simple_binary_search.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/searches/simple_binary_search.py b/searches/simple_binary_search.py index e75780eae8f0..f13699b4115e 100644 --- a/searches/simple_binary_search.py +++ b/searches/simple_binary_search.py @@ -18,6 +18,22 @@ def binary_search(a_list: List[int], item: int) -> bool: False >>> print(binary_search(test_list, 13)) True + >>> print(binary_search([4, 4, 5, 6, 7], 4)) + True + >>> print(binary_search([4, 4, 5, 6, 7], -10)) + False + >>> print(binary_search([-18, 2], -18)) + True + >>> print(binary_search([5], 5)) + True + >>> print(binary_search(['a', 'c', 'd'], 'c')) + True + >>> print(binary_search(['a', 'c', 'd'], 'f')) + False + >>> print(binary_search([], 1)) + False + >>> print(binary_search([.1, .4 , -.1], .1)) + True """ if len(a_list) == 0: return False From 5db1ba0858b17d22f2318c4dd650818e90026dec Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 17 Sep 2020 09:34:43 +0200 Subject: [PATCH 4/4] print(f"{target} was {not_str}found in {sequence}") --- searches/simple_binary_search.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/searches/simple_binary_search.py b/searches/simple_binary_search.py index f13699b4115e..1d898e2d9ee0 100644 --- a/searches/simple_binary_search.py +++ b/searches/simple_binary_search.py @@ -1,5 +1,5 @@ """ -Pure Python implementation of the binary search algorithm. +Pure Python implementation of a binary search algorithm. For doctests run following command: python3 -m doctest -v simple_binary_search.py @@ -12,7 +12,6 @@ def binary_search(a_list: List[int], item: int) -> bool: """ - >>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] >>> print(binary_search(test_list, 3)) False @@ -49,10 +48,6 @@ def binary_search(a_list: List[int], item: int) -> bool: if __name__ == "__main__": user_input = input("Enter numbers separated by comma:\n").strip() sequence = [int(item.strip()) for item in user_input.split(",")] - target_str = input("Enter the number to be found in the list:\n").strip() - target = int(target_str) - result = binary_search(sequence, target) - if result: - print(f"{target} was found in {sequence}") - else: - print(f"{target} was not found in {sequence}") + target = int(input("Enter the number to be found in the list:\n").strip()) + not_str = "" if binary_search(sequence, target) else "not " + print(f"{target} was {not_str}found in {sequence}")