Skip to content

Commit

Permalink
Full recode
Browse files Browse the repository at this point in the history
  • Loading branch information
TheAihopGG committed May 31, 2024
1 parent 3883972 commit f626133
Showing 1 changed file with 16 additions and 57 deletions.
73 changes: 16 additions & 57 deletions ufpy/ustack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())

def __nonzero__(self) -> bool:
return len(self.__list) == 0

0 comments on commit f626133

Please sign in to comment.