Skip to content
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

Add DocTests to is_palindrome.py #10081

Merged
92 changes: 92 additions & 0 deletions data_structures/linked_list/is_palindrome.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
class ListNode:
SaiHarshaK marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self, val=0, next_node=None):
self.val = val
self.next = next_node


def is_palindrome(head):
"""
Check if a linked list is a palindrome.

Args:
head (ListNode): The head of the linked list.
SaiHarshaK marked this conversation as resolved.
Show resolved Hide resolved

Returns:
bool: True if the linked list is a palindrome, False otherwise.

Examples:
>>> is_palindrome(None)
True

>>> is_palindrome(ListNode(1))
True

>>> is_palindrome(ListNode(1, ListNode(2)))
False

>>> is_palindrome(ListNode(1, ListNode(2, ListNode(1))))
True

>>> is_palindrome(ListNode(1, ListNode(2, ListNode(2, ListNode(1)))))
True
"""
if not head:
return True
# split the list to two parts
Expand Down Expand Up @@ -26,6 +57,31 @@ def is_palindrome(head):


def is_palindrome_stack(head):
"""
Check if a linked list is a palindrome using a stack.

Args:
head (ListNode): The head of the linked list.

Returns:
bool: True if the linked list is a palindrome, False otherwise.

Examples:
>>> is_palindrome_stack(None)
True

>>> is_palindrome_stack(ListNode(1))
True

>>> is_palindrome_stack(ListNode(1, ListNode(2)))
False

>>> is_palindrome_stack(ListNode(1, ListNode(2, ListNode(1))))
True

>>> is_palindrome_stack(ListNode(1, ListNode(2, ListNode(2, ListNode(1)))))
True
"""
if not head or not head.next:
return True

Expand All @@ -50,6 +106,36 @@ def is_palindrome_stack(head):


def is_palindrome_dict(head):
"""
Check if a linked list is a palindrome using a dictionary.

Args:
head (ListNode): The head of the linked list.

Returns:
bool: True if the linked list is a palindrome, False otherwise.

Examples:
>>> is_palindrome_dict(None)
True

>>> is_palindrome_dict(ListNode(1))
True

>>> is_palindrome_dict(ListNode(1, ListNode(2)))
False

>>> is_palindrome_dict(ListNode(1, ListNode(2, ListNode(1))))
True

>>> is_palindrome_dict(ListNode(1, ListNode(2, ListNode(2, ListNode(1)))))
True

>>> is_palindrome_dict(\
ListNode(\
1, ListNode(2, ListNode(1, ListNode(3, ListNode(2, ListNode(1)))))))
Comment on lines +150 to +152
Copy link
Member

Choose a reason for hiding this comment

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

PEP8 discourages backslash line termination in Python because any whitespace to the right of the backslash breaks the script on a change that is invisible to the reader. Also, backslashes are not required inside of (), [], {}...

Suggested change
>>> is_palindrome_dict(\
ListNode(\
1, ListNode(2, ListNode(1, ListNode(3, ListNode(2, ListNode(1)))))))
>>> is_palindrome_dict(
... ListNode(
... 1, ListNode(2, ListNode(1, ListNode(3, ListNode(2, ListNode(1)))))
... )
... )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ruff complains that the length of the line is >88 which is why I had to split them into multiple lines.

If i do not add backslash, DocTest assumes ListNode( is the expected value and thereby the test fails

Copy link
Member

Choose a reason for hiding this comment

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

You needed the three dots at the beginning of each continued line as discussed in the doctest docs.

False
"""
if not head or not head.next:
return True
d = {}
Expand All @@ -75,3 +161,9 @@ def is_palindrome_dict(head):
if middle > 1:
return False
return True


if __name__ == "__main__":
import doctest

doctest.testmod()