Skip to content

Commit 3e6c93a

Browse files
JKLiang9714keon
authored andcommitted
fix: test_heap, tree/traversal/__init__ (keon#481)
* fix: test_heap, tree/traversal/__init__ * fix a error in longest_non_repeat.py
1 parent f4b346c commit 3e6c93a

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

algorithms/arrays/longest_non_repeat.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,17 @@ def get_longest_non_repeat_v1(string):
5353
"""
5454
if string is None:
5555
return 0
56+
sub_string = ''
5657
temp = []
5758
max_len = 0
5859
for i in string:
5960
if i in temp:
6061
temp = []
6162
temp.append(i)
62-
max_len = max(max_len, len(temp))
63-
return max_len, temp
63+
if len(temp) > max_len:
64+
max_len = len(temp)
65+
sub_string = ''.join(temp)
66+
return max_len, sub_string
6467

6568
def get_longest_non_repeat_v2(string):
6669
"""
@@ -71,12 +74,15 @@ def get_longest_non_repeat_v2(string):
7174
"""
7275
if string is None:
7376
return 0
77+
sub_string = ''
7478
start, max_len = 0, 0
7579
used_char = {}
7680
for index, char in enumerate(string):
7781
if char in used_char and start <= used_char[char]:
7882
start = used_char[char] + 1
7983
else:
80-
max_len = max(max_len, index - start + 1)
84+
if index - start + 1 > max_len:
85+
max_len = index - start + 1
86+
sub_string = string[start: index + 1]
8187
used_char[char] = index
82-
return max_len, start
88+
return max_len, sub_string

algorithms/tree/traversal/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
from .preorder import *
22
from .postorder import *
3+
from .inorder import *

tests/test_heap.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ def test_max_sliding_window(self):
5656

5757
def test_k_closest_points(self):
5858
points = [(1, 0), (2, 3), (5, 2), (1, 1), (2, 8), (10, 2), (-1, 0), (-2, -2)]
59-
self.assertEqual([(-1, 0), (1, 0)], k_closest_points.k_closest(points, 2))
60-
self.assertEqual([(1, 1), (-1, 0), (1, 0)], k_closest_points.k_closest(points, 3))
61-
self.assertEqual([(-2, -2), (1, 1), (1, 0), (-1, 0)], k_closest_points.k_closest(points, 4))
59+
self.assertEqual([(-1, 0), (1, 0)], k_closest(points, 2))
60+
self.assertEqual([(1, 1), (-1, 0), (1, 0)], k_closest(points, 3))
61+
self.assertEqual([(-2, -2), (1, 1), (1, 0), (-1, 0)], k_closest(points, 4))
6262
self.assertEqual([(10, 2), (2, 8), (5, 2), (-2, -2), (2, 3),
63-
(1, 0), (-1, 0), (1, 1)], k_closest_points.k_closest(points, 8))
63+
(1, 0), (-1, 0), (1, 1)], k_closest(points, 8))
6464

6565

6666
if __name__ == "__main__":

0 commit comments

Comments
 (0)