forked from calistus-igwilo/python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assert.py
86 lines (64 loc) · 2.21 KB
/
assert.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
#######################
# Asert
#######################
"""
Assert the is_italy variable using the assert statement
"""
countries = ['POL', 'ENG', 'GER', 'USA', 'ITA']
is_italy = 'ITA' in countries
assert is_italy, f'ITA expected to be in the list of countries'
"""
The implementation of the max_min_diff() function is given:
def max_min_diff(numbers):
# enter your solution here
return max(numbers) - min(numbers)
Modify the implementation of the max_min_diff() function. By using the
assert statement inside this function, add the ability to check the
length of the numbers arguement before retruning the result. If the
length of the numbers object is 0 raise the AssertionErrorw without
any message. Otherwise return the correct result.
In response, call max_min_diff() function passin an empty list
"""
def max_min_diff(numbers):
assert len(numbers) != 0
return max(numbers) - min(numbers)
max_min_diff([])
"""
The following area() function is given, which returns the area of a
rectangle (no argument validation).
def area(width, height):
return width * height
Assert the following funtion calls:
area(4, 10)
area(5, 6)
with the appropriate values:
40
30
"""
def area(width, height):
return width * height
assert area(4, 10) == 40
assert area(5, 6) == 30
"""
The following area function is given which returns the area of a
rectangle with additional argument validation:
def area(width, height):
'''The function returns the area of the rectangle.'''
if not (isinstance(width, int) and isinstance(height, int)):
raise TypeError('The width and height must be of type int.')
if not (width > 0 and height > 0):
raise ValueError('The width and height must be positive.')
return width * height
Assert the following function call:
area('5', '4')
with value of 20
"""
def area(width, height):
"""The function returns the area of the rectangle."""
if not (isinstance(width, int) and isinstance(height, int)):
raise TypeError('The width and height must be of type int.')
if not (width > 0 and height > 0):
raise ValueError('The width and height must be positive.')
return width * height
assert area('5', '4') == 20
area('5', '4')