-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#15 deterministic_search, scramble_search
- Loading branch information
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters