From a22bea6fa6a403e201bb79662d43015c0795835b Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 16 Apr 2021 14:45:17 +0200 Subject: [PATCH] Add Python doctests to doubly linked list (#170) * Add Python doctests to doubly linked list * Update doubly.py * Update doubly.py * Rename linked-lists/Python/doubly.py to algorithms/Python/linked_lists/doubly.py --- algorithms/Python/linked_lists/doubly.py | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/algorithms/Python/linked_lists/doubly.py b/algorithms/Python/linked_lists/doubly.py index 4edda191..5807da8c 100644 --- a/algorithms/Python/linked_lists/doubly.py +++ b/algorithms/Python/linked_lists/doubly.py @@ -5,6 +5,10 @@ class Node: self.prev = None def __repr__(self): + """ + >>> Node(77) + Node(77) + """ return f"Node({self.data})" @@ -13,12 +17,34 @@ class DoublyLinkedList: self.head = None def __iter__(self): + """ + >>> dll = DoublyLinkedList() + >>> list(dll) + [] + >>> dll.push(88) + >>> tuple(dll) + (88,) + >>> dll.push(89) + >>> tuple(dll) + (89, 88) + """ node = self.head while node: yield node.data node = node.next def __repr__(self): + """ + >>> dll = DoublyLinkedList() + >>> repr(dll) + 'DoublyLinkedList()' + >>> dll.push(99) + >>> dll.push(100) + >>> repr(dll) + 'DoublyLinkedList(100, 99)' + >>> str(dll) + 'DoublyLinkedList(100, 99)' + """ return f"DoublyLinkedList({', '.join(str(node) for node in self)})" # Adding data elements is like pushing onto a stack