Skip to content

circular_linked_list: Add more len() tests #2051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 15 additions & 6 deletions data_structures/linked_list/circular_linked_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ def __len__(self) -> int:
>>> 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

Expand Down Expand Up @@ -137,15 +146,15 @@ def delete_front(self) -> None:
current_node = self.head

if current_node.next_ptr == current_node:
self.head, self.length = None, 0
self.head = None
self.length = 0
else:
while current_node.next_ptr != self.head:
current_node = current_node.next_ptr

current_node.next_ptr = self.head.next_ptr
self.head = self.head.next_ptr

self.length -= 1
self.length -= 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch 👍


def delete_rear(self) -> None:
"""
Expand All @@ -169,15 +178,15 @@ def delete_rear(self) -> None:
temp_node, current_node = self.head, self.head

if current_node.next_ptr == current_node:
self.head, self.length = None, 0
self.head = None
self.length = 0
else:
while current_node.next_ptr != self.head:
temp_node = current_node
current_node = current_node.next_ptr

temp_node.next_ptr = current_node.next_ptr

self.length -= 1
self.length -= 1


if __name__ == "__main__":
Expand Down
24 changes: 15 additions & 9 deletions strings/jaro_winkler.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,30 @@ def get_matched_characters(_str1: str, _str2: str) -> str:
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)
matching_2 = get_matched_characters(str2, str1)
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line has a length of 82 chars, is it below our threshold?

Copy link
Member Author

@cclauss cclauss May 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black max_line_length is 88 characters.

)

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
Expand All @@ -65,7 +70,8 @@ def get_matched_characters(_str1: str, _str2: str) -> str:
return jaro + 0.1 * prefix_len * (1 - jaro)


if __name__ == '__main__':
if __name__ == "__main__":
import doctest

doctest.testmod()
print(jaro_winkler("hello", "world"))