-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_two_pointers.py
More file actions
149 lines (112 loc) · 4 KB
/
test_two_pointers.py
File metadata and controls
149 lines (112 loc) · 4 KB
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
"""Comprehensive tests for two pointer implementations"""
import pytest
from implementations.two_pointers import (
two_sum_sorted, is_palindrome, three_sum, container_with_most_water,
remove_duplicates, move_zeroes, reverse_string, partition
)
class TestTwoSumSorted:
"""Test two sum on sorted array"""
def test_basic_two_sum(self):
"""Test basic two sum"""
result = two_sum_sorted([2, 7, 11, 15], 9)
assert result == [1, 2] # 1-indexed
def test_negative_numbers(self):
"""Test with negative numbers"""
result = two_sum_sorted([-1, 0, 1, 2], 1)
assert result == [1, 4] # 1-indexed (-1 + 2 = 1)
def test_no_solution(self):
"""Test when no solution exists"""
result = two_sum_sorted([1, 2, 3], 10)
assert result == []
class TestIsPalindrome:
"""Test palindrome check"""
def test_valid_palindrome(self):
"""Test valid palindrome"""
assert is_palindrome("racecar") == True
def test_invalid_palindrome(self):
"""Test invalid palindrome"""
assert is_palindrome("hello") == False
def test_empty_string(self):
"""Test empty string"""
assert is_palindrome("") == True
def test_single_char(self):
"""Test single character"""
assert is_palindrome("a") == True
class TestThreeSum:
"""Test three sum"""
def test_basic_three_sum(self):
"""Test basic three sum"""
result = three_sum([-1, 0, 1, 2, -1, -4])
expected = [[-1, -1, 2], [-1, 0, 1]]
assert sorted(result) == sorted(expected)
def test_no_solution(self):
"""Test no solution"""
result = three_sum([1, 2, 3])
assert result == []
def test_all_zeros(self):
"""Test all zeros"""
result = three_sum([0, 0, 0, 0])
assert result == [[0, 0, 0]]
class TestContainerWithMostWater:
"""Test container with most water"""
def test_basic_container(self):
"""Test basic container"""
result = container_with_most_water([1, 8, 6, 2, 5, 4, 8, 3, 7])
assert result == 49
def test_two_lines(self):
"""Test two lines"""
result = container_with_most_water([1, 1])
assert result == 1
class TestRemoveDuplicates:
"""Test remove duplicates"""
def test_with_duplicates(self):
"""Test with duplicates"""
nums = [1, 1, 2, 2, 3]
k = remove_duplicates(nums)
assert k == 3
assert nums[:k] == [1, 2, 3]
def test_no_duplicates(self):
"""Test no duplicates"""
nums = [1, 2, 3]
k = remove_duplicates(nums)
assert k == 3
class TestMoveZeroes:
"""Test move zeroes"""
def test_with_zeroes(self):
"""Test with zeroes"""
nums = [0, 1, 0, 3, 12]
move_zeroes(nums)
assert nums == [1, 3, 12, 0, 0]
def test_all_zeroes(self):
"""Test all zeroes"""
nums = [0, 0, 0]
move_zeroes(nums)
assert nums == [0, 0, 0]
def test_no_zeroes(self):
"""Test no zeroes"""
nums = [1, 2, 3]
move_zeroes(nums)
assert nums == [1, 2, 3]
class TestReverseString:
"""Test reverse string"""
def test_basic_reverse(self):
"""Test basic reverse"""
s = ["h", "e", "l", "l", "o"]
reverse_string(s)
assert s == ["o", "l", "l", "e", "h"]
def test_single_char(self):
"""Test single character"""
s = ["a"]
reverse_string(s)
assert s == ["a"]
class TestPartition:
"""Test palindrome partitioning"""
def test_basic_partitioning(self):
"""Test basic palindrome partitioning"""
result = partition("aab")
expected = [["a", "a", "b"], ["aa", "b"]]
assert sorted([sorted(p) for p in result]) == sorted([sorted(p) for p in expected])
def test_single_char(self):
"""Test single character"""
result = partition("a")
assert result == [["a"]]