/
lec10prob4.py
56 lines (45 loc) · 1.67 KB
/
lec10prob4.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# lec10prob4.py
# In lecture, we saw a version of linear search that used the fact that a statements
# of elements is sorted in increasing order. Here is the code from lecture:
def search(L, e):
for i in range(len(L)):
if L[i] == e:
return True
if L[i] > e:
return False
return False
# Consider the following code, which is an alternative version of search.
# search3 recursively calls itself
def search3(L, e):
if L[0] == e:
return True
elif L[0] > e:
return False
else:
return search3(L[1:], e)
# Which of the following statements is correct? You may assume that each
# function is tested with a list L whose elements are sorted in increasing
# order. For simplicity, assume L is a list of integers.
# - search and search3 return the same answers
# - search and search3 return the same answers provided L is non-empty
# - search and search3 return the same answers provided L is non-empty and e is in L
# - search and search3 do not return the same answers
# - search and search3 return the same answers for lists of length 0 and 1 only
# Sample list and calls to both search and search3 with different lists
# to match conditions in above possible answers
# Search for a number in list
print(search([2, 5, 7, 10, 15, 27, 35, 44], 35))
print(search3([2, 5, 7, 10, 15, 27, 35, 44], 35))
print
# Search for a number not in list
print(search([2, 5, 7, 10, 15, 27, 35, 44], 3))
print(search3([2, 5, 7, 10, 15, 27, 35, 44], 3))
print
# Search for a number in an empty list
print(search([], 5))
print(search3([], 5))
print
# Search for a number in a single element list
print(search([5], 5))
print(search3([5], 5))
print