From 71bce8969fd05f22f5fdedcefdf270db86c654ed Mon Sep 17 00:00:00 2001 From: praisearts <34782930+praisearts@users.noreply.github.com> Date: Thu, 24 Oct 2019 09:00:13 +0100 Subject: [PATCH 1/2] create simnple binary search #A binary search implementation to test if a number is in a list of elements --- searches/simple-binary-search | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 searches/simple-binary-search diff --git a/searches/simple-binary-search b/searches/simple-binary-search new file mode 100644 index 000000000000..56a486e04ed0 --- /dev/null +++ b/searches/simple-binary-search @@ -0,0 +1,20 @@ +#A binary search implementation +#to test if a number is in a list of elements + +def binary_search(a_list, item): + if len(a_list) == 0: + return False + else: + midpoint = len(a_list) // 2 + + if a_list[midpoint] == item: + return True + else: + if item < a_list[midpoint]: + return binary_search(a_list[:midpoint], item) + else: + return binary_search(a_list[midpoint + 1:], item) + +test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42,] +print(binary_search(test_list, 3)) +print(binary_search(test_list, 13)) From 1c592bd8629fc254b84afb5f7f4a375f7e4d0afd Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 24 Oct 2019 10:19:06 +0200 Subject: [PATCH 2/2] Add .py, format with psf/black, and add doctests --- searches/simple-binary-search | 20 -------------------- searches/simple-binary-search.py | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 20 deletions(-) delete mode 100644 searches/simple-binary-search create mode 100644 searches/simple-binary-search.py diff --git a/searches/simple-binary-search b/searches/simple-binary-search deleted file mode 100644 index 56a486e04ed0..000000000000 --- a/searches/simple-binary-search +++ /dev/null @@ -1,20 +0,0 @@ -#A binary search implementation -#to test if a number is in a list of elements - -def binary_search(a_list, item): - if len(a_list) == 0: - return False - else: - midpoint = len(a_list) // 2 - - if a_list[midpoint] == item: - return True - else: - if item < a_list[midpoint]: - return binary_search(a_list[:midpoint], item) - else: - return binary_search(a_list[midpoint + 1:], item) - -test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42,] -print(binary_search(test_list, 3)) -print(binary_search(test_list, 13)) diff --git a/searches/simple-binary-search.py b/searches/simple-binary-search.py new file mode 100644 index 000000000000..80e43ea346b2 --- /dev/null +++ b/searches/simple-binary-search.py @@ -0,0 +1,26 @@ +# 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()