Skip to content

Commit

Permalink
#15 deterministic_search, scramble_search
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtask committed May 16, 2024
1 parent e907391 commit a7fe862
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/book/chapter5/problem2.py
Original file line number Diff line number Diff line change
@@ -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)
30 changes: 30 additions & 0 deletions test/test_book/test_chapter5.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit a7fe862

Please sign in to comment.