I wrote some code that found the fastest way of getting the sum of a number by adding numbers that were part of a list together ex: bestSum(3, [800, 1, 3]) would return [3] because that would be the best way of getting 3 (first number) with the numbers provided would be simply adding 3. Code:
def bestSum(target, lst, mochi = {}):
    if target in mochi:
        return mochi[target]
    if target == 0:
        return []
    if target < 0:
        return None
    shortestCombination = None
    for i in lst:
        remainderCombination = bestSum(target - i, lst, mochi)
        if remainderCombination is not None:
        remainderCombination = [*remainderCombination, i]
            if shortestCombination is None or len(remainderCombination) < len(shortestCombination):
                shortestCombination = remainderCombination
    mochi[target] = shortestCombination
    return shortestCombination
I ran into this issue where data would be saved between times I ran the code, for example if I run just
print(bestSum(8, [4])
It Returns
[4, 4]
However if I run
print(bestSum(8, [2, 3, 5]))
print(bestSum(8, [4]))
it returns:
[5, 3]
[5, 3]
Am I doing something wrong here? Is this potentially a security vulnerability? Is there a way to make this return correctly? What would cause python to do something like this?
 
    