Skip to content

Commit 0735e90

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

3 files changed

Lines changed: 53 additions & 2 deletions

File tree

‎questions/buggy-mergesort.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Name:
2-
Debugging: Mergesort
2+
Debug: Mergesort
33

44
Question:
55

‎questions/inplace-reverse.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Name:
2-
Debugging: In-place reverse
2+
Debug: In-place reverse
33

44
Question:
55

‎questions/optimize-search.txt‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Name:
2+
Optimize: Element search
3+
4+
Question:
5+
6+
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.
9+
10+
Hint:
11+
Linear search will never work.
12+
13+
Solution:
14+
Implement binary search instead of linear search.
15+
16+
MaxInstructions: 50
17+
18+
Skeleton:
19+
20+
def search(lst, elt):
21+
ind = -1;
22+
for i in range(len(lst)):
23+
if lst[i] == elt:
24+
return i
25+
return -1
26+
27+
28+
Test:
29+
haystack = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
30+
needle = 'n'
31+
result = search(haystack, needle)
32+
33+
Expect:
34+
result = 13
35+
36+
Test:
37+
haystack = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
38+
needle = 'l'
39+
result = search(haystack, needle)
40+
41+
Expect:
42+
result = 11
43+
44+
Test:
45+
haystack = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
46+
needle = 'g'
47+
result = search(haystack, needle)
48+
49+
Expect:
50+
result = 6
51+

0 commit comments

Comments
 (0)