-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab-8.py
244 lines (200 loc) · 5.06 KB
/
lab-8.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# number 1
# template
# def camera_switch_on(lux, temp):
# """(num, num) -> bool
#
# Determine whether to turn on the camera given the light level and
# the temperature
# """
# return 0
light_level = 0.01
freezing_pt = 0
# template
# def camera_switch_on(lux, temp):
# """(num, num) -> bool
#
# Determine whether to turn on the camera given the light level and
# the temperature
# """
# return... lux temp light_level freezing_pt
def camera_switch_on(lux, temp):
"""(num, num) -> bool
Determines whether to turn on the camera given the light
level and the temperature
"""
return (lux < light_level) != (temp > freezing_pt)
#example
camera_switch_on(0, 0)#True
camera_switch_on(-1, 0) #True
camera_switch_on(0.01, 0) #True
camera_switch_on(0.2, 0) #False
camera_switch_on(-1, 1) #False
camera_switch_on(0.2, -2) #False
camera_switch_on(0.2, 2) #True
#stub
#def max_of_2(x, y):
# """ (int, int) -> int
# Accepts 2 integers and returns the value of the
# largest integer
# """
# return 0
#template
#def max_of_2(x, y):
# """ (int, int) -> int
# Accepts 2 integers and returns the value of the
# largest integer
# """
# return ... x y
def max_of_2(x, y):
""" (int, int) -> int
Accepts 2 integers and returns the value of the
largest integer
"""
if (x > y):
return x
else:
return y
#examples
max_of_2(2, 2) #2
max_of_2(2, 1) #2
max_of_2(1, 2) #1
#stub
#def max_of_3(x, y, z):
# """ (int, int, int) -> int
# Accepts 3 integers and returns the value of the
# largest integer
# """
# return 0
#template
#def max_of_3(x, y, z):
# """ (int, int, int) -> int
# Accepts 3 integers and returns the value of the
# largest integer
# """
# return ... x y z
def max_of_3(x, y, z):
""" (int, int, int) -> int
Accepts 3 integers and returns the value of the
largest integer
"""
return max_of_2(max_of_2(x, y), z)
#examples
max_of_3(3, 2, 1) # expect 3
max_of_3(3, 3, 1) # expect 3
max_of_3(3, 2, 3) # expect 3
max_of_3(2, 3, 1) # expect 3
max_of_3(3, 3, 1) # expect 3
max_of_3(2, 3, 3) # expect 3
max_of_3(1, 2, 3) # expect 3
max_of_3(1, 3, 3) # expect 3
max_of_3(3, 3, 3) # expect 3
age_constant = 45
BMI_constant = 22.0
#stub #def is_at_risk(age, BMI):
# """ (num, num) -> str
# Determines if someone is at risk for heart disease,
# given his age and BMI
# """
# return ""
#template
#def is_at_risk(age, BMI):
# """ (num, num) -> str
# Determines if someone is at risk for heart disease,
# given his age and BMI
# """
# return ... age BMI age_constant BMI_constant
def risk_level(age, BMI):
""" (num, num) -> str
Determines if someone is at risk for heart disease,
given his age and BMI
"""
if (BMI < BMI_constant and age < age_constant):
return "Low"
elif (BMI >= BMI_constant and age < age_constant) or (BMI < BMI_constant and age >= age_constant):
return "Medium"
else:
return "High"
#examples
risk_level(44, 21) # expect "low"
risk_level(44, 22) # expect "medium"
risk_level(44, 23) # expect "medium"
risk_level(45, 21) # expect "medium"
risk_level(45, 22) # expect "high"
risk_level(45, 23) # expect "high"
risk_level(46, 21) # expect "medium"
risk_level(46, 22) # expect "high"
risk_level(46, 23) # expect "high"
#stub
#def letter_grade(letter):
# """ (str) -> str
# Returns the next highest letter grade
# """
# return ""
#template
#def letter_grade(letter):
# """ (str) -> str
# Returns the next highest letter grade
# """
# return ... letter
def letter_grade(letter):
""" (str) -> str
Returns the next highest letter grade
"""
if (letter == "A") or (letter == "B"):
return "A"
else:
return "B"
#examples
letter_grade("A") == "A"
letter_grade("B") == "A"
letter_grade("C") == "B"
string_length_constant = 5
#stub
#def length_of_string(string):
# """ (str) -> num
#
# Returns the length of the given string
# """
# return 0
#template
#def length_of_string(string):
# """ (str) -> num
#
# Returns the length of the given string
# """
# return ... string
def length_of_string(string):
""" (str) -> num
Returns the length of the given string
"""
return len(string)
#examples
length_of_string("String") #6
length_of_string("") #0
length_of_string(" ") #1
length_of_string("Yes") #3
#stub
#def length_is_less_than_five(string):
# """ (str) -> bool
#
# Checks if the length of a given string is less
# than 5
# """
# return 0
#template
#def length_is_less_than_five(string):
# """ (str) -> bool
#
# Checks if the length of a given string is less
# than 5
# """
# return ... string string_length_constant
def length_is_less_than_five(string):
""" (str) -> bool
Checks if the length of a given string is less than 5
"""
return length_of_string(string) <= string_length_constant
#examples
length_is_less_than_five("String") #False
length_is_less_than_five("") #True
length_is_less_than_five("Mommy") #True