Algorithm Python硬币更改:返回语句中的递增列表?

Algorithm Python硬币更改:返回语句中的递增列表?,algorithm,recursion,python-2.6,tail-recursion,coin-change,Algorithm,Recursion,Python 2.6,Tail Recursion,Coin Change,编辑:仍在处理此问题,但仍在取得进展 def recursion_change(available_coins, tender): """ Returns a tuple containing: :an array counting which coins are used to make change, mirroring the input array :the number of coins to make tender. :coi

编辑:仍在处理此问题,但仍在取得进展

def recursion_change(available_coins, tender):
    """
    Returns a tuple containing:
        :an array counting which coins are used to make change, mirroring the input array
        :the number of coins to make tender.

    :coins: List[int]
    :money: int
    :rtype: (List[int], int)
    """
    change_list = [0] * len(available_coins)



    def _helper_recursion_change(change_index, remaining_balance, change_list):
        if remaining_balance == 0:
            return (change_list, sum(change_list))
        elif change_index == -1 or remaining_balance < 0:
            return float('inf')
        else:
            test_a = _helper_recursion_change(change_index-1, remaining_balance, change_list)
            test_b = _helper_recursion_change(_helper_recursion_change(len(available_coins)-1, tender, change_list))

            test_min = min(test_a or test_b)

            if :
                _helper_recursion_change()
            else:
                _helper_recursion_change()
    return 1 + _helper_recursion_change(change_index, remaining_balance-available_coins[change_index], change_list))

print str(recursion_change([1, 5, 10, 25, 50, 100], 72)) # Current Output: 5
# Desired Output: ([2, 0, 2, 0, 1, 0], 5)
def递归变化(可用硬币,投标):
"""
返回包含以下内容的元组:
:一个数组,用于计算用于进行更改的硬币,镜像输入数组
:要投标的硬币数量。
:硬币:列表[int]
:货币:整数
:rtype:(列表[int],int)
"""
更改列表=[0]*len(可用硬币)
定义辅助递归更改(更改索引、剩余余额、更改列表):
如果剩余余额=0:
返回(更改列表,总和(更改列表))
elif变化指数==-1或剩余余额<0:
返回浮点('inf')
其他:
测试a=\u助手\u递归\u更改(更改索引-1,剩余余额,更改列表)
测试b=_helper_recursion_change(_helper_recursion_change(len(可用硬币)-1,投标,变更列表))
测试最小值=最小值(测试a或测试b)
如果:
_helper\u递归\u更改()
其他:
_helper\u递归\u更改()
返回1+\u helper\u recursion\u change(更改索引,剩余余额-可用硬币[更改索引],更改列表))
打印str(递归_变化([1,5,10,25,50,100],72))#当前输出:5
#期望输出:([2,0,2,0,1,0],5)
快速概述:这个硬币兑换算法应该会收到一个可能的兑换选项和投标的列表。它应该递归地输出一个镜像数组和进行投标所需的硬币数量,我认为最好的方法是使用一个元组

例如:

>递归变化([1,2,5,10,25],49)

>>([0,2,0,2,1],5)

工作代码示例:

def递归\u更改(硬币、货币):
"""
返回包含以下内容的元组:
:一个数组,用于计算用于进行更改的硬币,镜像输入数组
:要投标的硬币数量。
:硬币:列表[int]
:货币:整数
:rtype:(列表[int],int)
"""
更改列表=[0]*len(硬币)
定义、帮助、递归、更改(i、k、更改列表):
如果k==0:#基本情况:此(子)问题中的货币精确匹配变化
返回0
elif i==-1或k<0:#基本情况:无法对此子问题进行更改
返回浮点('inf')
else:#否则,通过递归简化:
#至少采取以下措施:
#制造1美分的硬币数量
#制造k-i美分的硬币数量
return min(_helper_recursion_change(i-1,k,change_list),1+_helper_recursion_change(i,k-coins[i],change_list))
返回(_helper_recursion_change(len(coins)-1,money,change_list))
打印str(递归_变化([1,5,10,25,50,100],6))#当前输出:2
#期望输出:([1,1,0,0,0,0,0],2)
特别是这一行:

1+\u helper\u recursion\u change(i,k-coins[i],change\u list))

正如该程序现在所做的那样,捕获我们需要的硬币数量非常容易。我是否必须更改返回值以包含change_list,以便增加它?不干扰递归的最好方法是什么,因为它当前只返回一个简单的整数

用change_list[i]+1替换上面列表中的change_list会给我一个
TypeError:“int”对象不可订阅
或change_list[i]+=1无法运行,因为它是“无效语法”。

您不能在调用中赋值,也不能索引为整数。对。但是,增加索引的正确位置在哪里?如果我把一个change_list[I]+=1放在其他任何地方,它似乎只是增加change_list[0]。
def recursion_change(coins, money):
    """
    Returns a tuple containing:
        :an array counting which coins are used to make change, mirroring the input array
        :the number of coins to make tender.

    :coins: List[int]
    :money: int
    :rtype: (List[int], int)
    """
    change_list = [0] * len(coins)

    def _helper_recursion_change(i, k, change_list):
        if k == 0: # Base case: money in this (sub)problem matches change precisely
            return 0
        elif i == -1 or k < 0: # Base case: change cannot be made for this subproblem
            return float('inf')
        else: # Otherwise, simplify by recursing:
            # Take the minimum of:
                # the number of coins to make i cents
                # the number of coins to make k-i cents

            return min(_helper_recursion_change(i-1, k, change_list), 1 + _helper_recursion_change(i, k-coins[i], change_list))
    return (_helper_recursion_change(len(coins)-1, money, change_list))

print str(recursion_change([1, 5, 10, 25, 50, 100], 6)) # Current Output: 2

# Desired Output: ([1, 1, 0, 0, 0, 0], 2)