mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 01:00:15 +00:00
circular_linked_list: Add more len() tests (#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>
This commit is contained in:
parent
3357768fc3
commit
1e8fe8efcf
|
@ -622,6 +622,7 @@
|
|||
* [Boyer Moore Search](https://github.com/TheAlgorithms/Python/blob/master/strings/boyer_moore_search.py)
|
||||
* [Check Pangram](https://github.com/TheAlgorithms/Python/blob/master/strings/check_pangram.py)
|
||||
* [Is Palindrome](https://github.com/TheAlgorithms/Python/blob/master/strings/is_palindrome.py)
|
||||
* [Jaro Winkler](https://github.com/TheAlgorithms/Python/blob/master/strings/jaro_winkler.py)
|
||||
* [Knuth Morris Pratt](https://github.com/TheAlgorithms/Python/blob/master/strings/knuth_morris_pratt.py)
|
||||
* [Levenshtein Distance](https://github.com/TheAlgorithms/Python/blob/master/strings/levenshtein_distance.py)
|
||||
* [Lower](https://github.com/TheAlgorithms/Python/blob/master/strings/lower.py)
|
||||
|
|
|
@ -37,6 +37,15 @@ class CircularLinkedList:
|
|||
>>> cll.append(1)
|
||||
>>> len(cll)
|
||||
1
|
||||
>>> cll.prepend(0)
|
||||
>>> len(cll)
|
||||
2
|
||||
>>> cll.delete_front()
|
||||
>>> len(cll)
|
||||
1
|
||||
>>> cll.delete_rear()
|
||||
>>> len(cll)
|
||||
0
|
||||
"""
|
||||
return self.length
|
||||
|
||||
|
@ -130,6 +139,9 @@ class CircularLinkedList:
|
|||
>>> cll.delete_front()
|
||||
>>> print(f"{len(cll)}: {cll}")
|
||||
1: <Node data=2>
|
||||
>>> cll.delete_front()
|
||||
>>> print(f"{len(cll)}: {cll}")
|
||||
0: Empty linked list
|
||||
"""
|
||||
if not self.head:
|
||||
raise IndexError("Deleting from an empty list")
|
||||
|
@ -137,7 +149,7 @@ class CircularLinkedList:
|
|||
current_node = self.head
|
||||
|
||||
if current_node.next_ptr == current_node:
|
||||
self.head, self.length = None, 0
|
||||
self.head = None
|
||||
else:
|
||||
while current_node.next_ptr != self.head:
|
||||
current_node = current_node.next_ptr
|
||||
|
@ -146,6 +158,8 @@ class CircularLinkedList:
|
|||
self.head = self.head.next_ptr
|
||||
|
||||
self.length -= 1
|
||||
if not self.head:
|
||||
assert self.length == 0
|
||||
|
||||
def delete_rear(self) -> None:
|
||||
"""
|
||||
|
@ -162,6 +176,9 @@ class CircularLinkedList:
|
|||
>>> cll.delete_rear()
|
||||
>>> print(f"{len(cll)}: {cll}")
|
||||
1: <Node data=1>
|
||||
>>> cll.delete_rear()
|
||||
>>> print(f"{len(cll)}: {cll}")
|
||||
0: Empty linked list
|
||||
"""
|
||||
if not self.head:
|
||||
raise IndexError("Deleting from an empty list")
|
||||
|
@ -169,7 +186,7 @@ class CircularLinkedList:
|
|||
temp_node, current_node = self.head, self.head
|
||||
|
||||
if current_node.next_ptr == current_node:
|
||||
self.head, self.length = None, 0
|
||||
self.head = None
|
||||
else:
|
||||
while current_node.next_ptr != self.head:
|
||||
temp_node = current_node
|
||||
|
@ -178,6 +195,8 @@ class CircularLinkedList:
|
|||
temp_node.next_ptr = current_node.next_ptr
|
||||
|
||||
self.length -= 1
|
||||
if not self.head:
|
||||
assert self.length == 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
|
@ -34,7 +34,7 @@ def jaro_winkler(str1: str, str2: str) -> float:
|
|||
matched.append(l)
|
||||
_str2 = f"{_str2[0:_str2.index(l)]} {_str2[_str2.index(l) + 1:]}"
|
||||
|
||||
return ''.join(matched)
|
||||
return "".join(matched)
|
||||
|
||||
# matching characters
|
||||
matching_1 = get_matched_characters(str1, str2)
|
||||
|
@ -42,17 +42,22 @@ def jaro_winkler(str1: str, str2: str) -> float:
|
|||
match_count = len(matching_1)
|
||||
|
||||
# transposition
|
||||
transpositions = len(
|
||||
[(c1, c2) for c1, c2 in zip(matching_1, matching_2) if c1 != c2]
|
||||
) // 2
|
||||
transpositions = (
|
||||
len([(c1, c2) for c1, c2 in zip(matching_1, matching_2) if c1 != c2]) // 2
|
||||
)
|
||||
|
||||
if not match_count:
|
||||
jaro = 0.0
|
||||
else:
|
||||
jaro = 1 / 3 * (
|
||||
match_count / len(str1)
|
||||
+ match_count / len(str2)
|
||||
+ (match_count - transpositions) / match_count)
|
||||
jaro = (
|
||||
1
|
||||
/ 3
|
||||
* (
|
||||
match_count / len(str1)
|
||||
+ match_count / len(str2)
|
||||
+ (match_count - transpositions) / match_count
|
||||
)
|
||||
)
|
||||
|
||||
# common prefix up to 4 characters
|
||||
prefix_len = 0
|
||||
|
@ -65,7 +70,8 @@ def jaro_winkler(str1: str, str2: str) -> float:
|
|||
return jaro + 0.1 * prefix_len * (1 - jaro)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
|
||||
doctest.testmod()
|
||||
print(jaro_winkler("hello", "world"))
|
||||
|
|
Loading…
Reference in New Issue
Block a user