-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtwo_sum.py
120 lines (83 loc) · 2.63 KB
/
two_sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'''
DATA: 22/03/2023
FONTE: Leetcode
https://leetcode.com/problems/two-sum/
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Input: nums = [2,11,7,15], target = 9
Output: [0,2]
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
Constraints:
2 <= nums.length <= 104
-109 <= nums[i] <= 109
-109 <= target <= 109
Only one valid answer exists.
Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?
Participantes:
- Vitor Pestana
- Cássio
- Márcio Contado
- Fred
- Everton Matos
- Greg
-
'''
import pytest
def two_sum(array, target):
if len(array) < 2:
raise ValueError('The length of the "array" must be higher or equals to 2')
if len(array) > 10**4:
raise ValueError('The length of the "array" must be lower or equals to 104')
if not -10**9 <= target <= 10**9:
raise ValueError('Error')
if min(array) < -10**9:
raise ValueError('Error')
if max(array) > 10**9:
raise ValueError('Error')
# for key, element in enumerate(array):
# if array[key] + array[key + 1] == target:
# return [key, key + 1]
resp = []
for i,x in enumerate(array):
for j,y in enumerate(array):
#print(f'i={i}, x={x}, j={j}, y={y}')
if not i == j:
if (x + y) == target and not [j, i] in resp:
resp.append([i,j])
if len(resp) != 1:
raise ValueError('Error')
return resp[0]
def test_two_sum():
assert(two_sum([2,7,11,15], 9)) == [0, 1]
assert(two_sum([3,2,4], 6)) == [1, 2]
assert(two_sum([3,3], 6)) == [0, 1]
assert(two_sum([3,2,3], 6)) == [0, 2]
assert(two_sum([2222222,2222222],4444444)) == [0, 1]
with pytest.raises(ValueError):
two_sum([], 0)
with pytest.raises(ValueError):
two_sum([1], 0)
with pytest.raises(ValueError):
two_sum([1,2], 110)
with pytest.raises(ValueError):
two_sum([-110,1], 0)
with pytest.raises(ValueError):
two_sum([1,110], 0)
with pytest.raises(ValueError):
two_sum([-110,110], 110**10)
with pytest.raises(ValueError):
two_sum([3,3,3], 6)
with pytest.raises(ValueError):
two_sum([2, 7, 9, 2], 6)
if __name__ == "__main__":
pytest.main(['-svv', __file__])