0

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])
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • 1
    Any idea what version of Python they're using? The `*` syntax within a list is allowed in Python 3, but not Python 2. – Tom Karzes Aug 01 '21 at 05:51
  • @TomKarzes LeetCode identifies two languages `python` and `python3`. The former is obviously Py 2.7. – iBug Aug 01 '21 at 05:58
  • @iBug They should add `python2`, and either eliminate `python` or else change it to be equivalent to `python3`, since Python 2 is no longer supported, and inappropriate as a default version of Python. – Tom Karzes Aug 01 '21 at 06:01

1 Answers1

6

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.

iBug
  • 35,554
  • 7
  • 89
  • 134