Search code examples
pythonalgorithmiterable-unpacking

unpacking * is throwing syntax error on leetcode


I am solving the coin change problem. I run code on jupyter-notebook with the given example on leetcode and it works.

enter image description here

The same code does not work on leetcode. causing syntax error:

enter image description here

Here is the code to copy:

def best_sum(target,nums):
    dp=[None for y in range(target+1)]
    dp[0]=[]
    for i in range(len(dp)):
        if dp[i]!=None:
            for num in nums:
                if i+num<=target:
                    combination=[*dp[i],num]
                    if dp[i+num]==None or len(combination)<len(dp[i+num]):
                        dp[i+num]=combination
    return dp[-1]
best_sum(11,[1,2,5])

Solution

  • Set your LeetCode language to "Python 3". The unpacking opereator isn't a thing in Python 2.

    In case you weren't aware, there are two languages python and python3 available on LeetCode. Obviously python refers to Python 2.7.