This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE TABLE "cards" ( | |
| "id" serial, | |
| "card_type" varchar(20), | |
| "card_fee" money, | |
| PRIMARY KEY ("id") | |
| ); | |
| CREATE TABLE "personalizations" ( | |
| "id" serial, | |
| "font" varchar(15), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from typing import Any, Union | |
| class Node: | |
| """A linked list node implementation""" | |
| def __init__(self, node_data: Any) -> None: | |
| """Initialize the node with the given data and pointing to None""" | |
| self._data = node_data | |
| self._next_node = None |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SinglyLinkedList: | |
| """A singly linked list implementation""" | |
| def __init__(self) -> None: | |
| """Initialize a singly link list where the head is None""" | |
| self.head = None | |
| def __str__(self) -> str: | |
| """Return a string representation of the linked list""" | |
| sll_nodes = [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_last_node(self) -> "Node": | |
| """Return the last node in the linked list""" | |
| current_node = self.head | |
| while current_node.next_node is not None: | |
| current_node = current_node.next_node | |
| return current_node | |
| def get_node(self, node_data: Any) -> Union["Node", None]: | |
| """Return the first node whose data is node_data or None""" | |
| current_node = self.head |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def is_empty(self) -> bool: | |
| """Returns True is the linked list is empty""" | |
| return not self.head |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def add_front(self, new_data: Any) -> None: | |
| """Add a node to the front of the linked list""" | |
| new_node = Node(new_data) | |
| new_node.next_node = self.head | |
| self.head = new_node |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def add_back(self, new_data: Any) -> None: | |
| """Add a node to the back of the linked list""" | |
| self.get_last_node().next_node = Node(new_data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def add_after(self, node_data: Any, new_data: Any) -> None: | |
| """Add a node after the first node whose data is node_data""" | |
| new_node = Node(new_data) | |
| insert_at_node = self.get_node(node_data) | |
| if insert_at_node: | |
| new_node.next_node = insert_at_node.next_node | |
| insert_at_node.next_node = new_node |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def add_before(self, node_data: Any, new_data: Any) -> None: | |
| """Add a node before the first node whose data is node_data""" | |
| new_node = Node(new_data) | |
| insert_at_node = self.get_prev_node(node_data) | |
| if insert_at_node: | |
| new_node.next_node = insert_at_node.next_node | |
| insert_at_node.next_node = new_node | |
| else: | |
| self.add_front(new_data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def pop(self) -> Union[Any, None]: | |
| """Return and remove the node at the front of the linked list""" | |
| if self.is_empty(): | |
| return None | |
| node_to_pop = self.head | |
| self.head = self.head.next_node | |
| return node_to_pop.data |
OlderNewer