From a7fe862af317e8e7bc740a7a0bb2b82cb2c98d84 Mon Sep 17 00:00:00 2001 From: Krzysztof Wojtas Date: Thu, 16 May 2024 23:02:51 +0200 Subject: [PATCH] #15 deterministic_search, scramble_search --- src/book/chapter5/problem2.py | 43 +++++++++++++++++++++++++++++++++ test/test_book/test_chapter5.py | 30 +++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/book/chapter5/problem2.py diff --git a/src/book/chapter5/problem2.py b/src/book/chapter5/problem2.py new file mode 100644 index 0000000..3ba11fb --- /dev/null +++ b/src/book/chapter5/problem2.py @@ -0,0 +1,43 @@ +from typing import Optional + +from book.chapter5.section3 import randomly_permute +from book.data_structures import Array +from util import range_of + + +def deterministic_search(A: Array, n: int, x: int) -> Optional[int]: + """Searches for a value in an array, examining its elements sequentially. + + Implements: + Deterministic-Search + + Args: + A: an Array to search through + n: the number of numbers in array A + x: the value to search for + + Returns: + An index i such that x equals A[i] or None if x does not appear in A. + """ + for i in range_of(1, to=n): + if A[i] == x: + return i + return None + + +def scramble_search(A: Array, n: int, x: int) -> Optional[int]: + """Searches for a value in an array by first permuting it, then examining its elements sequentially. + + Implements: + Scramble-Search + + Args: + A: an Array to search through + n: the number of numbers in array A + x: the value to search for + + Returns: + An index i such that x equals A[i] or None if x does not appear in A. + """ + randomly_permute(A, n) + return deterministic_search(A, n, x) diff --git a/test/test_book/test_chapter5.py b/test/test_book/test_chapter5.py index f76b2ae..2b62cce 100644 --- a/test/test_book/test_chapter5.py +++ b/test/test_book/test_chapter5.py @@ -8,6 +8,8 @@ from hypothesis.strategies import lists from book.chapter5.problem1 import increment +from book.chapter5.problem2 import deterministic_search +from book.chapter5.problem2 import scramble_search from book.chapter5.section1 import hire_assistant from book.chapter5.section3 import permute_by_cycle from book.chapter5.section3 import permute_with_all @@ -160,3 +162,31 @@ def test_online_maximum(self, data): if actual_maximum_position < n: for i in range_of(1, to=k): self.assertGreater(score[actual_maximum_position], score[i]) + + @given(st.data()) + def test_deterministic_search(self, data): + elements = data.draw(lists(integers(min_value=-10, max_value=10), min_size=1, max_size=20)) + x = data.draw(integers(min_value=-10, max_value=10)) + A = create_array(elements) + n = len(elements) + + actual_index = deterministic_search(A, n, x) + + if actual_index: + self.assertEqual(A[actual_index], x) + else: + self.assertNotIn(x, elements) + + @given(st.data()) + def test_scramble_search(self, data): + elements = data.draw(lists(integers(min_value=-10, max_value=10), min_size=1, max_size=20)) + x = data.draw(integers(min_value=-10, max_value=10)) + A = create_array(elements) + n = len(elements) + + actual_index = scramble_search(A, n, x) + + if actual_index: + self.assertEqual(A[actual_index], x) + else: + self.assertNotIn(x, elements)