From 010ff844c5503392ed2654c5440a193572e7dd2f Mon Sep 17 00:00:00 2001 From: Pablo Osorio Lopez Date: Mon, 21 Sep 2020 14:38:21 -0500 Subject: [PATCH 01/10] Add type hints, documentation and tests. --- searches/ternary_search.py | 162 ++++++++++++++++++++++++++----------- 1 file changed, 117 insertions(+), 45 deletions(-) diff --git a/searches/ternary_search.py b/searches/ternary_search.py index 6fdee58cf5dc..3880088edad8 100644 --- a/searches/ternary_search.py +++ b/searches/ternary_search.py @@ -6,7 +6,7 @@ Time Complexity : O(log3 N) Space Complexity : O(1) """ -import sys +from typing import List # This is the precision for this function which can be altered. # It is recommended for users to keep this number greater than or equal to 10. @@ -14,44 +14,123 @@ # This is the linear search that will occur after the search space has become smaller. -def lin_search(left, right, A, target): - for i in range(left, right + 1): +def lin_search(left: int, right: int, A: List[int], target: int) -> int: + """Perform linear search in list. Returns -1 if element is not found. + + Parameters + ---------- + left : int + left index bound. + right : int + right index bound. + A : List[int] + List of elements to be searched on + target : int + Element that is searched + + Returns + ------- + int + index of element that is looked for. + + Examples + -------- + >>> print(lin_search(0, 4, [4, 5, 6, 7], 7)) + 3 + >>> print(lin_search(0, 3, [4, 5, 6, 7], 7)) + -1 + >>> print(lin_search(0, 2, [-18, 2], -18)) + 0 + >>> print(lin_search(0, 1, [5], 5)) + 0 + >>> print(lin_search(0, 3, ['a', 'c', 'd'], 'c')) + 1 + >>> print(lin_search(0, 3, [.1, .4 , -.1], .1)) + 0 + >>> print(lin_search(0, 3, [.1, .4 , -.1], -.1)) + 2 + """ + for i in range(left, right): if A[i] == target: return i - - -# This is the iterative method of the ternary search algorithm. -def ite_ternary_search(A, target): + return -1 + + +def ite_ternary_search(A: List[int], target: int) -> int: + """Iterative method of the ternary search algorithm. + >>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] + >>> print(ite_ternary_search(test_list, 3)) + -1 + >>> print(ite_ternary_search(test_list, 13)) + 4 + >>> print(ite_ternary_search([4, 5, 6, 7], 4)) + 0 + >>> print(ite_ternary_search([4, 5, 6, 7], -10)) + -1 + >>> print(ite_ternary_search([-18, 2], -18)) + 0 + >>> print(ite_ternary_search([5], 5)) + 0 + >>> print(ite_ternary_search(['a', 'c', 'd'], 'c')) + 1 + >>> print(ite_ternary_search(['a', 'c', 'd'], 'f')) + -1 + >>> print(ite_ternary_search([], 1)) + -1 + >>> print(ite_ternary_search([.1, .4 , -.1], .1)) + 0 + """ left = 0 - right = len(A) - 1 - while True: - if left < right: - - if right - left < precision: - return lin_search(left, right, A, target) + right = len(A) + while left <= right: + if right - left < precision: + return lin_search(left, right, A, target) - oneThird = (left + right) / 3 + 1 - twoThird = 2 * (left + right) / 3 + 1 + oneThird = (left + right) / 3 + 1 + twoThird = 2 * (left + right) / 3 + 1 - if A[oneThird] == target: - return oneThird - elif A[twoThird] == target: - return twoThird + if A[oneThird] == target: + return oneThird + elif A[twoThird] == target: + return twoThird - elif target < A[oneThird]: - right = oneThird - 1 - elif A[twoThird] < target: - left = twoThird + 1 + elif target < A[oneThird]: + right = oneThird - 1 + elif A[twoThird] < target: + left = twoThird + 1 - else: - left = oneThird + 1 - right = twoThird - 1 else: - return None - - -# This is the recursive method of the ternary search algorithm. -def rec_ternary_search(left, right, A, target): + left = oneThird + 1 + right = twoThird - 1 + else: + return -1 + + +def rec_ternary_search(left: int, right: int, A: List[int], target: int) -> int: + """Recursive method of the ternary search algorithm. + + >>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] + >>> print(rec_ternary_search(0, len(test_list), test_list, 3)) + -1 + >>> print(rec_ternary_search(4, len(test_list), test_list, 42)) + 8 + >>> print(rec_ternary_search(0, 2, [4, 5, 6, 7], 4)) + 0 + >>> print(rec_ternary_search(0, 3, [4, 5, 6, 7], -10)) + -1 + >>> print(rec_ternary_search(0, 1, [-18, 2], -18)) + 0 + >>> print(rec_ternary_search(0, 1, [5], 5)) + 0 + >>> print(rec_ternary_search(0, 2, ['a', 'c', 'd'], 'c')) + 1 + >>> print(rec_ternary_search(0, 2, ['a', 'c', 'd'], 'f')) + -1 + >>> print(rec_ternary_search(0, 0, [], 1)) + -1 + >>> print(rec_ternary_search(0, 3, [.1, .4 , -.1], .1)) + 0 + """ if left < right: if right - left < precision: @@ -69,35 +148,28 @@ def rec_ternary_search(left, right, A, target): return rec_ternary_search(left, oneThird - 1, A, target) elif A[twoThird] < target: return rec_ternary_search(twoThird + 1, right, A, target) - else: return rec_ternary_search(oneThird + 1, twoThird - 1, A, target) else: - return None + return -1 # This function is to check if the array is sorted. def __assert_sorted(collection): if collection != sorted(collection): - raise ValueError("Collection must be sorted") + raise ValueError("Collection is not sorted.") return True if __name__ == "__main__": - user_input = input("Enter numbers separated by coma:\n").strip() - collection = [int(item) for item in user_input.split(",")] - - try: - __assert_sorted(collection) - except ValueError: - sys.exit("Sequence must be sorted to apply the ternary search") - - target_input = input("Enter a single number to be found in the list:\n") - target = int(target_input) + user_input = input("Enter numbers separated by comma:\n").strip() + collection = [int(item.strip()) for item in user_input.split(",")] + __assert_sorted(collection) + target = int(input("Enter the number to be found in the list:\n").strip()) result1 = ite_ternary_search(collection, target) result2 = rec_ternary_search(0, len(collection) - 1, collection, target) - if result2 is not None: + if result2 != -1: print(f"Iterative search: {target} found at positions: {result1}") print(f"Recursive search: {target} found at positions: {result2}") else: From d953a02ac1759fd2439e3b2cc1cacb3728362024 Mon Sep 17 00:00:00 2001 From: poloso Date: Tue, 22 Sep 2020 09:21:23 -0500 Subject: [PATCH 02/10] Update searches/ternary_search.py Sort collection and remove the assertion logic. Co-authored-by: Christian Clauss --- searches/ternary_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/ternary_search.py b/searches/ternary_search.py index 3880088edad8..c081fecdea5d 100644 --- a/searches/ternary_search.py +++ b/searches/ternary_search.py @@ -163,7 +163,7 @@ def __assert_sorted(collection): if __name__ == "__main__": user_input = input("Enter numbers separated by comma:\n").strip() - collection = [int(item.strip()) for item in user_input.split(",")] + collection = sorted(int(item.strip()) for item in user_input.split(",")) __assert_sorted(collection) target = int(input("Enter the number to be found in the list:\n").strip()) result1 = ite_ternary_search(collection, target) From e37a1dc424a3645b57b4b878334765ef088cf3a3 Mon Sep 17 00:00:00 2001 From: Pablo Osorio Lopez Date: Tue, 22 Sep 2020 09:24:14 -0500 Subject: [PATCH 03/10] Remove assert sorted logic. --- searches/ternary_search.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/searches/ternary_search.py b/searches/ternary_search.py index c081fecdea5d..359f13c9c228 100644 --- a/searches/ternary_search.py +++ b/searches/ternary_search.py @@ -154,17 +154,9 @@ def rec_ternary_search(left: int, right: int, A: List[int], target: int) -> int: return -1 -# This function is to check if the array is sorted. -def __assert_sorted(collection): - if collection != sorted(collection): - raise ValueError("Collection is not sorted.") - return True - - if __name__ == "__main__": user_input = input("Enter numbers separated by comma:\n").strip() collection = sorted(int(item.strip()) for item in user_input.split(",")) - __assert_sorted(collection) target = int(input("Enter the number to be found in the list:\n").strip()) result1 = ite_ternary_search(collection, target) result2 = rec_ternary_search(0, len(collection) - 1, collection, target) From ca04b305677f1cfaaf0c627b0c480d2490f1303d Mon Sep 17 00:00:00 2001 From: Pablo Osorio Lopez Date: Tue, 22 Sep 2020 15:47:58 -0500 Subject: [PATCH 04/10] Add assertion list is ordered. --- searches/ternary_search.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/searches/ternary_search.py b/searches/ternary_search.py index 359f13c9c228..e7190cc2c511 100644 --- a/searches/ternary_search.py +++ b/searches/ternary_search.py @@ -156,11 +156,11 @@ def rec_ternary_search(left: int, right: int, A: List[int], target: int) -> int: if __name__ == "__main__": user_input = input("Enter numbers separated by comma:\n").strip() - collection = sorted(int(item.strip()) for item in user_input.split(",")) + collection = [int(item.strip()) for item in user_input.split(",")] + assert collection == sorted(collection), f"List must be ordered.\n{collection}." target = int(input("Enter the number to be found in the list:\n").strip()) result1 = ite_ternary_search(collection, target) result2 = rec_ternary_search(0, len(collection) - 1, collection, target) - if result2 != -1: print(f"Iterative search: {target} found at positions: {result1}") print(f"Recursive search: {target} found at positions: {result2}") From 618db89fe5d03cfb9dc1b8be8c3bf8347ba075c3 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Thu, 29 Oct 2020 22:08:03 +0000 Subject: [PATCH 05/10] updating DIRECTORY.md --- DIRECTORY.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index ea5e01addeb0..7c695892112a 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -674,6 +674,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_056/sol1.py) * Problem 057 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_057/sol1.py) + * Problem 058 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_058/sol1.py) * Problem 062 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_062/sol1.py) * Problem 063 @@ -699,6 +701,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_080/sol1.py) * Problem 081 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_081/sol1.py) + * Problem 087 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_087/sol1.py) * Problem 091 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_091/sol1.py) * Problem 097 @@ -817,6 +821,7 @@ * [Prefix Function](https://github.com/TheAlgorithms/Python/blob/master/strings/prefix_function.py) * [Rabin Karp](https://github.com/TheAlgorithms/Python/blob/master/strings/rabin_karp.py) * [Remove Duplicate](https://github.com/TheAlgorithms/Python/blob/master/strings/remove_duplicate.py) + * [Reverse Letters](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_letters.py) * [Reverse Words](https://github.com/TheAlgorithms/Python/blob/master/strings/reverse_words.py) * [Split](https://github.com/TheAlgorithms/Python/blob/master/strings/split.py) * [Swap Case](https://github.com/TheAlgorithms/Python/blob/master/strings/swap_case.py) From 17eaff306757e88701961f2d1f0e5de39c2eb969 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 3 Nov 2020 23:54:16 +0000 Subject: [PATCH 06/10] updating DIRECTORY.md --- DIRECTORY.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 0cecae28d51d..14931ff18896 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -205,6 +205,7 @@ * [Heaps Algorithm](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/heaps_algorithm.py) * [Heaps Algorithm Iterative](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/heaps_algorithm_iterative.py) * [Inversions](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/inversions.py) + * [Kth Order Statistic](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/kth_order_statistic.py) * [Max Subarray Sum](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/max_subarray_sum.py) * [Mergesort](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/mergesort.py) * [Power](https://github.com/TheAlgorithms/Python/blob/master/divide_and_conquer/power.py) @@ -389,6 +390,7 @@ * [Chudnovsky Algorithm](https://github.com/TheAlgorithms/Python/blob/master/maths/chudnovsky_algorithm.py) * [Collatz Sequence](https://github.com/TheAlgorithms/Python/blob/master/maths/collatz_sequence.py) * [Combinations](https://github.com/TheAlgorithms/Python/blob/master/maths/combinations.py) + * [Decimal Isolate](https://github.com/TheAlgorithms/Python/blob/master/maths/decimal_isolate.py) * [Entropy](https://github.com/TheAlgorithms/Python/blob/master/maths/entropy.py) * [Eulers Totient](https://github.com/TheAlgorithms/Python/blob/master/maths/eulers_totient.py) * [Explicit Euler](https://github.com/TheAlgorithms/Python/blob/master/maths/explicit_euler.py) @@ -680,6 +682,8 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_062/sol1.py) * Problem 063 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_063/sol1.py) + * Problem 064 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_064/sol1.py) * Problem 065 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_065/sol1.py) * Problem 067 @@ -693,6 +697,7 @@ * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_072/sol2.py) * Problem 074 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_074/sol1.py) + * [Sol2](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_074/sol2.py) * Problem 075 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_075/sol1.py) * Problem 076 @@ -725,12 +730,16 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_174/sol1.py) * Problem 191 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_191/sol1.py) + * Problem 203 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_203/sol1.py) * Problem 206 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_206/sol1.py) * Problem 207 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_207/sol1.py) * Problem 234 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_234/sol1.py) + * Problem 301 + * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_301/sol1.py) * Problem 551 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_551/sol1.py) From 8c94201cd402c0091e60cbd3090a0ac2472245fd Mon Sep 17 00:00:00 2001 From: Pablo Osorio Lopez Date: Tue, 3 Nov 2020 18:57:15 -0500 Subject: [PATCH 07/10] Format with black. --- searches/ternary_search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/searches/ternary_search.py b/searches/ternary_search.py index c4f3a639d5b5..d3ed81483e85 100644 --- a/searches/ternary_search.py +++ b/searches/ternary_search.py @@ -15,6 +15,7 @@ # This is the linear search that will occur after the search space has become smaller. + def lin_search(left: int, right: int, A: List[int], target: int) -> int: """Perform linear search in list. Returns -1 if element is not found. @@ -155,7 +156,6 @@ def rec_ternary_search(left: int, right: int, A: List[int], target: int) -> int: return -1 - if __name__ == "__main__": user_input = input("Enter numbers separated by comma:\n").strip() collection = [int(item.strip()) for item in user_input.split(",")] From bb58c558c332f571f97a48c53963ef4768f15efe Mon Sep 17 00:00:00 2001 From: Pablo Osorio Lopez Date: Thu, 12 Nov 2020 10:57:25 -0500 Subject: [PATCH 08/10] Change names of variables to descriptive names --- searches/ternary_search.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/searches/ternary_search.py b/searches/ternary_search.py index d3ed81483e85..22a7d3a6ac77 100644 --- a/searches/ternary_search.py +++ b/searches/ternary_search.py @@ -16,7 +16,7 @@ # This is the linear search that will occur after the search space has become smaller. -def lin_search(left: int, right: int, A: List[int], target: int) -> int: +def lin_search(left: int, right: int, array: List[int], target: int) -> int: """Perform linear search in list. Returns -1 if element is not found. Parameters @@ -25,7 +25,7 @@ def lin_search(left: int, right: int, A: List[int], target: int) -> int: left index bound. right : int right index bound. - A : List[int] + array : List[int] List of elements to be searched on target : int Element that is searched @@ -53,12 +53,12 @@ def lin_search(left: int, right: int, A: List[int], target: int) -> int: 2 """ for i in range(left, right): - if A[i] == target: + if array[i] == target: return i return -1 -def ite_ternary_search(A: List[int], target: int) -> int: +def ite_ternary_search(array: List[int], target: int) -> int: """Iterative method of the ternary search algorithm. >>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] >>> print(ite_ternary_search(test_list, 3)) @@ -84,22 +84,22 @@ def ite_ternary_search(A: List[int], target: int) -> int: """ left = 0 - right = len(A) + right = len(array) while left <= right: if right - left < precision: - return lin_search(left, right, A, target) + return lin_search(left, right, array, target) oneThird = (left + right) / 3 + 1 twoThird = 2 * (left + right) / 3 + 1 - if A[oneThird] == target: + if array[oneThird] == target: return oneThird - elif A[twoThird] == target: + elif array[twoThird] == target: return twoThird - elif target < A[oneThird]: + elif target < array[oneThird]: right = oneThird - 1 - elif A[twoThird] < target: + elif array[twoThird] < target: left = twoThird + 1 else: @@ -110,7 +110,7 @@ def ite_ternary_search(A: List[int], target: int) -> int: return -1 -def rec_ternary_search(left: int, right: int, A: List[int], target: int) -> int: +def rec_ternary_search(left: int, right: int, array: List[int], target: int) -> int: """Recursive method of the ternary search algorithm. >>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] @@ -137,21 +137,21 @@ def rec_ternary_search(left: int, right: int, A: List[int], target: int) -> int: """ if left < right: if right - left < precision: - return lin_search(left, right, A, target) + return lin_search(left, right, array, target) oneThird = (left + right) / 3 + 1 twoThird = 2 * (left + right) / 3 + 1 - if A[oneThird] == target: + if array[oneThird] == target: return oneThird - elif A[twoThird] == target: + elif array[twoThird] == target: return twoThird - elif target < A[oneThird]: - return rec_ternary_search(left, oneThird - 1, A, target) - elif A[twoThird] < target: - return rec_ternary_search(twoThird + 1, right, A, target) + elif target < array[oneThird]: + return rec_ternary_search(left, oneThird - 1, array, target) + elif array[twoThird] < target: + return rec_ternary_search(twoThird + 1, right, array, target) else: - return rec_ternary_search(oneThird + 1, twoThird - 1, A, target) + return rec_ternary_search(oneThird + 1, twoThird - 1, array, target) else: return -1 From a3df50ac84699716eb0a5e21700d3762dc1e7d2d Mon Sep 17 00:00:00 2001 From: Pablo Osorio Lopez Date: Thu, 12 Nov 2020 11:00:27 -0500 Subject: [PATCH 09/10] Remove print in doctests --- searches/ternary_search.py | 54 +++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/searches/ternary_search.py b/searches/ternary_search.py index 22a7d3a6ac77..1e1aabd9e42a 100644 --- a/searches/ternary_search.py +++ b/searches/ternary_search.py @@ -37,19 +37,19 @@ def lin_search(left: int, right: int, array: List[int], target: int) -> int: Examples -------- - >>> print(lin_search(0, 4, [4, 5, 6, 7], 7)) + >>> lin_search(0, 4, [4, 5, 6, 7], 7) 3 - >>> print(lin_search(0, 3, [4, 5, 6, 7], 7)) + >>> lin_search(0, 3, [4, 5, 6, 7], 7) -1 - >>> print(lin_search(0, 2, [-18, 2], -18)) + >>> lin_search(0, 2, [-18, 2], -18) 0 - >>> print(lin_search(0, 1, [5], 5)) + >>> lin_search(0, 1, [5], 5) 0 - >>> print(lin_search(0, 3, ['a', 'c', 'd'], 'c')) + >>> lin_search(0, 3, ['a', 'c', 'd'], 'c') 1 - >>> print(lin_search(0, 3, [.1, .4 , -.1], .1)) + >>> lin_search(0, 3, [.1, .4 , -.1], .1) 0 - >>> print(lin_search(0, 3, [.1, .4 , -.1], -.1)) + >>> lin_search(0, 3, [.1, .4 , -.1], -.1) 2 """ for i in range(left, right): @@ -61,25 +61,25 @@ def lin_search(left: int, right: int, array: List[int], target: int) -> int: def ite_ternary_search(array: List[int], target: int) -> int: """Iterative method of the ternary search algorithm. >>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] - >>> print(ite_ternary_search(test_list, 3)) + >>> ite_ternary_search(test_list, 3) -1 - >>> print(ite_ternary_search(test_list, 13)) + >>> ite_ternary_search(test_list, 13) 4 - >>> print(ite_ternary_search([4, 5, 6, 7], 4)) + >>> ite_ternary_search([4, 5, 6, 7], 4) 0 - >>> print(ite_ternary_search([4, 5, 6, 7], -10)) + >>> ite_ternary_search([4, 5, 6, 7], -10) -1 - >>> print(ite_ternary_search([-18, 2], -18)) + >>> ite_ternary_search([-18, 2], -18) 0 - >>> print(ite_ternary_search([5], 5)) + >>> ite_ternary_search([5], 5) 0 - >>> print(ite_ternary_search(['a', 'c', 'd'], 'c')) + >>> ite_ternary_search(['a', 'c', 'd'], 'c') 1 - >>> print(ite_ternary_search(['a', 'c', 'd'], 'f')) + >>> ite_ternary_search(['a', 'c', 'd'], 'f') -1 - >>> print(ite_ternary_search([], 1)) + >>> ite_ternary_search([], 1) -1 - >>> print(ite_ternary_search([.1, .4 , -.1], .1)) + >>> ite_ternary_search([.1, .4 , -.1], .1) 0 """ @@ -114,25 +114,25 @@ def rec_ternary_search(left: int, right: int, array: List[int], target: int) -> """Recursive method of the ternary search algorithm. >>> test_list = [0, 1, 2, 8, 13, 17, 19, 32, 42] - >>> print(rec_ternary_search(0, len(test_list), test_list, 3)) + >>> rec_ternary_search(0, len(test_list), test_list, 3) -1 - >>> print(rec_ternary_search(4, len(test_list), test_list, 42)) + >>> rec_ternary_search(4, len(test_list), test_list, 42) 8 - >>> print(rec_ternary_search(0, 2, [4, 5, 6, 7], 4)) + >>> rec_ternary_search(0, 2, [4, 5, 6, 7], 4) 0 - >>> print(rec_ternary_search(0, 3, [4, 5, 6, 7], -10)) + >>> rec_ternary_search(0, 3, [4, 5, 6, 7], -10) -1 - >>> print(rec_ternary_search(0, 1, [-18, 2], -18)) + >>> rec_ternary_search(0, 1, [-18, 2], -18) 0 - >>> print(rec_ternary_search(0, 1, [5], 5)) + >>> rec_ternary_search(0, 1, [5], 5) 0 - >>> print(rec_ternary_search(0, 2, ['a', 'c', 'd'], 'c')) + >>> rec_ternary_search(0, 2, ['a', 'c', 'd'], 'c') 1 - >>> print(rec_ternary_search(0, 2, ['a', 'c', 'd'], 'f')) + >>> rec_ternary_search(0, 2, ['a', 'c', 'd'], 'f') -1 - >>> print(rec_ternary_search(0, 0, [], 1)) + >>> rec_ternary_search(0, 0, [], 1) -1 - >>> print(rec_ternary_search(0, 3, [.1, .4 , -.1], .1)) + >>> rec_ternary_search(0, 3, [.1, .4 , -.1], .1) 0 """ if left < right: From efdc76fe217d00f5142c6bb5923a40048d4bc037 Mon Sep 17 00:00:00 2001 From: Pablo Osorio Lopez Date: Thu, 12 Nov 2020 11:03:33 -0500 Subject: [PATCH 10/10] Fix variables to snake_case notation. --- searches/ternary_search.py | 50 +++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/searches/ternary_search.py b/searches/ternary_search.py index 1e1aabd9e42a..9422a4ccb966 100644 --- a/searches/ternary_search.py +++ b/searches/ternary_search.py @@ -89,23 +89,23 @@ def ite_ternary_search(array: List[int], target: int) -> int: if right - left < precision: return lin_search(left, right, array, target) - oneThird = (left + right) / 3 + 1 - twoThird = 2 * (left + right) / 3 + 1 + one_third = (left + right) / 3 + 1 + two_third = 2 * (left + right) / 3 + 1 - if array[oneThird] == target: - return oneThird - elif array[twoThird] == target: - return twoThird + if array[one_third] == target: + return one_third + elif array[two_third] == target: + return two_third - elif target < array[oneThird]: - right = oneThird - 1 - elif array[twoThird] < target: - left = twoThird + 1 + elif target < array[one_third]: + right = one_third - 1 + elif array[two_third] < target: + left = two_third + 1 else: - left = oneThird + 1 - right = twoThird - 1 + left = one_third + 1 + right = two_third - 1 else: return -1 @@ -138,20 +138,20 @@ def rec_ternary_search(left: int, right: int, array: List[int], target: int) -> if left < right: if right - left < precision: return lin_search(left, right, array, target) - oneThird = (left + right) / 3 + 1 - twoThird = 2 * (left + right) / 3 + 1 - - if array[oneThird] == target: - return oneThird - elif array[twoThird] == target: - return twoThird - - elif target < array[oneThird]: - return rec_ternary_search(left, oneThird - 1, array, target) - elif array[twoThird] < target: - return rec_ternary_search(twoThird + 1, right, array, target) + one_third = (left + right) / 3 + 1 + two_third = 2 * (left + right) / 3 + 1 + + if array[one_third] == target: + return one_third + elif array[two_third] == target: + return two_third + + elif target < array[one_third]: + return rec_ternary_search(left, one_third - 1, array, target) + elif array[two_third] < target: + return rec_ternary_search(two_third + 1, right, array, target) else: - return rec_ternary_search(oneThird + 1, twoThird - 1, array, target) + return rec_ternary_search(one_third + 1, two_third - 1, array, target) else: return -1