Repository navigation
Expand file tree
/
Copy pathlist1.py
More file actions
105 lines (80 loc) · 2.93 KB
/
Copy pathlist1.py
File metadata and controls
105 lines (80 loc) · 2.93 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
# List 1
# Author: 292680
# Ex 1
def divisible(numbers, divisor):
"""Return the numbers from the list that are divisible by divisor.
Args:
numbers (list): List of integers.
divisor (int): Integer by which the numbers are divided.
Returns:
list: New list with the divisible numbers by divisor.
Raises:
TypeError: If numbers is not a list of integers or divisor is not an integer.
ValueError: If divisor == 0.
AssertionError: If a postcondition is not met.
Preconditions:
numbers is a list of integers, divisor is an integer other than 0.
Postconditions:
Every number in the result is divisible by divisor and comes from numbers. The result is no longer than len(numbers).
"""
# checking preconditions:
# - 'numbers' must be a list
# - all elements of numbers must be an integers
# - divisor must be an integer
# - divisor cannot be 0
if not isinstance(numbers, list):
raise TypeError("numbers must be a list")
for number in numbers:
if not isinstance(number, int):
raise TypeError("all elements of numbers must be integers")
if not isinstance(divisor, int):
raise TypeError("divisor must be an integer")
if divisor == 0:
raise ValueError("divisor cannot be 0")
# Main function
result = []
for number in numbers:
if number % divisor == 0:
result.append(number)
# checking postconditions
for number in result:
assert number % divisor == 0, "number in result is not divisible"
assert number in numbers, "number in result is not from numbers"
assert len(result) <= len(numbers), "result is longer than numbers"
return result
# Ex 2
def common(x, y):
"""Return the intersection of two multisets.
Args:
x (list): First multiset.
y (list): Second multiset.
Returns:
list: New list with the intersection of x and y.
Raises:
TypeError: If x or y is not a list.
AssertionError: If a postcondition is not met.
Preconditions:
x and y are lists.
Postconditions:
Every element appears in the result min(x.count(element),
y.count(element)) times. Lists x and y are not changed.
"""
# checking preconditions:
# x and y must be a list
if not isinstance(x, list):
raise TypeError("x must be a list")
if not isinstance(y, list):
raise TypeError("y must be a list")
result = []
# making copy to not to lose the original data
y_copy = y.copy()
for element in x:
if element in y_copy:
result.append(element)
y_copy.remove(element)
# checking postconditions
for element in x + y:
expected = min(x.count(element), y.count(element))
assert result.count(element) == expected, "wrong number of elements"
assert len(result) <= len(x), "result is longer than x"
return result