Ex06 adicionado
This commit is contained in:
parent
96a28133dd
commit
642b88e74f
|
@ -6,48 +6,72 @@
|
|||
# Face values of coins (in cents):
|
||||
COINS = [200, 100, 50, 20, 10, 5, 2, 1]
|
||||
|
||||
|
||||
def value(bag):
|
||||
"""Return total amount in a bag."""
|
||||
...
|
||||
totalValue = 0
|
||||
for coin in bag:
|
||||
totalValue += bag[coin] * coin
|
||||
return totalValue
|
||||
|
||||
|
||||
def transfer1coin(bag1, c, bag2):
|
||||
"""Try to transfer one coin of value c from bag1 to bag2.
|
||||
If possible, transfer coin and return True, otherwise return False."""
|
||||
...
|
||||
if bag1[c] == 0:
|
||||
return False
|
||||
|
||||
bag1[c] -= 1
|
||||
bag2[c] = 1 if c not in bag2 else bag2[c] + 1
|
||||
|
||||
return True
|
||||
|
||||
|
||||
def transfer(bag1, amount, bag2):
|
||||
"""Try to transfer an amount from bag1 to bag2.
|
||||
If possible, transfer coins and return True,
|
||||
otherwise, return False and leave bags with same values."""
|
||||
|
||||
# Creates a backup of the bags in case the transfer is not possible
|
||||
bagBackup = (bag1.copy(), bag2.copy())
|
||||
|
||||
for coin in COINS:
|
||||
while amount >= coin and transfer1coin(bag1, coin, bag2) and amount > 0:
|
||||
amount -= coin
|
||||
|
||||
if amount == 0:
|
||||
return True
|
||||
if value(bag1) < amount:
|
||||
return False
|
||||
...
|
||||
|
||||
for coin in COINS:
|
||||
bag1[coin] = bagBackup[0][coin] if coin in bagBackup[0] else 0
|
||||
bag2[coin] = bagBackup[1][coin] if coin in bagBackup[1] else 0
|
||||
return False
|
||||
|
||||
|
||||
def strbag(bag):
|
||||
"""Return a string representing the contents of a bag."""
|
||||
# You may want to change this to produce a more user-friendly
|
||||
# representation such as "4x200+3x50+1x5+3x1=958".
|
||||
return str(bag)
|
||||
...
|
||||
string = ""
|
||||
for coin in bag:
|
||||
if bag[coin] != 0:
|
||||
string = f"+{bag[coin]}x{coin}{string}"
|
||||
string = f"{string[1:]}={value(bag)}"
|
||||
return string
|
||||
|
||||
|
||||
def main():
|
||||
# A bag of coins is represented by a dict of {coin: number} items
|
||||
bag1 = {1: 4, 2: 0, 5:1, 10: 0, 20: 5, 50: 4, 100: 2, 200: 1}
|
||||
bag1 = {1: 4, 2: 0, 5: 1, 10: 0, 20: 5, 50: 4, 100: 2, 200: 1}
|
||||
bag2 = {}
|
||||
|
||||
# Test the value function.
|
||||
assert value({}) == 0
|
||||
assert value({1:7, 5:2, 20:4, 100:1}) == 197
|
||||
assert value({1: 7, 5: 2, 20: 4, 100: 1}) == 197
|
||||
|
||||
# Test the strbag function.
|
||||
print( strbag({1:7, 5:2, 20:4, 100:1}) ) # 1x100+4x20+2x5+7x1=197
|
||||
print( strbag({1:7, 5:2, 10:0, 20:4, 100:1}) ) # 1x100+4x20+2x5+7x1=197
|
||||
print(strbag({1: 7, 5: 2, 20: 4, 100: 1})) # 1x100+4x20+2x5+7x1=197
|
||||
print(strbag({1: 7, 5: 2, 10: 0, 20: 4, 100: 1})) # 1x100+4x20+2x5+7x1=197
|
||||
|
||||
print("bag1:", strbag(bag1)) # bag1: 1x200+2x100+4x50+5x20+1x5+4x1=709
|
||||
print("bag2:", strbag(bag2)) # bag2: =0
|
||||
|
@ -74,6 +98,7 @@ def main():
|
|||
|
||||
return
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
|
Loading…
Reference in New Issue