-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem_set_2.py
69 lines (57 loc) · 1.99 KB
/
problem_set_2.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
# Define a procedure, stamps, which takes as its input a positive integer in
# pence and returns the number of 5p, 2p and 1p stamps (p is pence) required
# to make up that value. The return value should be a tuple of three numbers
# (that is, your return statement should be followed by the number of 5p,
# the number of 2p, and the nuber of 1p stamps).
#
# Your answer should use as few total stamps as possible by first using as
# many 5p stamps as possible, then 2 pence stamps and finally 1p stamps as
# needed to make up the total.
#
# (No fair for USians to just say use a "Forever" stamp and be done with it!)
#
def stamps(n):
five = n/5
two = (n-(five*5))/2
one = (n-(two*2)-(five*5))
return (five, two, one)
print stamps(8)
#>>> (1, 1, 1) # one 5p stamp, one 2p stamp and one 1p stamp
print stamps(5)
#>>> (1, 0, 0) # one 5p stamp, no 2p stamps and no 1p stamps
print stamps(29)
#>>> (5, 2, 0) # five 5p stamps, two 2p stamps and no 1p stamps
print stamps(0)
#>>> (0, 0, 0) # no 5p stamps, no 2p stamps and no 1p stamps
#-------------------------------------------------------------
# The range of a set of values is the maximum value minus the minimum
# value. Define a procedure, set_range, which returns the range of three input
# values.
# Hint: the procedure, biggest which you coded in this unit
# might help you with this question. You might also like to find a way to
# code it using some built-in functions.
def bigger(a,b):
if a > b:
return a
elif b > a:
return b
def biggest(a,b,c):
return bigger(a,bigger(b,c))
def smaller(a,b):
if a < b:
return a
elif b < a:
return b
def set_range(a,b,c):
maximum = biggest(a,b,c)
if maximum == a:
minimum = smaller(b,c)
elif maximum == b:
minimum = smaller(a,c)
elif maximum == c:
minimum = smaller(a,b)
return maximum - minimum
print set_range(10, 4, 7)
#>>> 6 # since 10 - 4 = 6
print set_range(1.1, 7.4, 18.7)
#>>> 17.6 # since 18.7 - 1.1 = 17.6