-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from warmachine028/v0.0.3
Added NEW MODULES in DataStax Library
- Loading branch information
Showing
23 changed files
with
632 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# __init__.py | ||
|
||
# Versioning of the datastax package | ||
__version__ = "0.0.2" | ||
__all__ = ['trees', 'linkedlists'] | ||
__version__ = "0.0.3" | ||
__all__ = ['trees', 'linkedlists', 'arrays'] |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .queue import Queue | ||
from .stack import Stack | ||
|
||
__all__ = [ | ||
'Queue', | ||
'Stack' | ||
] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Queue Implementation using Lists (Pseudo Arrays) | ||
import math | ||
from typing import Any, Union | ||
|
||
|
||
class Queue: | ||
def __init__(self, capacity: int = None): | ||
self._capacity = capacity if capacity is not None else math.inf | ||
self._array: list[Any] = [] | ||
self._front = self._rear = 0 | ||
|
||
@property | ||
def array_repr(self) -> list[Any]: | ||
return self._array[self._front:self._rear] | ||
|
||
def is_full(self) -> bool: | ||
return len(self._array) == self._capacity | ||
|
||
def is_empty(self) -> bool: | ||
return not len(self._array) | ||
|
||
def enqueue(self, item: Any) -> int: | ||
if self.is_full(): | ||
print("WARNING: THE QUEUE IS ALREADY FULL, CANT ENQUEUE ANY FURTHER") | ||
return -1 | ||
self._array.append(item) | ||
self._rear += 1 | ||
return 0 | ||
|
||
def dequeue(self) -> Union[int, Any]: | ||
if self.is_empty() or self._front >= self._rear: | ||
print("WARNING: THE QUEUE IS ALREADY EMPTY, CANT DEQUEUE ANY FURTHER") | ||
return -1 | ||
deleted_item = self._array[self._front] | ||
self._front += 1 | ||
return deleted_item | ||
|
||
def peek(self) -> str: | ||
if self.is_empty() or self._front >= self._rear: return "QUEUE EMPTY" | ||
return str(self._array[self._front]) | ||
|
||
def __str__(self): | ||
if self.is_empty(): return '┌───────────────────┐\n' \ | ||
'│ QUEUE EMPTY │\n' \ | ||
'└───────────────────┘' | ||
padding = 4 | ||
max_breadth = max(len(str(item)) for item in self._array) + padding | ||
middle_part = 'FRONT -> │' | ||
upper_part = f"{' ' * (len(middle_part) - 1)}┌" | ||
lower_part = f"{' ' * (len(middle_part) - 1)}└" | ||
if self._front: # Representing Garbage Values with '╳' | ||
for _ in self._array[:self._front]: | ||
middle_part += f"{'╳'.center(max_breadth)}│" | ||
upper_part += f"{'─' * max_breadth}┬" | ||
lower_part += f"{'─' * max_breadth}┴" | ||
upper_part = upper_part[:-1] + '╥' | ||
middle_part = middle_part[:-1] + '║' | ||
lower_part = lower_part[:-1] + '╨' | ||
for item in self._array[self._front:]: | ||
middle_part += f'{str(item).center(max_breadth)}│' | ||
upper_part += f"{'─' * max_breadth}┬" | ||
lower_part += f"{'─' * max_breadth}┴" | ||
upper_part = f"{upper_part[:-1]}{'╖' if len(self._array) == self._front else '┐'}\n" | ||
middle_part += ' <- REAR\n' | ||
lower_part = f"{lower_part[:-1]}{'╜' if len(self._array) == self._front else '┘'}\n" | ||
return upper_part + middle_part + lower_part | ||
|
||
def __repr__(self): | ||
return self.__str__() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Stack Implementation using Lists (Pseudo Arrays) | ||
import math | ||
from typing import Any, Union | ||
|
||
|
||
class Stack: | ||
def __init__(self, capacity: int = None): | ||
self.capacity = capacity if capacity is not None else math.inf | ||
self._array: list[Any] = [] | ||
|
||
@property | ||
def array(self): | ||
return self._array | ||
|
||
def is_full(self) -> bool: | ||
return len(self._array) == self.capacity | ||
|
||
def is_empty(self) -> bool: | ||
return not len(self._array) | ||
|
||
def push(self, item: Any) -> int: | ||
if self.is_full(): # Overflow Condition | ||
print('OVERFLOW WARNING: ATTEMPTED PUSH OPERATION IN FULL STACK') | ||
return -1 | ||
self._array.append(item) | ||
return 0 | ||
|
||
def pop(self) -> Union[int, Any]: | ||
if self.is_empty(): # Underflow Condition handled | ||
print('UNDERFLOW WARNING: ATTEMPTED POP OPERATION FROM EMPTY STACK') | ||
return -1 | ||
return self._array.pop() | ||
|
||
def peek(self) -> str: | ||
return 'STACK EMPTY' if self.is_empty() else self._array[-1] | ||
|
||
def __str__(self): | ||
if self.is_empty(): return '│STACK EMPTY│\n' \ | ||
'╰───────────╯\n' | ||
padding = 9 | ||
maximum_breadth = (max(len(str(item)) for item in self._array)) + padding | ||
if self.is_full(): string = f"┌{'─' * maximum_breadth}┐\n" # For FULLY LOADED STACK | ||
elif (len(self._array) < 0.7 * self.capacity) or (self.capacity - len(self._array) > 5): | ||
string = f"│{' ' * maximum_breadth}│\n" \ | ||
f":{' ' * maximum_breadth}:\n" # Shrink visualization | ||
else: string = f"│{' ' * maximum_breadth}│\n" * 2 * (self.capacity - len(self._array)) # Expand Visualization | ||
|
||
for n, item in enumerate(self._array[::-1]): | ||
top = '' if self.is_full() and not n else f"├{'─' * maximum_breadth}┤\n" | ||
bottom = f"│{str(item).center(maximum_breadth)}│ {'<- TOP' if not n else ''}\n" | ||
string += top + bottom | ||
string += f"╰{'─' * maximum_breadth}╯\n" | ||
return string | ||
|
||
def __repr__(self): | ||
return self.__str__() |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Queue implementation using LinkedLists | ||
from __future__ import annotations | ||
|
||
from sys import maxsize | ||
from typing import Any, Optional, Union | ||
|
||
from datastax.linkedlists.linked_list import LinkedList, Node | ||
|
||
|
||
class Queue(LinkedList): | ||
def __init__(self, array: list[Any] = None, capacity: int = None): | ||
super().__init__() | ||
self._rear = 0 | ||
if capacity is not None and capacity < 0: | ||
print("Capacity can't be negative" | ||
"Setting unbounded capacity") | ||
self._capacity = capacity if capacity is not None and capacity > 0 else maxsize | ||
if array and array[0] is not None: | ||
for item in array[:self._capacity]: | ||
self.enqueue(item) | ||
|
||
def is_empty(self) -> bool: | ||
return self.head is None | ||
|
||
def is_full(self) -> bool: | ||
return self._rear == self._capacity | ||
|
||
def enqueue(self, data: Any) -> int: | ||
if self.is_full(): | ||
print("WARNING: THE QUEUE IS ALREADY FULL, CANT ENQUEUE ANY FURTHER") | ||
return -1 | ||
super().append(data) | ||
self._rear += 1 | ||
return 0 | ||
|
||
def dequeue(self) -> Union[int, Any]: | ||
if self.is_empty(): | ||
print("WARNING: THE QUEUE IS ALREADY EMPTY, CANT DEQUEUE ANY FURTHER") | ||
return -1 | ||
# Dequeue Operation | ||
deleted_node = self.head | ||
deleted_item = deleted_node.data | ||
self._head = self.head.next | ||
self._rear -= 1 | ||
return deleted_item | ||
|
||
def peek(self) -> str: | ||
if self.is_empty(): return "QUEUE EMPTY" | ||
return str(self._tail.data if self._tail else None) | ||
|
||
def append(self, data: Any) -> None: | ||
print("WARNING: Method not available here.") | ||
|
||
def insert(self, data: Any) -> None: | ||
print("WARNING: Method not implemented here." | ||
"Please use enqueue method to insert") | ||
|
||
def __str__(self, head: Node = None): | ||
def maximum_breadth(ref: Optional[Node]) -> int: | ||
result = 0 | ||
while ref: | ||
result = max(len(str(ref.data)), result) | ||
ref = ref.next | ||
return result | ||
|
||
if self.is_empty(): return '╔═══════════════════╗\n' \ | ||
'║ QUEUE EMPTY ║\n' \ | ||
'╚═══════════════════╝' | ||
padding = 4 | ||
max_breadth = maximum_breadth(self.head) + padding | ||
middle_part = 'FRONT -> ' | ||
upper_part = f"{' ' * (len(middle_part) - 1)} " | ||
lower_part = f"{' ' * (len(middle_part) - 1)} " | ||
temp = self.head | ||
while temp: | ||
item = temp.data | ||
upper_part += f"╔{'═' * max_breadth}╗ " | ||
middle_part += f'║{str(item).center(max_breadth)}║ <-' | ||
lower_part += f"╚{'═' * max_breadth}╝ " | ||
temp = temp.next | ||
upper_part = f"{upper_part[:-1]}\n" | ||
middle_part += ' REAR\n' | ||
lower_part = f"{lower_part[:-1]}\n" | ||
|
||
return upper_part + middle_part + lower_part |
Oops, something went wrong.