from itertools import combinations some_list = [2, 4, 6, 8, 7, 3] #example of received list value = 10 #example of received value def counter(some_list, value): size = len(some_list) #combination size count = 0 while size > 0: data = list(combinations(some_list, size)) #get every possible combination of elements in list (as tuples) without repeating for item in data: #get tuple summ = 0 for elem in item: #get element of tuple summ += elem #the sum of the elements of a tuple if summ == value: #if sum is equal to desired value: count++ count += 1 size -= 1 return (count) print(counter(some_list, value))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question