Skip to content

Commit a2f5598

Browse files
committed
reorganize arrays
1 parent 194500a commit a2f5598

16 files changed

+94
-58
lines changed

algorithms/arrays/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from .delete_nth import *
2+
from .flatten import *
3+
from .garage import *
4+
from .josephus import *
5+
from .longest_non_repeat import *
6+
from .merge_intervals import *
7+
from .missing_ranges import *
8+
from .move_zeros import *
9+
from .plus_one import *
10+
from .rotate import *
11+
from .summarize_ranges import *
12+
from .three_sum import *
13+
from .two_sum import *
File renamed without changes.

arrays/flatten.py renamed to algorithms/arrays/flatten.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ def flatten_iter(iterable):
2626
"""
2727
for element in iterable:
2828
if isinstance(element, Iterable):
29-
yield from flatten(element)
29+
yield from flatten_iter(element)
3030
else:
3131
yield element
File renamed without changes.
File renamed without changes.

arrays/longest_non_repeat.py renamed to algorithms/arrays/longest_non_repeat.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"""
1111

1212

13-
def longest_non_repeat(string):
13+
def longest_non_repeat_v1(string):
1414
"""
1515
Finds the length of the longest substring
1616
without repeating characters.
@@ -27,7 +27,7 @@ def longest_non_repeat(string):
2727
return max_len
2828

2929

30-
def longest_non_repeat_two(string):
30+
def longest_non_repeat_v2(string):
3131
"""
3232
Finds the length of the longest substring
3333
without repeating characters.

arrays/merge_intervals.py renamed to algorithms/arrays/merge_intervals.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def print_intervals(intervals):
6464
print("".join(res))
6565

6666

67-
def merge_v2(intervals):
67+
def merge_intervals(intervals):
6868
""" Merges intervals in the form of list. """
6969
if intervals is None:
7070
return None
File renamed without changes.
File renamed without changes.

arrays/plus_one.py renamed to algorithms/arrays/plus_one.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"""
88

99

10-
def plus_one(digits):
10+
def plus_one_v1(digits):
1111
"""
1212
:type digits: List[int]
1313
:rtype: List[int]
File renamed without changes.

arrays/summary_ranges.py renamed to algorithms/arrays/summarize_ranges.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77

88

