Repository navigation
Expand file tree
/
Copy pathnum_divisible.py
More file actions
65 lines (44 loc) · 1.85 KB
/
Copy pathnum_divisible.py
File metadata and controls
65 lines (44 loc) · 1.85 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
def num_divisible(lb, ub, p, q):
"""
How many numbers between lb and ub (inclusive) are divisible by p
or divisible by q, but not divisible by both p and q.
"""
### YOUR CODE GOES HERE
# Replace the following line with your code.
# After running your code, variable n should contain the value
# we ask you to compute in this exercise.
n = 0
for i in range(lb, ub + 1):
if i % p == 0 and i % q == 0 or i % p != and i % q != 0:
n += 0
else:
n += 1
### DO NOT MODIFY THE FOLLOWING LINE!
return n
#############################################################
### ###
### Testing code. ###
### !!! DO NOT MODIFY ANY CODE BELOW THIS POINT !!! ###
### ###
#############################################################
import sys
sys.path.append('../')
import test_utils as utils
def do_test_num_divisible(lb, ub, p, q, expected):
recreate_msg = utils.gen_recreate_msg("num_divisible", *(lb, ub, p, q))
actual = num_divisible(lb, ub, p, q)
utils.check_none(actual, recreate_msg)
utils.check_type(actual, expected, recreate_msg)
utils.check_equals(actual, expected, recreate_msg)
def test_num_divisible_1():
do_test_num_divisible(lb=1, ub=20, p=2, q=3, expected=10)
def test_num_divisible_2():
do_test_num_divisible(lb=2, ub=3, p=2, q=3, expected=2)
def test_num_divisible_3():
do_test_num_divisible(lb=12, ub=20, p=2, q=2, expected=0)
def test_num_divisible_4():
do_test_num_divisible(lb=1, ub=25, p=3, q=5, expected=11)
def test_num_divisible_5():
do_test_num_divisible(lb=1, ub=25, p=27, q=5, expected=5)
def test_num_divisible_6():
do_test_num_divisible(lb=1, ub=25, p=27, q=29, expected=0)