-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9dddaf9
commit 8905905
Showing
1 changed file
with
80 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# -*- encodibg: utf-8 -*- | ||
from __future__ import annotations | ||
from typing import Any | ||
|
||
class UStackElement(): | ||
def __init__(self, value: Any) -> UStackElement: | ||
""" | ||
UStackElement is using like an element in UStack. | ||
""" | ||
self.__value: Any = value | ||
|
||
def __len__(self): | ||
return len(self.__value) | ||
|
||
def __str__(self): | ||
return str(self.__value) | ||
|
||
def __int__(self): | ||
return int(self.__value) | ||
|
||
def __bool__(self): | ||
return bool(self.__value) | ||
|
||
def __dict__(self): | ||
return dict(self.__value) | ||
|
||
def __bytes__(self, encoding: str = 'utf-8'): | ||
return bytes(self.__value, encoding) | ||
|
||
class UStack(): | ||
def __init__(self, _list: list | tuple = list()) -> UStack: | ||
""" | ||
UStack's methods: | ||
append(**items) - to add item to begin. | ||
empty() - true if stack is empty. | ||
pop() - delete the last element in stack. | ||
""" | ||
if len(_list) != 0: | ||
self.__list = [UStackElement(item) for item in _list] | ||
else: | ||
self.__list = _list | ||
|
||
def append(self, *items) -> UStackElement: | ||
for item in items: | ||
item = UStackElement(item) | ||
self.__list.append(item) | ||
return items | ||
|
||
def empty(self) -> bool: | ||
return len(self.__list) == 0 | ||
|
||
def pop(self) -> UStackElement | None: | ||
if not self.empty(): | ||
item: UStackElement = self.__list[-1] | ||
del self.__list[-1] | ||
return item | ||
else: | ||
return None | ||
|
||
def top(self) -> UStackElement | None: | ||
if not self.empty(): | ||
return self.__list[-1] | ||
else: | ||
return None | ||
|
||
def __add__(self, stack: UStack) -> UStack: | ||
return UStack(self.__list + stack.__list) | ||
|
||
def __radd__(self, stack: UStack) -> UStack: | ||
return UStack(stack.__list + self.__list) | ||
|
||
def __len__(self) -> int: | ||
return len(self.__list) | ||
|
||
s = UStack() | ||
s.append(1, 2, 3, 4, 6) | ||
s2 = UStack() | ||
s2.append(1, 2, 3, 4, 5) | ||
s3 = s + s2 | ||
print(s3.top()) |