Skip to content

Commit 8095d33

Browse files
author
Philip Guo
committed
bah
1 parent 0735e90 commit 8095d33

1 file changed

Lines changed: 20 additions & 11 deletions

File tree

‎questions/optimize-search.txt‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ Optimize: Element search
44
Question:
55

66
The following search function finds the index of the first occurrence of
7-
elt in lst (or -1 if elt not found). Optimize this function so that it
8-
processes an 16-element list in less than executed 50 instructions.
7+
elt in a sorted list lst (or -1 if elt not found). Optimize this
8+
function so that it works on a 64-element sorted list using less than 50
9+
instructions.
910

1011
Hint:
1112
Linear search will never work.
@@ -26,26 +27,34 @@ def search(lst, elt):
2627

2728

2829
Test:
29-
haystack = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
30-
needle = 'n'
30+
haystack = range(64)
31+
needle = 63
3132
result = search(haystack, needle)
3233

3334
Expect:
34-
result = 13
35+
result = 63
3536

3637
Test:
37-
haystack = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
38-
needle = 'l'
38+
haystack = range(64)
39+
needle = 60
3940
result = search(haystack, needle)
4041

4142
Expect:
42-
result = 11
43+
result = 60
4344

4445
Test:
45-
haystack = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
46-
needle = 'g'
46+
haystack = range(64)
47+
needle = 32
4748
result = search(haystack, needle)
4849

4950
Expect:
50-
result = 6
51+
result = 32
52+
53+
Test:
54+
haystack = range(64)
55+
needle = 65
56+
result = search(haystack, needle)
57+
58+
Expect:
59+
result = -1
5160

0 commit comments

Comments
 (0)