From 38f4e5c5150101b932c3fd52689a673e6ac1d729 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Mon, 30 Jan 2023 15:08:33 +0000 Subject: [PATCH] Bug fixes --- 1ano/fp/aula07/coins.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/1ano/fp/aula07/coins.py b/1ano/fp/aula07/coins.py index b2aff83..038dfe2 100755 --- a/1ano/fp/aula07/coins.py +++ b/1ano/fp/aula07/coins.py @@ -32,12 +32,21 @@ def transfer(bag1, amount, 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()) + # Uses a support method not to reuse the coins if they will make the process not possible + return transferProcess(bag1, amount, bag2, COINS) - for coin in COINS: + +def transferProcess(bag1, amount, bag2, coins): + + bagBackup = (bag1.copy(), bag2.copy()) + amountBackup = amount + + firstUsedCoin = None + for coin in coins: while amount >= coin and transfer1coin(bag1, coin, bag2) and amount > 0: amount -= coin + if firstUsedCoin is None: + firstUsedCoin = coin if amount == 0: return True @@ -45,7 +54,10 @@ def transfer(bag1, amount, bag2): 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 + + if len(coins) == 1: + return False + return transferProcess(bag1, amountBackup, bag2, COINS[COINS.index(firstUsedCoin)+1:]) def strbag(bag):