将输入分解为列出的频率
Factoring input to listed frequencies
我正在尝试编写一个函数,用于计算某个值的最低可能硬币找零 return你可以给 0.70 和 0.50 + 0.20.
这是我目前写的代码:
def pay_with_coins( amount ):
amount = float()
numberof200 = 2.00
numberof100 = 1.00
numberof050 = 0.50
numberof020 = 0.20
numberof010 = 0.10
numberof005 = 0.05
numberof002 = 0.02
numberof001 = 0.01
change = []
no200counter = amount.count(numberof200)
no100counter = amount.count(numberof100)
no050counter = amount.count(numberof050)
no020counter = amount.count(numberof020)
no010counter = amount.count(numberof010)
no005counter = amount.count(numberof005)
no002counter = amount.count(numberof002)
no001counter = amount.count(numberof001)
numberofchange = no200counter + no100counter + no050counter + no020counter + no010counter + no005counter + no002counter + no001counter
if no200counter > 0: +1
elif no100counter > 0: +1
elif no050counter > 0: +1
elif no020counter > 0: +1
elif no010counter > 0: +1
elif no005counter > 0: +1
elif no002counter > 0: +1
elif no001counter > 0: +1
change.append(numberofchange)
return list(change)
我在我的代码中尝试用 if 语句做的是检查下一个最大的变化值是否可以计入我们的数量,在我的列表中的索引处添加一个应该 return 最后,当当前值不再能计入我们的数量时,移动到可用的下一个最大变化值(这在我下面给出的示例中得到了更好的说明)。
我 运行 遇到的一个问题是我的控制台显示 'float' 对象没有属性 'count',但我想确保数量是 2dp 浮点数。
我想以 [2.00, 1.00, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01] 格式列出输出值,每个元素的增加取决于其中的数量。因此,没有输入的输出应该是 [0, 0, 0, 0, 0, 0, 0, 0]。
如果我们要找到对上述示例 (0.70) 的更改,我希望我的输出是:
[0, 0, 1, 1, 0, 0, 0, 0]
另一个例子是找到5.18的变化。输出应该是:
[2, 1, 0, 0, 1, 1, 1, 1]
我想列出最终输出的方式有点类似于二进制转换,除了每个'bit'如果需要可以超过1。
如您所见,我对如何编写代码有一个想法,但我只是在为如何实际将其组合在一起而苦苦挣扎。请帮忙?
这段代码应该可以解决您的问题。让我知道结果如何
def pay_with_coins( amount ):
allCoins = [2.00, 1.00, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01]
change = []
for coin in allCoins:
# Find out how many maximum coins can fit into the amount. Ex for amount 5 a max of 2 coins of value 2 can fit
n = (int)(amount / coin)
change.append(n)
# Substract the value of amount for which change is generated.
# Ex - for amount 5, and coin 2, change will be generated and balance left will be
amount = round(amount - (n * coin), 2) # Rounding to 2 decimals
return change
print(pay_with_coins(5.18))
Output - > [2, 1, 0, 0, 1, 1, 1, 1]
我正在尝试编写一个函数,用于计算某个值的最低可能硬币找零 return你可以给 0.70 和 0.50 + 0.20.
这是我目前写的代码:
def pay_with_coins( amount ):
amount = float()
numberof200 = 2.00
numberof100 = 1.00
numberof050 = 0.50
numberof020 = 0.20
numberof010 = 0.10
numberof005 = 0.05
numberof002 = 0.02
numberof001 = 0.01
change = []
no200counter = amount.count(numberof200)
no100counter = amount.count(numberof100)
no050counter = amount.count(numberof050)
no020counter = amount.count(numberof020)
no010counter = amount.count(numberof010)
no005counter = amount.count(numberof005)
no002counter = amount.count(numberof002)
no001counter = amount.count(numberof001)
numberofchange = no200counter + no100counter + no050counter + no020counter + no010counter + no005counter + no002counter + no001counter
if no200counter > 0: +1
elif no100counter > 0: +1
elif no050counter > 0: +1
elif no020counter > 0: +1
elif no010counter > 0: +1
elif no005counter > 0: +1
elif no002counter > 0: +1
elif no001counter > 0: +1
change.append(numberofchange)
return list(change)
我在我的代码中尝试用 if 语句做的是检查下一个最大的变化值是否可以计入我们的数量,在我的列表中的索引处添加一个应该 return 最后,当当前值不再能计入我们的数量时,移动到可用的下一个最大变化值(这在我下面给出的示例中得到了更好的说明)。
我 运行 遇到的一个问题是我的控制台显示 'float' 对象没有属性 'count',但我想确保数量是 2dp 浮点数。
我想以 [2.00, 1.00, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01] 格式列出输出值,每个元素的增加取决于其中的数量。因此,没有输入的输出应该是 [0, 0, 0, 0, 0, 0, 0, 0]。
如果我们要找到对上述示例 (0.70) 的更改,我希望我的输出是:
[0, 0, 1, 1, 0, 0, 0, 0]
另一个例子是找到5.18的变化。输出应该是:
[2, 1, 0, 0, 1, 1, 1, 1]
我想列出最终输出的方式有点类似于二进制转换,除了每个'bit'如果需要可以超过1。
如您所见,我对如何编写代码有一个想法,但我只是在为如何实际将其组合在一起而苦苦挣扎。请帮忙?
这段代码应该可以解决您的问题。让我知道结果如何
def pay_with_coins( amount ):
allCoins = [2.00, 1.00, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01]
change = []
for coin in allCoins:
# Find out how many maximum coins can fit into the amount. Ex for amount 5 a max of 2 coins of value 2 can fit
n = (int)(amount / coin)
change.append(n)
# Substract the value of amount for which change is generated.
# Ex - for amount 5, and coin 2, change will be generated and balance left will be
amount = round(amount - (n * coin), 2) # Rounding to 2 decimals
return change
print(pay_with_coins(5.18))
Output - > [2, 1, 0, 0, 1, 1, 1, 1]