import random import time def sumExists(num_list, sum_num): """ [1,5,2,5], 7 """ print "Searching list of {0} numbers for sum of {1}".format(len(num_list), sum_num) currentPos = 0 num_list.sort() for y in num_list: if y >= sum_num: return False x = sum_num - y if x in num_list and num_list.index(x) != currentPos: print "{0} + {1} equals {2}".format(x,y,sum_num) return True currentPos = currentPos + 1 return False print "Generating numbers" l = [random.randint(1,2000) for i in range(0,100000)] sum_num = random.randint(1,4000) print "Starting test" start_time = time.time() print sumExists(l, sum_num) print "Total seconds: {0}".format(time.time()-start_time)
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