-
Notifications
You must be signed in to change notification settings - Fork 2
/
codechallenge_120.py
149 lines (116 loc) · 4.81 KB
/
codechallenge_120.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'''
Date: 06/28/2022
Two pointers problem: (from leetcode)
=======================
Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.
Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.
The tests are generated such that there is exactly one solution. You may not use the same element twice.
Your solution must use only constant extra space.
Constraints:
============
2 <= numbers.length <= 3 * 104
-1000 <= numbers[i] <= 1000
numbers is sorted in non-decreasing order.
-1000 <= target <= 1000
The tests are generated such that there is exactly one solution.
Example 1:
==========
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
Example 2:
==========
Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].
Example 3:
==========
Input: numbers = [-1,0], target = -1
Output: [1,2]
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].
'''
import unittest
import os, time
from unittest.runner import TextTestResult
def sum(num1, num2):
return num1 + num2
# worst case time complexity of O(n)
# traverse the list from both ends using loop[i, j]
def sum2numbers(numbers, target):
i = 0 # start from the beginning of the list
j = len(numbers) - 1 # start from the end of the list
# traverse the list from both ends going toward the middle
while i < j:
if numbers[i] + numbers[j] == target:
return [i + 1, j + 1]
elif numbers[i] + numbers[j] < target:
i += 1
else:
j -= 1
return []
# traverse the list from both ends using list comprehension
def sum_two_numbers(numbers, target):
n = len(numbers)
i = 0
# list comprehension
[sum(numbers[i], numbers[n - i - 1]) == target for i in range(n // 2)]
return [i + 1, n - i - 2]
def main():
numbers = [2,3,5,7,11,15]
target = 9
print("Test case #0:(calling list comprehension method)")
print("numbers = [2,3,5,7,11,15], target = 9 and expected result = {}".format(sum_two_numbers(numbers, target)))
numbers = [2,7,11,15]
print("\nTest case #1:")
print("Given a 1-indexed sorted in non-decreasing order array of integers numbers {} and target = {}".format(numbers, target))
print("The indices of two numbers that add up to a target number: {}".format(sum2numbers(numbers, target)))
print("\nTest case #2:")
numbers = [2,3,4]
target = 6
print("Given a 1-indexed sorted in non-decreasing order array of integers numbers {} and target = {}".format(numbers, target))
print("The indices of two numbers that add up to a target number: {}".format(sum2numbers(numbers, target)))
print("\nTest case #3:")
numbers = [-1,0]
target = -1
print("Given a 1-indexed sorted in non-decreasing order array of integers numbers {} and target = {}".format(numbers, target))
print("The indices of two numbers that add up to a target number: {}".format(sum2numbers(numbers, target)))
print("\n\n")
class TestSum2Numbers(unittest.TestCase):
def testCase1(self):
assert sum2numbers([2,7,11,15], 9) == [1, 2]
def testCase2(self):
assert sum2numbers([2,3,4], 6) == [1, 3]
def testCase3(self):
assert sum2numbers([-1,0], -1) == [1, 2]
if __name__ == '__main__':
if os.environ.get('UNITTEST_ONLY') == 'True':
main()
else:
try:
unittest.main()
except Exception as e:
print(e)
'''
Unit test output:
=================
PS D:\DEVEL\GIT\DailyCodingChallenge> pipenv run python .\codechallenge_120.py
Loading .env environment variables...
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
Runtime main():
==============
Loading .env environment variables...
Test case #0:
numbers = [2,3,5,7,11,15], target = 9 and expected result = [1, 4]
Test case #1:
Given a 1-indexed sorted in non-decreasing order array of integers numbers [2, 7, 11, 15] and target = 9
The indices of two numbers that add up to a target number: [1, 2]
Test case #2:
Given a 1-indexed sorted in non-decreasing order array of integers numbers [2, 3, 4] and target = 6
The indices of two numbers that add up to a target number: [1, 3]
Test case #3:
Given a 1-indexed sorted in non-decreasing order array of integers numbers [-1, 0] and target = -1
The indices of two numbers that add up to a target number: [1, 2]
'''