From f626133d581a41bc816c525ae3b503d849ebc79d Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Fri, 31 May 2024 23:08:47 +0300 Subject: [PATCH] Full recode --- ufpy/ustack.py | 73 +++++++++++--------------------------------------- 1 file changed, 16 insertions(+), 57 deletions(-) diff --git a/ufpy/ustack.py b/ufpy/ustack.py index b1dddec..06a5aab 100644 --- a/ufpy/ustack.py +++ b/ufpy/ustack.py @@ -2,66 +2,29 @@ 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: + 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 + self.__list = __list - 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(): + def pop(self) -> Any: + return self.__list.pop() + + @property + def top(self) -> Any | bool: + if self: return self.__list[-1] else: - return None + return False + + @property + def empty(self) -> bool: + return len(self.__list) == 0 def __add__(self, stack: UStack) -> UStack: return UStack(self.__list + stack.__list) @@ -71,10 +34,6 @@ def __radd__(self, stack: UStack) -> UStack: 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()) \ No newline at end of file + + def __nonzero__(self) -> bool: + return len(self.__list) == 0 \ No newline at end of file