0

Here is the code and error message in PyCharm. If anyone have any insights what is wrong with the syntax, it will be great. :)

candidates is an array of non-negative integers, and target is a positive integer. For example, candidates are [10,1,2,7,6,1,5] and target is 10.

I am using Python 2.6 on OSX.

def combinationSum2(self, candidates, target):
    candidates.sort()
    table = [None] + [set() for i in range(target)]
    for i in candidates:
        if i > target:
            break
        for j in range(target - i, 0, -1):
            table[i + j] |= {elt + (i,) for elt in table[j]}
        table[i].add((i,))
    return map(list, table[target])

enter image description here

Lin Ma
  • 9,739
  • 32
  • 105
  • 175
  • 1
    Entered into my interpreters (2 and 3), no syntax error. – TigerhawkT3 Dec 03 '15 at 08:48
  • 1
    Could be a problem with tab or spaces? – toti08 Dec 03 '15 at 08:51
  • 1
    Set comprehensions only exist in Python 2.7 and later. – BrenBarn Dec 03 '15 at 08:52
  • @BrenBarn, thanks a lot, want to confirm my understanding of set comprehension is correct, set comprehension means construct a set without using set(), but using {}? Here is more details for what I studied. http://love-python.blogspot.com/2012/12/set-comprehensions-in-python.html – Lin Ma Dec 04 '15 at 08:05
  • @TigerhawkT3, if you could advise whether my above understanding of set comprehension is correct, it will be great. – Lin Ma Dec 04 '15 at 08:06
  • @toti08, if you could advise whether my above understanding of set comprehension is correct, it will be great. – Lin Ma Dec 04 '15 at 08:06
  • 1
    Set comprehension is like list comprehension: a literal expression for the object you want to create, without needing to start with an empty iterable and then add to it in an explicit traditional loop (e.g. `myset = set(); for i in range(5): myset.add(i)`). – TigerhawkT3 Dec 04 '15 at 08:09
  • @TigerhawkT3, great answer. So for list comprehension, I should use `{}`? – Lin Ma Dec 04 '15 at 08:11
  • 1
    No. List comprehensions use `[]`, set comprehensions use `{}`. [This page of the tutorial](https://docs.python.org/3.4/tutorial/datastructures.html#list-comprehensions) describes list, set, and dictionary comprehensions. – TigerhawkT3 Dec 04 '15 at 08:14
  • @TigerhawkT3, what is the benefit of set comprehension, comparing to not using it? – Lin Ma Dec 05 '15 at 05:49
  • 1
    See [my comment above](http://stackoverflow.com/questions/34061397/strange-syntax-error-for-python-tuple-operation?noredirect=1#comment55919686_34061397) regarding concision. – TigerhawkT3 Dec 05 '15 at 05:59
  • @TigerhawkT3, want to confirm we are on the same page, the only benefit is "without needing to start with an empty iterable"? Thanks. :) – Lin Ma Dec 05 '15 at 06:01
  • 1
    Pretty much. Also see [here](https://docs.python.org/3.4/tutorial/datastructures.html#list-comprehensions) for more. – TigerhawkT3 Dec 05 '15 at 06:02
  • @TigerhawkT3, read the document, but does not feel too much benefit other than shorter line of code? I could be wrong, appreciate if you could highlight 1 or 2 points which you think set or list comprehension is more benefit than not using it? Thanks and have a good weekend. – Lin Ma Dec 06 '15 at 00:28
  • 1
    "Shorter and clearer" is a real benefit. Readability counts. There may sometimes be slight performance benefits in a comprehension, but that's not the important part. – TigerhawkT3 Dec 06 '15 at 00:32
  • @TigerhawkT3, thanks but why performance could be even better? I thought underlying they should generate the same C/Java code? – Lin Ma Dec 06 '15 at 00:48
  • 1
    See [here](http://stackoverflow.com/questions/22108488/are-list-comprehensions-and-functional-functions-faster-than-for-loops) for some more information. – TigerhawkT3 Dec 06 '15 at 01:40
  • @TigerhawkT3, thanks a lot! Could you add a reply to my question? I will mark it as answered so that it benefit other people as well. :) – Lin Ma Dec 21 '15 at 00:08
  • @LinMa - Nope; the question is closed, so no answers can be added. Don't worry about it. – TigerhawkT3 Dec 21 '15 at 00:10
  • @TigerhawkT3, thanks. My initial ask is just credit more for your help. Have a good day. :) – Lin Ma Dec 21 '15 at 00:30

0 Answers0