Skip to content

Commit a314b9f

Browse files
author
Philip Guo
committed
reshuffling
1 parent e6012cb commit a314b9f

5 files changed

Lines changed: 201 additions & 0 deletions

File tree

‎questions/debug-bsearch.txt‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
Name:
2+
Debug: Binary search
3+
4+
Question:
5+
6+
Change ONE line of code to make the given binary search function work
7+
properly on all tests.
8+
9+
Hint:
10+
What happens when you search for an element that is not in the list?
11+
'bsearch' should return -1.
12+
13+
Solution:
14+
Change the first line of 'helper' to "if low >= hi:"
15+
16+
MaxLineDelta: 1
17+
MaxInstructions: 100
18+
19+
Skeleton:
20+
21+
def bsearch(lst, elt):
22+
return helper(lst, elt, 0, len(lst))
23+
24+
def helper(lst, elt, lo, hi):
25+
if lo > hi:
26+
return -1
27+
mid = (lo+hi)//2
28+
midval = lst[mid]
29+
if midval < elt:
30+
return helper(lst, elt, mid+1, hi)
31+
elif midval > elt:
32+
return helper(lst, elt, lo, mid)
33+
else:
34+
return mid
35+
36+
37+
Test:
38+
haystack = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
39+
needle = 'f'
40+
result = bsearch(haystack, needle)
41+
42+
Expect:
43+
result = 5
44+
45+
Test:
46+
haystack = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
47+
needle = 'a'
48+
result = bsearch(haystack, needle)
49+
50+
Expect:
51+
result = 0
52+
53+
Test:
54+
haystack = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
55+
needle = 'd'
56+
result = bsearch(haystack, needle)
57+
58+
Expect:
59+
result = 3
60+
61+
Test:
62+
haystack = ['b', 'c', 'd', 'e', 'f', 'g', 'h']
63+
needle = 'a'
64+
result = bsearch(haystack, needle)
65+
66+
Expect:
67+
result = -1
68+
69+
Test:
70+
haystack = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
71+
needle = 'x'
72+
result = bsearch(haystack, needle)
73+
74+
Expect:
75+
result = -1
76+
77+
Test:
78+
haystack = ['a', 'b', 'd', 'e', 'f', 'g', 'h']
79+
needle = 'c'
80+
result = bsearch(haystack, needle)
81+
82+
Expect:
83+
result = -1
84+

‎questions/optimize-find-dups.txt‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
Name:
2+
Optimize: Find duplicates
3+
4+
Question:
5+
6+
The given function returns a list of duplicate elements in the input
7+
list. Optimize this function so that it works on any 10-element sorted
8+
list by executing less than 50 instructions.
9+
10+
Hint:
11+
Think about iterating through the list only once.
12+
13+
Solution:
14+
Use a set to check for duplicates.
15+
16+
MaxInstructions: 50
17+
18+
Skeleton:
19+
20+
def findDups(lst):
21+
dups = set()
22+
for i in range(len(lst)):
23+
for j in range(i + 1, len(lst)):
24+
if lst[i] == lst[j]:
25+
dups.add(lst[i])
26+
return dups
27+
28+
29+
// sample solution:
30+
// def findDups(lst):
31+
// seen = set()
32+
// dups = set()
33+
// for e in lst:
34+
// if e in seen:
35+
// dups.add(e)
36+
// seen.add(e)
37+
// return dups
38+
39+
40+
Test:
41+
input = ['a', 'b', 'c', 'a', 'd', 'c', 'e', 'f', 'g', 'f']
42+
result = findDups(input)
43+
44+
Expect:
45+
result = set(['a', 'c', 'f'])
46+
47+
Test:
48+
input = ['a', 'b', 'xxx', 'c', 'd', 'e', 'f', 'g', 'h', 'xxx']
49+
result = findDups(input)
50+
51+
Expect:
52+
result = set(['xxx'])
53+
54+
Test:
55+
input = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
56+
result = findDups(input)
57+
58+
Expect:
59+
result = set()
60+

‎questions/reverse.txt‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Name:
2+
Reverse list
3+
4+
Question:
5+
Write a function to return the reverse of the input list.
6+
7+
Hint:
8+
Think about iterating backwards.
9+
10+
Solution:
11+
Either iterate backwards to build a new list, or use the Python extended
12+
indexing syntax: "return lst[::-1]"
13+
14+
Skeleton:
15+
16+
def reverse(lst):
17+
# write your solution code here
18+
19+
Test:
20+
input = ['a', 'b', 'c', 'd', 'e']
21+
result = reverse(input)
22+
23+
Expect:
24+
result = ['e', 'd', 'c', 'b', 'a']
25+
26+
27+
Test:
28+
input = ['a', 'b', 'c', 'd']
29+
result = reverse(input)
30+
31+
Expect:
32+
result = ['d', 'c', 'b', 'a']
33+
34+
35+
Test:
36+
input = ['a', 'b', 'c']
37+
result = reverse(input)
38+
39+
Expect:
40+
result = ['c', 'b', 'a']
41+
42+
43+
Test:
44+
input = ['a', 'b']
45+
result = reverse(input)
46+
47+
Expect:
48+
result = ['b', 'a']
49+
50+
51+
Test:
52+
input = ['a']
53+
result = reverse(input)
54+
55+
Expect:
56+
result = ['a']
57+

0 commit comments

Comments
 (0)