Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
refactor: reduce code spacing and verbosity
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsantana06 committed Dec 25, 2023
1 parent b1e3028 commit b6a55b5
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 73 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
.venv/
__pycache__/
__secret__.py
setup.py
dist/
pyproject.toml
setup.cfg
*.egg-info/
*.json
8 changes: 4 additions & 4 deletions linkeds/__init__.py → src/linkeds/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .jsonifier import (
InvalidJsonException,
InvalidJson,
Jsonifier
)
from .list import (
EmptyListException, FullListException, IndexListError, InvalidIterableAssignmentException,
EmptyList, FullList, IndexListError, InvalidIterableAssignment,
LinkedList,
BoundedList, DynamicList
)
Expand All @@ -12,12 +12,12 @@
DoubleNode, SingleNode
)
from .queue import (
EmptyQueueException, FullQueueException,
EmptyQueue, FullQueue,
LinkedQueue,
BoundedQueue, DynamicQueue
)
from .stack import (
EmptyStackException, FullStackException,
EmptyStack, FullStack,
LinkedStack,
BoundedStack, DynamicStack
)
2 changes: 1 addition & 1 deletion linkeds/jsonifier.py → src/linkeds/jsonifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json


class InvalidJsonException(Exception):
class InvalidJson(Exception):
'''Exception raised for invalid JSON.'''

def __init__(self, message: str = 'Invalid JSON') -> None:
Expand Down
65 changes: 20 additions & 45 deletions linkeds/list.py → src/linkeds/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
from .node import DoubleNode


class EmptyListException(Exception):
class EmptyList(Exception):
'''Exception raised for attempting operations on an empty linked list.'''

def __init__(self, message: str = 'List is empty') -> None:
super().__init__(message)


class FullListException(Exception):
class FullList(Exception):
'''Exception raised for attempting operations on a full linked list.'''

def __init__(self, message: str = 'List is full') -> None:
Expand All @@ -26,15 +26,15 @@ def __init__(self, message: str = 'List index out of range') -> None:
super().__init__(message)


class InvalidIterableAssignmentException(Exception):
class InvalidIterableAssignment(Exception):
'''Exception raised for invalid iterable assignments to a linked list.'''

def __init__(self, message: str = 'Iterable must be a list, tuple, or set') -> None:
super().__init__(message)


class LinkedList(ABC):
'''Abstract base class for a doubly linked list.'''
'''Abstract base class for a linked list.'''

ASSIGNABLE_ITERABLE_TYPES = (list, tuple, set)

Expand All @@ -55,14 +55,12 @@ def is_empty(self) -> bool:
def _add_first(self, data: object) -> None:
'''Internal method to add a new node with the given data to the beginning of the list.'''
node = DoubleNode(data)

if self.is_empty():
self._head = self._tail = node
else:
node.next = self._head
self._head.prev = node
self._head = node

self._size += 1

@abstractmethod
Expand All @@ -73,14 +71,12 @@ def add_first(self, data: object) -> None:
def _add_last(self, data: object) -> None:
'''Internal method to add a new node with the given data to the end of the list.'''
node = DoubleNode(data)

if self.is_empty():
self._head = self._tail = node
else:
node.prev = self._tail
self._tail.next = node
self._tail = node

self._size += 1

@abstractmethod
Expand All @@ -105,15 +101,12 @@ def _insert(self, index: int, data: object) -> None:
else:
node = DoubleNode(data)
current = self._head

for _ in range(index):
current = current.next

node.next = current
node.prev = current.prev
current.prev.next = node
current.prev = node

self._size += 1

@abstractmethod
Expand All @@ -140,97 +133,82 @@ def get(self, index: int) -> object:
raise IndexListError()

node = self._head

for _ in range(index):
node = node.next

return node.data

def remove_first(self) -> object:
'''
Removes and returns the data of the first element in the list.
Raises:
EmptyListException: If the list is empty.
EmptyList: If the list is empty.
'''
if self.is_empty():
raise EmptyListException()
raise EmptyList()

data = self._head.data

if self._head is self._tail:
self._head = self._tail = None
else:
self._head = self._head.next
self._head.prev = None

self._size -= 1

return data

def remove_last(self) -> object:
'''
Removes and returns the data of the last element in the list.
Raises:
EmptyListException: If the list is empty.
EmptyList: If the list is empty.
'''
if self.is_empty():
raise EmptyListException()
raise EmptyList()

data = self._tail.data

if self._head is self._tail:
self._head = self._tail = None
else:
self._tail = self._tail.prev
self._tail.next = None

self._size -= 1

return data

def remove(self, index: int) -> object:
'''
Removes and returns the data of the element at the specified index.
Raises:
EmptyListException: If the list is empty.
EmptyList: If the list is empty.
IndexListError: If the index is out of range.
'''
if self.is_empty():
raise EmptyListException()
raise EmptyList()
elif index < 0 or index >= self._size:
raise IndexListError()


