Skip to content

Commit 1e8fe8e

Browse files
cclaussgithub-actions
and
github-actions
authored
circular_linked_list: Add more len() tests (TheAlgorithms#2051)
* circular_linked_list: Add more len() tests * fixup! Format Python code with psf/black push * prepend() * updating DIRECTORY.md * Fix decrementation of self.length * Add empty list tests Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 3357768 commit 1e8fe8e

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-11
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@
622622
* [Boyer Moore Search](https://github.com/TheAlgorithms/Python/blob/master/strings/boyer_moore_search.py)
623623
* [Check Pangram](https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py)
624624
* [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/is_palindrome.py)
625+
* [Jaro Winkler](https://github.com/TheAlgorithms/Python/blob/master/strings/jaro_winkler.py)
625626
* [Knuth Morris Pratt](https://github.com/TheAlgorithms/Python/blob/master/strings/knuth_morris_pratt.py)
626627
* [Levenshtein Distance](https://github.com/TheAlgorithms/Python/blob/master/strings/levenshtein_distance.py)
627628
* [Lower](https://github.com/TheAlgorithms/Python/blob/master/strings/lower.py)

data_structures/linked_list/circular_linked_list.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ def __len__(self) -> int:
3737
>>> cll.append(1)
3838
>>> len(cll)
3939
1
40+
>>> cll.prepend(0)
41+
>>> len(cll)
42+
2
43+
>>> cll.delete_front()
44+
>>> len(cll)
45+
1
46+
>>> cll.delete_rear()
47+
>>> len(cll)
48+
0
4049
"""
4150
return self.length
4251

@@ -130,14 +139,17 @@ def delete_front(self) -> None:
130139
>>> cll.delete_front()
131140
>>> print(f"{len(cll)}: {cll}")
132141
1: <Node data=2>
142+
>>> cll.delete_front()
143+
>>> print(f"{len(cll)}: {cll}")
144+
0: Empty linked list
133145
"""
134146
if not self.head:
135147
raise IndexError("Deleting from an empty list")
136148

137149
current_node = self.head
138150

139151
if current_node.next_ptr == current_node:
140-
self.head, self.length = None, 0
152+
self.head = None
141153
else:
142154
while current_node.next_ptr != self.head:
143155
current_node = current_node.next_ptr
@@ -146,6 +158,8 @@ def delete_front(self) -> None:
146158
self.head = self.head.next_ptr
147159

148160
self.length -= 1
161+
if not self.head:
162+
assert self.length == 0
149163

150164
def delete_rear(self) -> None:
151165
"""
@@ -162,14 +176,17 @@ def delete_rear(self) -> None:
162176
>>> cll.delete_rear()
163177
>>> print(f"{len(cll)}: {cll}")
164178
1: <Node data=1>
179+
>>> cll.delete_rear()
180+
>>> print(f"{len(cll)}: {cll}")
181+
0: Empty linked list
165182
"""
166183
if not self.head:
167184
raise IndexError("Deleting from an empty list")
168185

169186
temp_node, current_node = self.head, self.head
170187

171188
if current_node.next_ptr == current_node:
172-
self.head, self.length = None, 0
189+
self.head = None
173190
else:
174191
while current_node.next_ptr != self.head:
175192
temp_node = current_node
@@ -178,6 +195,8 @@ def delete_rear(self) -> None:
178195
temp_node.next_ptr = current_node.next_ptr
179196

180197
self.length -= 1
198+
if not self.head:
199+
assert self.length == 0
181200

182201

183202
if __name__ == "__main__":

strings/jaro_winkler.py

+15-9
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,30 @@ def get_matched_characters(_str1: str, _str2: str) -> str:
3434
matched.append(l)
3535
_str2 = f"{_str2[0:_str2.index(l)]} {_str2[_str2.index(l) + 1:]}"
3636

37-
return ''.join(matched)
37+
return "".join(matched)
3838

3939
# matching characters
4040
matching_1 = get_matched_characters(str1, str2)
4141
matching_2 = get_matched_characters(str2, str1)
4242
match_count = len(matching_1)
4343

4444
# transposition
45-
transpositions = len(
46-
[(c1, c2) for c1, c2 in zip(matching_1, matching_2) if c1 != c2]
47-
) // 2
45+
transpositions = (
46+
len([(c1, c2) for c1, c2 in zip(matching_1, matching_2) if c1 != c2]) // 2
47+
)
4848

4949
if not match_count:
5050
jaro = 0.0
5151
else:
52-
jaro = 1 / 3 * (
53-
match_count / len(str1)
54-
+ match_count / len(str2)
55-
+ (match_count - transpositions) / match_count)
52+
jaro = (
53+
1
54+
/ 3
55+
* (
56+
match_count / len(str1)
57+
+ match_count / len(str2)
58+
+ (match_count - transpositions) / match_count
59+
)
60+
)
5661

5762
# common prefix up to 4 characters
5863
prefix_len = 0
@@ -65,7 +70,8 @@ def get_matched_characters(_str1: str, _str2: str) -> str:
6570
return jaro + 0.1 * prefix_len * (1 - jaro)
6671

6772

68-
if __name__ == '__main__':
73+
if __name__ == "__main__":
6974
import doctest
75+
7076
doctest.testmod()
7177
print(jaro_winkler("hello", "world"))

0 commit comments

Comments
 (0)