-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesten_beispiele.py
227 lines (176 loc) · 4.34 KB
/
testen_beispiele.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
# Folie 10
def copy(L1, L2):
"""
Assumes L1 and L2 are lists
Mutates L2 to be a copy of L1
"""
while len(L2) > 0: # remove all elements from L2
L2.pop() # remove last element from L2
for e in L1: # append L1's elements to initially empty L2
L2.append(e)
a = [2, 3, 4]
b = [7, 8]
copy(a, b)
print(b)
copy(a, a)
print(a)
# Folie 22
def is_palindrome(x):
"""
Assumes x is a list
Returns True if the list is a palindrom; False otherwise """
temp = x
temp.reverse
if temp == x:
return True
else:
return False
def test(n):
"""
Assumes n is an int > 0
gets n inputs from user
Prints 'Yes' if the sequence of inputs forms a palindrome; 'No' otherwise """
for i in range(n):
result = []
elem = input('Enter element: ')
result.append(elem)
if is_palindrome(result):
print('Yes')
else:
print('No')
test(5)
# Folie 29
a = int(input("a: "))
b = int(input("b: "))
c = a / b
print("Ratio a/b:", c)
print("Further input: ")
a = int(input("a: "))
b = int(input("b: "))
try:
c = a / b
print("Ratio a/b: ", c)
except ZeroDivisionError:
print("Failure - ratio undefined")
print("Further input: ")
# Folie 31
try:
a = int(input("Tell me one number: "))
b = int(input("Tell me another number: "))
print("a/b = ", a / b)
print("a+b= ", a + b)
except ValueError:
print("Could not convert to a number.")
except ZeroDivisionError:
print("Can't divide by zero.")
except:
print("Something went very wrong.")
# Folie 32
while True:
val = input("Enter an integer: ")
try:
val = int(val)
print("The square of", val, "is", val ** 2)
break
except ValueError:
print(val, "is not an integer")
# Folie 33
def read_int():
while True:
val = input("Enter an integer: ")
try:
return int(val)
except ValueError:
print(val, "is not an integer")
print(read_int())
def read_val(val_type, request_msg, error_msg):
while True:
val = input(request_msg + " ")
try:
return val_type(val)
except ValueError:
print(val, error_msg)
print(read_val(int, "Enter an integer:", "is not an integer"))
# Folie 37
def get_ratios(v_1, v_2):
"""
Assumes v_1 and v_2 are equal length lists of numbers
Returns a list containing the meaningful values of v_1[i]/v_2[i]
"""
ratios = []
for index in range(len(v_1)):
try:
ratios.append(v_1[index] / v_2[index])
except ZeroDivisionError:
ratios.append(float("nan")) # nan = Not a Number
except:
raise ValueError("get_ratios called with bad arguments")
return ratios
try:
print(get_ratios([1.0, 2.0, 7.0, 6.0], [1.0, 2.0, 0.0, 3.0]))
print(get_ratios([], []))
print(get_ratios([1.0, 2.0], [3.0]))
except ValueError as msg:
print(msg)
# Folie 38
def factors(number):
if number < 0:
raise ValueError("Number must be >= 0!")
if type(number) != int:
raise TypeError("Number must be an int!")
x = number
factor_list = []
factor = 2
while x > 1:
while x % factor == 0:
factor_list.append(factor)
x /= factor
factor += 1
return factor_list
try:
print(factors(12))
print(factors(25))
print(factors(25.5))
except (ValueError, TypeError) as msg:
print(msg)
# Folie 41
def avg(grades):
assert len(grades) != 0, "no grades data"
return sum(grades) / len(grades)
print(avg([80, 75, 90]))
print(avg([]))
# Folie 43
def factors(number):
x = number
factor_list = []
factor = 2
while x > 1:
while x % factor == 0:
factor_list.append(factor)
x /= factor
factor += 1
return factor_list
print(factors(12))
print(factors(25))
print(factors(25.5))
# Folie 44
def factors(number):
# precondition
assert (type(number) == int) and (number > 0)
x = number
factor_list = []
factor = 2
while x > 1:
while x % factor == 0:
factor_list.append(factor)
x /= factor
factor += 1
# postcondition
product = 1
for i in factor_list:
product *= i
assert product == number
return factor_list
print(factors(12))
print(factors(25))
print(factors(25.5))