9-
def summary_ranges(array):
9+
def summarize_ranges(array):
1010
"""
1111
:type array: List[int]
1212
:rtype: List[]
File renamed without changes.
File renamed without changes.

arrays/__init__.py

Whitespace-only changes.

tests/test_array.py

+75-52
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
from arrays.delete_nth import delete_nth, delete_nth_naive
2-
from arrays.flatten import flatten, flatten_iter
3-
from arrays.garage import garage
4-
from arrays.josephus_problem import josephus
5-
from arrays.longest_non_repeat import longest_non_repeat, longest_non_repeat_two
6-
from arrays.merge_intervals import Interval, merge_v2
7-
from arrays.missing_ranges import missing_ranges
8-
from arrays.move_zeros_to_end import move_zeros
9-
from arrays.plus_one import plus_one, plus_one_v2, plus_one_v3
10-
from arrays.rotate_array import rotate_v1, rotate_v2, rotate_v3
11-
from arrays.summary_ranges import summary_ranges
12-
from arrays.three_sum import three_sum
13-
from arrays.two_sum import two_sum
1+
from algorithms.arrays import delete_nth, delete_nth_naive
2+
from algorithms.arrays import flatten, flatten_iter
3+
from algorithms.arrays import garage
4+
from algorithms.arrays import josephus
5+
from algorithms.arrays import longest_non_repeat_v1, longest_non_repeat_v2
6+
from algorithms.arrays import Interval, merge_intervals
7+
from algorithms.arrays import missing_ranges
8+
from algorithms.arrays import move_zeros
9+
from algorithms.arrays import plus_one_v1, plus_one_v2, plus_one_v3
10+
from algorithms.arrays import rotate_v1, rotate_v2, rotate_v3
11+
from algorithms.arrays import summarize_ranges
12+
from algorithms.arrays import three_sum
13+
from algorithms.arrays import two_sum
1414

1515
import unittest
1616

@@ -37,15 +37,19 @@ class TestDeleteNth(unittest.TestCase):
3737

3838
def test_delete_nth_naive(self):
3939

40-
self.assertListEqual(delete_nth_naive([20, 37, 20, 21, 37, 21, 21], n=1),
40+
self.assertListEqual(delete_nth_naive(
41+
[20, 37, 20, 21, 37, 21, 21], n=1),
4142
[20, 37, 21])
42-
self.assertListEqual(delete_nth_naive([1, 1, 3, 3, 7, 2, 2, 2, 2], n=3),
43+
self.assertListEqual(delete_nth_naive(
44+
[1, 1, 3, 3, 7, 2, 2, 2, 2], n=3),
4345
[1, 1, 3, 3, 7, 2, 2, 2])
44-
self.assertListEqual(delete_nth_naive([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=3),
46+
self.assertListEqual(delete_nth_naive(
47+
[1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=3),
4548
[1, 2, 3, 1, 1, 2, 2, 3, 3, 4, 5])
4649
self.assertListEqual(delete_nth_naive([], n=5),
4750
[])
48-
self.assertListEqual(delete_nth_naive([1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=0),
51+
self.assertListEqual(delete_nth_naive(
52+
[1, 2, 3, 1, 1, 2, 1, 2, 3, 3, 2, 4, 5, 3, 1], n=0),
4953
[])
5054

5155
def test_delete_nth(self):
@@ -117,32 +121,35 @@ def test_garage(self):
117121
steps, seq = garage(initial, final)
118122

119123
self.assertEqual(steps, 4)
120-
self.assertListEqual(seq, [[0, 2, 3, 1, 4], [2, 0, 3, 1, 4], [2, 3, 0, 1, 4], [0, 3, 2, 1, 4]])
124+
self.assertListEqual(seq, [[0, 2, 3, 1, 4],
125+
[2, 0, 3, 1, 4],
126+
[2, 3, 0, 1, 4],
127+
[0, 3, 2, 1, 4]])
121128

122129

123130
class TestLongestNonRepeat(unittest.TestCase):
124131

125-
def test_longest_non_repeat(self):
132+
def test_longest_non_repeat_v1(self):
126133

127134
string = "abcabcbb"
128-
self.assertEqual(longest_non_repeat(string), 3)
135+
self.assertEqual(longest_non_repeat_v1(string), 3)
129136

130137
string = "bbbbb"
131-
self.assertEqual(longest_non_repeat(string), 1)
138+
self.assertEqual(longest_non_repeat_v1(string), 1)
132139

133140
string = "pwwkew"
134-
self.assertEqual(longest_non_repeat(string), 3)
141+
self.assertEqual(longest_non_repeat_v1(string), 3)
135142

136-
def test_longest_non_repeat_two(self):
143+
def test_longest_non_repeat_v2(self):
137144

138145
string = "abcabcbb"
139-
self.assertEqual(longest_non_repeat_two(string), 3)
146+
self.assertEqual(longest_non_repeat_v2(string), 3)
140147

141148
string = "bbbbb"
142-
self.assertEqual(longest_non_repeat_two(string), 1)
149+
self.assertEqual(longest_non_repeat_v2(string), 1)
143150

144151
string = "pwwkew"
145-
self.assertEqual(longest_non_repeat_two(string), 3)
152+
self.assertEqual(longest_non_repeat_v2(string), 3)
146153

147154

148155
class TestMergeInterval(unittest.TestCase):
@@ -156,9 +163,9 @@ def test_merge(self):
156163
[Interval(1, 6), Interval(8, 10), Interval(15, 18)]
157164
)
158165

159-
def test_merge_v2(self):
166+
def test_merge_intervals(self):
160167
interval_list = [[1, 3], [2, 6], [8, 10], [15, 18]]
161-
merged_intervals = merge_v2(interval_list)
168+
merged_intervals = merge_intervals(interval_list)
162169
self.assertEqual(
163170
merged_intervals,
164171
[[1, 6], [8, 10], [15, 18]]
@@ -172,7 +179,8 @@ def test_missing_ranges(self):
172179
arr = [3, 5, 10, 11, 12, 15, 19]
173180

174181
self.assertListEqual(missing_ranges(arr, 0, 20),
175-
[(0, 2), (4, 4), (6, 9), (13, 14), (16, 18), (20, 20)])
182+
[(0, 2), (4, 4), (6, 9),
183+
(13, 14), (16, 18), (20, 20)])
176184

177185
self.assertListEqual(missing_ranges(arr, 6, 100),
178186
[(6, 9), (13, 14), (16, 18), (20, 100)])
@@ -191,64 +199,79 @@ def test_move_zeros(self):
191199

192200
class TestPlusOne(unittest.TestCase):
193201

194-
def test_plus_one(self):
202+
def test_plus_one_v1(self):
195203

196-
self.assertListEqual(plus_one([0]), [1])
197-
self.assertListEqual(plus_one([9]), [1, 0])
198-
self.assertListEqual(plus_one([1, 0, 9]), [1, 1, 0])
199-
self.assertListEqual(plus_one([9, 9, 8, 0, 0, 9]), [9, 9, 8, 0, 1, 0])
200-
self.assertListEqual(plus_one([9, 9, 9, 9]), [1, 0, 0, 0, 0])
204+
self.assertListEqual(plus_one_v1([0]), [1])
205+
self.assertListEqual(plus_one_v1([9]), [1, 0])
206+
self.assertListEqual(plus_one_v1([1, 0, 9]), [1, 1, 0])
207+
self.assertListEqual(plus_one_v1([9, 9, 8, 0, 0, 9]),
208+
[9, 9, 8, 0, 1, 0])
209+
self.assertListEqual(plus_one_v1([9, 9, 9, 9]),
210+
[1, 0, 0, 0, 0])
201211

202212
def test_plus_one_v2(self):
203213

204214
self.assertListEqual(plus_one_v2([0]), [1])
205215
self.assertListEqual(plus_one_v2([9]), [1, 0])
206216
self.assertListEqual(plus_one_v2([1, 0, 9]), [1, 1, 0])
207-
self.assertListEqual(plus_one_v2([9, 9, 8, 0, 0, 9]), [9, 9, 8, 0, 1, 0])
208-
self.assertListEqual(plus_one_v2([9, 9, 9, 9]), [1, 0, 0, 0, 0])
217+
self.assertListEqual(plus_one_v2([9, 9, 8, 0, 0, 9]),
218+
[9, 9, 8, 0, 1, 0])
219+
self.assertListEqual(plus_one_v2([9, 9, 9, 9]),
220+
[1, 0, 0, 0, 0])
209221

210222
def test_plus_one_v3(self):
211223

212224
self.assertListEqual(plus_one_v3([0]), [1])
213225
self.assertListEqual(plus_one_v3([9]), [1, 0])
214226
self.assertListEqual(plus_one_v3([1, 0, 9]), [1, 1, 0])
215-
self.assertListEqual(plus_one_v3([9, 9, 8, 0, 0, 9]), [9, 9, 8, 0, 1, 0])
216-
self.assertListEqual(plus_one_v3([9, 9, 9, 9]), [1, 0, 0, 0, 0])
227+
self.assertListEqual(plus_one_v3([9, 9, 8, 0, 0, 9]),
228+
[9, 9, 8, 0, 1, 0])
229+
self.assertListEqual(plus_one_v3([9, 9, 9, 9]),
230+
[1, 0, 0, 0, 0])
217231

218232

219233
class TestRotateArray(unittest.TestCase):
220234

221235
def test_rotate_v1(self):
222236

223-
self.assertListEqual(rotate_v1([1, 2, 3, 4, 5, 6, 7], k=3), [5, 6, 7, 1, 2, 3, 4])
224-
self.assertListEqual(rotate_v1([1, 2, 3, 4, 5, 6, 7], k=1), [7, 1, 2, 3, 4, 5, 6])
225-
self.assertListEqual(rotate_v1([1, 2, 3, 4, 5, 6, 7], k=7), [1, 2, 3, 4, 5, 6, 7])
237+
self.assertListEqual(rotate_v1([1, 2, 3, 4, 5, 6, 7], k=3),
238+
[5, 6, 7, 1, 2, 3, 4])
239+
self.assertListEqual(rotate_v1([1, 2, 3, 4, 5, 6, 7], k=1),
240+
[7, 1, 2, 3, 4, 5, 6])
241+
self.assertListEqual(rotate_v1([1, 2, 3, 4, 5, 6, 7], k=7),
242+
[1, 2, 3, 4, 5, 6, 7])
226243
self.assertListEqual(rotate_v1([1, 2], k=111), [2, 1])
227244

228245
def test_rotate_v2(self):
229246

230-
self.assertListEqual(rotate_v2([1, 2, 3, 4, 5, 6, 7], k=3), [5, 6, 7, 1, 2, 3, 4])
231-
self.assertListEqual(rotate_v2([1, 2, 3, 4, 5, 6, 7], k=1), [7, 1, 2, 3, 4, 5, 6])
232-
self.assertListEqual(rotate_v2([1, 2, 3, 4, 5, 6, 7], k=7), [1, 2, 3, 4, 5, 6, 7])
247+
self.assertListEqual(rotate_v2([1, 2, 3, 4, 5, 6, 7], k=3),
248+
[5, 6, 7, 1, 2, 3, 4])
249+
self.assertListEqual(rotate_v2([1, 2, 3, 4, 5, 6, 7], k=1),
250+
[7, 1, 2, 3, 4, 5, 6])
251+
self.assertListEqual(rotate_v2([1, 2, 3, 4, 5, 6, 7], k=7),
252+
[1, 2, 3, 4, 5, 6, 7])
233253
self.assertListEqual(rotate_v2([1, 2], k=111), [2, 1])
234254

235255
def test_rotate_v3(self):
236256

237-
self.assertListEqual(rotate_v3([1, 2, 3, 4, 5, 6, 7], k=3), [5, 6, 7, 1, 2, 3, 4])
238-
self.assertListEqual(rotate_v3([1, 2, 3, 4, 5, 6, 7], k=1), [7, 1, 2, 3, 4, 5, 6])
239-
self.assertListEqual(rotate_v3([1, 2, 3, 4, 5, 6, 7], k=7), [1, 2, 3, 4, 5, 6, 7])
257+
self.assertListEqual(rotate_v3([1, 2, 3, 4, 5, 6, 7], k=3),
258+
[5, 6, 7, 1, 2, 3, 4])
259+
self.assertListEqual(rotate_v3([1, 2, 3, 4, 5, 6, 7], k=1),
260+
[7, 1, 2, 3, 4, 5, 6])
261+
self.assertListEqual(rotate_v3([1, 2, 3, 4, 5, 6, 7], k=7),
262+
[1, 2, 3, 4, 5, 6, 7])
240263
self.assertListEqual(rotate_v3([1, 2], k=111), [2, 1])
241264

242265

243266
class TestSummaryRanges(unittest.TestCase):
244267

245-
def test_summary_ranges(self):
268+
def test_summarize_ranges(self):
246269

247-
self.assertListEqual(summary_ranges([0, 1, 2, 4, 5, 7]),
270+
self.assertListEqual(summarize_ranges([0, 1, 2, 4, 5, 7]),
248271
[(0, 2), (4, 5), (7, 7)])
249-
self.assertListEqual(summary_ranges([-5, -4, -3, 1, 2, 4, 5, 6]),
272+
self.assertListEqual(summarize_ranges([-5, -4, -3, 1, 2, 4, 5, 6]),
250273
[(-5, -3), (1, 2), (4, 6)])
251-
self.assertListEqual(summary_ranges([-2, -1, 0, 1, 2]),
274+
self.assertListEqual(summarize_ranges([-2, -1, 0, 1, 2]),
252275
[(-2, 2)])
253276

254277

0 commit comments

Comments
 (0)