10 examples of 'python program to find sum of 3 numbers' in Python

Every line of 'python program to find sum of 3 numbers' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
59def threeSum_2(self, numbers):
60
61 if not numbers or len(numbers) < 3:
62 return []
63
64 results = []
65 numbers.sort()
66
67 for i in range(len(numbers) - 2):
68 if i == 0 or numbers[i] != numbers[i - 1]:
69 target = -numbers[i]
70
71 left = i + 1
72 right = len(numbers) - 1
73
74 while left < right:
75 total = numbers[left] + numbers[right]
76
77 if total < target:
78 left += 1
79
80 elif total > target:
81 right -= 1
82
83 else:
84
85 results.append([numbers[i], numbers[left], numbers[right]])
86 left += 1
87 right -= 1
88
89 while left < right and numbers[left - 1] == numbers[left]:
90 left += 1
91
92 while left < right and numbers[right] == numbers[right + 1]:
93 right -= 1
94
95 return results
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

23def threeSum(self, nums):
24 """
25 :type nums: List[int]
26 :rtype: List[List[int]]
27 """
28 nums.sort()
29 res = []
30
31 for i in range(len(nums)-1): # because sums of 3 numbers
32 if i == 0 or i > 0 and nums[i-1] != nums[i]:
33 # avoid duplicate triplets [1 ,1, 1, -2]
34 left = i + 1
35 right = len(nums) - 1
36 while left < right: # two-way pointer
37 s = nums[i] + nums[left] + nums[right]
38 if s == 0:
39 res.append([nums[i], nums[left], nums[right]])
40 left += 1
41 right -= 1
42 while left < right and nums[left] == nums[left - 1]:
43 left += 1
44 while right > left and nums[right] == nums[right + 1]:
45 right -= 1
46 elif s < 0:
47 left += 1
48 else:
49 right -= 1
50 return res
16def main():
17 n = int(input())
18 print(sum(get_digit(n)))
2def singleNumber(self, nums: List[int]) -> int:
3 start = nums[0]
4 for val in nums[1:]:
5 start ^= val
6
7 return start
29def main():
30 print "source include/have_tokudb.inc;"
31 print "# this test is generated by change_int_rename.py"
32 print "--disable_warnings"
33 print "DROP TABLE IF EXISTS t, ti;"
34 print "--enable_warnings"
35 print "SET SESSION DEFAULT_STORAGE_ENGINE=\"TokuDB\";"
36 print "SET SESSION TOKUDB_DISABLE_SLOW_ALTER=1;"
37 gen_test(
38 [ "TINYINT", "SMALLINT", "MEDIUMINT", "INT", "BIGINT" ],
39 [ [ -128, -1, 0, 1, 127 ], [ -32768, -1, 0, 1, 32767], [-8388608, -1, 0, 1, 8388607], [-2147483648, 0, 1, 2147483647], [-9223372036854775808, 0, 1, 9223372036854775807] ]
40 )
41 gen_test(
42 [ "TINYINT UNSIGNED", "SMALLINT UNSIGNED", "MEDIUMINT UNSIGNED", "INT UNSIGNED", "BIGINT UNSIGNED" ],
43 [ [ 0, 1, 255 ], [ 0, 1, 65535], [0, 1, 16777215], [0, 1, 4294967295], [0, 1, 18446744073709551615] ]
44 )
45 return 0
32def main():
33 f = fac(N)
34 d('factorial(%i)=%i' %(N, f))
35 #s = digitsum(f)
36 s = sum([int(i) for i in str(f)])
37 print(s)
9def consecutive_sum(number):
10 if number // 10 < 1:
11 return number
12 return number % 10 + consecutive_sum(number // 10)
17def my_sum(a, b):
18 print("a + b = %d" % (a + b))
34def listsum(numList):
35 if len(numList) == 1:
36 return numList[0]
37 else:
38 return numList[0] + listsum(numList[1:])
3def threeSum(self, num):
4 num = sorted(num)
5 res = []
6 i = 0
7 while i < len(num) - 2:
8 j = i + 1
9 k = len(num) - 1
10 while j < k:
11 sum = num[i] + num[j] + num[k]
12 if sum == 0:
13 res.append([num[i], num[j], num[k]])
14 j, k = j + 1, k - 1
15 while j < k and num[j] == num[j - 1]:
16 j += 1
17 while j < k and num[k] == num[k + 1]:
18 k -= 1
19 elif sum < 0:
20 j += 1
21 else:
22 k -= 1
23 i += 1
24 while i < len(num) - 2 and num[i] == num[i - 1]:
25 i += 1
26
27 return res

Related snippets