-
Notifications
You must be signed in to change notification settings - Fork 0
/
change.py
76 lines (61 loc) · 2.28 KB
/
change.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
"""
Change
Write a Python function change(money)
that converts a given amount of money (≥ 0)
into several Euro coins.
Minimize the number of coins given.
The return value should be a dictionary specifying
how many coins of each type should be given to the customer.
The keys of the dictionary should correspond to each coin:
from 0.01 (1 cent) all the way up to 2.00 (2 euros).
Using if-else is forbidden.
Example:
Input: 5
Output: {2.0: 2, 1.0: 1, 0.5: 0, 0.2: 0, 0.1: 0, 0.05: 0, 0.02: 0, 0.01: 0}
@author: Luísa Maria
"""
def maior_ou_igual_que_0(money, coin):
return money >= coin
def change(money):
count2euros = 0
count1euro = 0
count50cents = 0
count20cents = 0
count10cents = 0
count5cents = 0
count2cents = 0
count1cent = 0
result = {2.0: 0, 1.0: 0, 0.5: 0, 0.2: 0, 0.1: 0, 0.05: 0, 0.02: 0, 0.01: 0}
while(maior_ou_igual_que_0(money, 2.0)):
count2euros = count2euros + 1
money = round((money - 2.0),2)
while(maior_ou_igual_que_0(money, 1.0)):
count1euro = count1euro + 1
money = round((money - 1.0),2)
while(maior_ou_igual_que_0(money, 0.5)):
count50cents = count50cents + 1
money = round((money - 0.5),2)
while(maior_ou_igual_que_0(money, 0.2)):
count20cents = count20cents + 1
money = round((money - 0.2),2)
while(maior_ou_igual_que_0(money, 0.1)):
count10cents = count10cents + 1
money = round((money - 0.1),2)
while(maior_ou_igual_que_0(money, 0.05)):
count5cents = count5cents + 1
money = round((money - 0.05),2)
while(maior_ou_igual_que_0(money, 0.02)):
count2cents = count2cents + 1
money = round((money - 0.02),2)
while(maior_ou_igual_que_0(money, 0.01)):
count1cent = count1cent + 1
money = round((money - 0.01),2)
result[2.0] = count2euros
result[1.0] = count1euro
result[0.5] = count50cents
result[0.2] = count20cents
result[0.1] = count10cents
result[0.05] = count5cents
result[0.02] = count2cents
result[0.01] = count1cent
return result