diff --git a/DIRECTORY.md b/DIRECTORY.md index 78095b264..78afe07ec 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) diff --git a/data_structures/linked_list/circular_linked_list.py b/data_structures/linked_list/circular_linked_list.py index cf523f0a4..290e30ebf 100644 --- a/data_structures/linked_list/circular_linked_list.py +++ b/data_structures/linked_list/circular_linked_list.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: + >>> 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: + >>> 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__": diff --git a/strings/jaro_winkler.py b/strings/jaro_winkler.py index 73827c233..de0953854 100644 --- a/strings/jaro_winkler.py +++ b/strings/jaro_winkler.py @@ -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"))