-
Notifications
You must be signed in to change notification settings - Fork 32
/
lec10prob2.py
55 lines (45 loc) · 1.64 KB
/
lec10prob2.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
# lec10prob2.py
# In lecture, we saw a version of linear search that used the fact that a set
# 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.
def search1(L, e):
for i in L:
if i == e:
return True
if i > e:
return False
return False
# 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 positive integers.
#
# - search and search1 return the same answers
# - search and search1 return the same answers provided L is non-empty
# - search and search1 return the same answers provided L is non-empty and e is in L
# - search and search1 do not return the same answers
# - search and search1 return the same answers for lists of length 0 and 1 only
# Sample list and calls to both search and search1 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(search1([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(search1([2, 5, 7, 10, 15, 27, 35, 44], 3))
print
# Search for a number in an empty list
print(search([], 5))
print(search1([], 5))
print
# Search for a number in a single element list
print(search([5], 5))
print(search1([5], 5))
print