if index == 0:
data = self.remove_first()
elif index == self._size - 1:
data = self.remove_last()
else:
node = self._head

for _ in range(index):
node = node.next

data = node.data
node.prev.next = node.next
node.next.prev = node.prev

self._size -= 1

return data

def _reverse(self, **kwargs) -> 'LinkedList':
'''Internal method to create and return a new reversed linked list.'''
reverse_list = self.__class__(**kwargs)
node = self._tail

while node is not None:
reverse_list.add_last(node.data)
node = node.prev

return reverse_list

@abstractmethod
Expand Down Expand Up @@ -258,7 +236,6 @@ def to_set(self) -> Set[object]:
def __iter__(self) -> Iterator[object]:
'''Iterator method to allow iterating through the elements of the linked list.'''
node = self._head

while node is not None:
yield node.data
node = node.next
Expand Down Expand Up @@ -286,10 +263,10 @@ def add_first(self, data: object) -> None:
Adds data to the beginning of the list.
Raises:
FullListException: If the list is full.
FullList: If the list is full.
'''
if self.is_full():
raise FullListException()
raise FullList()

self._add_first(data)

Expand All @@ -298,10 +275,10 @@ def add_last(self, data: object) -> None:
Adds data to the end of the list.
Raises:
FullListException: If the list is full.
FullList: If the list is full.
'''
if self.is_full():
raise FullListException()
raise FullList()

self._add_last(data)

Expand All @@ -310,10 +287,10 @@ def insert(self, index: int, data: object) -> None:
Inserts data at the specified index.
Raises:
FullListException: If the list is full.
FullList: If the list is full.
'''
if self.is_full():
raise FullListException()
raise FullList()

self._insert(index, data)

Expand All @@ -326,15 +303,14 @@ def assign_iterable(self, iterable: Union[List[object], Tuple[object], Set[objec
Assigns data from an iterable to the linked list.
Raises:
InvalidIterableAssignmentException: If the iterable type is not supported.
InvalidIterableAssignment: If the iterable type is not supported.
'''
if type(iterable) not in self.ASSIGNABLE_ITERABLE_TYPES:
raise InvalidIterableAssignmentException()
raise InvalidIterableAssignment()

self._head = self._tail = None
self._size = 0
self._capacity = len(iterable)

for item in iterable:
self.add_last(item)

Expand Down Expand Up @@ -379,14 +355,13 @@ def assign_iterable(self, iterable: Union[List[object], Tuple[object], Set[objec
Assigns data from an iterable to the linked list.
Raises:
InvalidIterableAssignmentException: If the iterable type is not supported.
InvalidIterableAssignment: If the iterable type is not supported.
'''
if type(iterable) not in self.ASSIGNABLE_ITERABLE_TYPES:
raise InvalidIterableAssignmentException()
raise InvalidIterableAssignment()

self._head = self._tail = None
self._size = 0

for item in iterable:
self.add_last(item)

Expand Down
File renamed without changes.
18 changes: 7 additions & 11 deletions linkeds/queue.py → src/linkeds/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
from .node import SingleNode


class EmptyQueueException(Exception):
class EmptyQueue(Exception):
'''Exception raised for attempting operations on an empty queue.'''

def __init__(self, message: str = 'Queue is empty') -> None:
super().__init__(message)


class FullQueueException(Exception):
class FullQueue(Exception):
'''Exception raised for attempting to enqueue into a full queue.'''

def __init__(self, message: str = 'Queue is full') -> None:
Expand All @@ -36,13 +36,11 @@ def is_empty(self) -> bool:
def _enqueue(self, data: object) -> None:
'''Internal method to enqueue a new node with the given data.'''
node = SingleNode(data)

if self.is_empty():
self._front = self._rear = node
else:
self._rear.next = node
self._rear = node

self._size += 1

@abstractmethod
Expand All @@ -55,18 +53,16 @@ def dequeue(self) -> object:
Removes and returns the front element from the queue.
Raises:
EmptyQueueException: If the queue is empty.
EmptyQueue: If the queue is empty.
'''
if self.is_empty():
raise EmptyQueueException()
raise EmptyQueue()

data = self._front.data
self._front = self._front.next
self._size -= 1

if self.is_empty():
self._rear = None

return data

def peek(self) -> object:
Expand Down Expand Up @@ -101,13 +97,13 @@ def enqueue(self, data: object) -> None:
Enqueues data into the queue.
Raises:
FullQueueException: If the queue is full.
FullQueue: If the queue is full.
'''
if self.is_full():
raise FullQueueException()
raise FullQueue()

self._enqueue(data)


class DynamicQueue(LinkedQueue):
'''Class representing a dynamic (unbounded) queue.'''
Expand Down
Loading

0 comments on commit b6a55b5

Please sign in to comment.