-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab07.py
More file actions
executable file
·187 lines (125 loc) · 6.14 KB
/
lab07.py
File metadata and controls
executable file
·187 lines (125 loc) · 6.14 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Starting point for lab07, CMPSC 8, M18
import pytest
# The following function is copied from lab05---as an example of how to
# check for type, and because it is generally useful
def isList(x):
"""
check whether parameter is a list
"""
return "stub"
# @@@ READ OVER THIS FUNCTION---Then delete this @@@ Comment
# @@@ IT IS PROVIDED AS AN EXAMPLE OF HOW TO FIND THE LARGEST ELEMENT IN A LIST
# @@@ THIS IS WHAT YOU DO IF YOU DON'T HAVE A MAX FUNCTION
def largestInt(listOfInts):
"""
return largest element of a list of ints,
raise ValueError if empty, not a list, or not a list of ints
By "largest", we mean a value that is no smaller than any other
value in the list (There may be more than one instance of that
value--e.g. in [7,3,7], 7 is largest
"""
if type(listOfInts)!=list:
raise ValueError("argument to largestInt should be a list")
if listOfInts==[]:
raise ValueError("argument to largestInt may not be empty")
# Now we know there is at least one item in the list.
# We make an initial assumption that this item will be the largest.
# We then check every other item in the list--each gets a chance to
# knock the maxSoFar out of the winner's circle
maxSoFar = listOfInts[0] # the one in position zero is best guess so far
# Note: we have to start from 0 because we need to check the type
# of element[0] to see if it is an int. Otherwise, we could start from 1
for i in range(0,len(listOfInts)): # all indexes in the list
if type(listOfInts[i])!=int: # make sure it is an int
raise ValueError("element " + str(i) + " isn't an int")
if listOfInts[i] > maxSoFar: # compare the new item with maxSoFar
maxSoFar = listOfInts[i] # we have a new candidate
# for the title of "largest int"
# Now we've gone through the entire list. Every other item on the list
# has had a chance to defeat maxSoFar as the largest int. So the one
# left in maxSoFar must be the largest one.
return maxSoFar
# @@@ READ OVER THIS FUNCTION---Then delete this @@@ Comment
# @@@ IT IS PROVIDED AS AN EXAMPLE OF HOW TO FIND THE INDEX of the largest
# @@@ element in a list.
def indexOfLargestInt(listOfInts):
"""
return index of largest element of a list of ints
raise ValueError if non empty, not a list, or not a list of ints
By "largest", we mean a value that is no smaller than any other
value in the list (see comment in definition of largestInt)
By "index", we mean the subscript that will select that item of
the list when placed in []. Since there can be more than one
largest, we'll return the index of the first such value in those
cases, i.e. the one with the lowest index.
"""
if type(listOfInts)!=list:
raise ValueError("argument to indexOfLargestInt should be a list")
if listOfInts==[]:
raise ValueError("argument to indexOfLargestInt may not be empty")
# Now we know there is at least one item in the list.
# We make an initial assumption that this item will be the largest.
# We then check every other item in the list--each gets a chance to
# knock the maxSoFar out of the winner's circle
indexOfMaxSoFar = 0 # the one in position zero is the first candidate
# Note: we have to start from 0 because we need to check the type
# of element[0] to see if it is an int. Otherwise, we could start from 1
for i in range(0,len(listOfInts)): # all indexes in the list
if type(listOfInts[i])!=int: # make sure it is an int
raise ValueError("element " + str(i) + " isn't an int")
if listOfInts[i] > listOfInts[indexOfMaxSoFar]:
# compare the new item with maxSoFar
indexOfMaxSoFar = i # we have a new candidate; store the INDEX
# Now we've gone through the entire list. Every other item on the list
# has had a chance to defeat maxSoFar as the largest int. So the one
# left in maxSoFar must be the largest one.
return indexOfMaxSoFar
# @@@ NOW: complete the function below
# @@@ It will be similar to largestInt
def smallestInt(listOfInts):
"""
return smallest element of a list of ints
raise ValueError if non empty, not a list, or not a list of ints
By "smallest", we mean a value that is no smaller than any
other value in the list
"""
return "stub"
# @@@ NOW: complete the function below
# @@@ It will be similar to indexOfLargestInt
def indexOfSmallestInt(listOfInts):
"""
return index of smallest element of a list of ints
raise ValueError if non empty, not a list, or not a list of ints
"""
return "stub"
# @@@ NOW: complete the function below
# @@@ You'll have to decide which of the models above fits
# @@@ Recall that len(s) returns the length of a string
# @@@ If you have a listOfStrings, then len(listOfStrings[i]))
# @@@ returns the length of the ith string in that list
def longestString(listOfStrings):
"""
return longestString from a list of strings
raise ValueError if non empty, not a list, or not a list of strings
By "longest", we mean a value that is no shorter than any
other value in the list
There may be more than one string that would qualify:
For example in the list ["dog","bear","wolf","cat"] both bear and
wolf are longest strings
In such cases, return the one with the lowest index (in this case, bear)
"""
return "stub"
# @@@ NOW: complete the function below
# @@@ You'll have to decide which of the models above fits
def indexOfShortestString(listOfStrings):
"""
return index of shortest string from a list of strings
raise ValueError if non empty, not a list, or not a list of strings
By "shortest", we mean a value that is no longer than any other
value in the list
There may be more than one string that would qualify,
For example in the list ["dog","bear","wolf","cat"] both dog and cat
are shortest strings
In such cases, return the one with the lowest index (in this case, dog)
"""
return "stub"