From 158a803f07709aaa344348d9a1eea1b4a0828fbc Mon Sep 17 00:00:00 2001 From: hopTech <108916224+TheHakerTech@users.noreply.github.com> Date: Mon, 1 Jul 2024 07:59:40 +0300 Subject: [PATCH 01/16] Create deque.py Demo --- ufpy/deque.py | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 ufpy/deque.py diff --git a/ufpy/deque.py b/ufpy/deque.py new file mode 100644 index 0000000..2b5f012 --- /dev/null +++ b/ufpy/deque.py @@ -0,0 +1,88 @@ +from __future__ import annotations +from typing import TypeVar, Generic, Iterable, Any + +__all__ = ( + "UDeque" +) + +VT = TypeVar("VT") + +class UDeque(Generic[VT]): + def __init__(self, __list: Iterable[VT] = []): + self.__list = list(__list) + + def addend(self, value: VT): + self.__list.append(value) + + def addbegin(self, value: VT): + self.__list.insert(0, value) + + def popend(self): + return self.__list.pop() + + def popbegin(self): + return self.__list.pop(0) + + def setend(self, value: VT): + self[1] = value + + def setbegin(self, value: VT): + self[0] = value + + def end(self) -> VT: + if self: + return self.__list[-1] + else: + return None + + def begin(self) -> VT: + if self: + return self.__list[0] + else: + return None + + def copy(self) -> UDeque: + return UDeque(self.__list.copy()) + + def is_empty(self) -> bool: + return len(self) == 0 + + def reversed(self) -> UDeque: + return self.__reversed__() + + def __len__(self) -> int: + return len(self.__list) + + def __nonzero__(self) -> bool: + return not self.is_empty() + + def __getitem__(self, index: Any) -> VT: + if isinstance(index, int): + if index in (0, 1): + return self.__list[index] + else: + raise IndexError(f"{index} out of range (0, 1)") + + def __setitem__(self, index: int, value: VT): + if isinstance(index, int): + if index == 0: + self.__list[index] = value + elif index == 1: + self.__list[-index] = value + else: + raise IndexError(f"{index} out of range (0, 1)") + + def __delitem__(self, index: int): + if isinstance(index, int): + if index == 0: + del self.__list[index] + elif index == 1: + del self.__list[-index] + else: + raise IndexError(f"{index} out of range (0, 1)") + + def __reversed__(self) -> UDeque: + return UDeque((self.end(), self.begin())) + + def __str__(self) -> str: + return str(self.__list) From 5ea427128ed2ec348468b2d12e086b440a6e7407 Mon Sep 17 00:00:00 2001 From: hopTech <108916224+TheHakerTech@users.noreply.github.com> Date: Mon, 1 Jul 2024 08:00:04 +0300 Subject: [PATCH 02/16] Rename deque.py to udeque.py fixed name --- ufpy/{deque.py => udeque.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ufpy/{deque.py => udeque.py} (100%) diff --git a/ufpy/deque.py b/ufpy/udeque.py similarity index 100% rename from ufpy/deque.py rename to ufpy/udeque.py From 3346d90cb50d8833d1cbd2ab829d991a99431dac Mon Sep 17 00:00:00 2001 From: hopTech <108916224+TheHakerTech@users.noreply.github.com> Date: Mon, 1 Jul 2024 08:01:23 +0300 Subject: [PATCH 03/16] Create udeque.md demo --- examples/udeque.md | 105 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 examples/udeque.md diff --git a/examples/udeque.md b/examples/udeque.md new file mode 100644 index 0000000..f991220 --- /dev/null +++ b/examples/udeque.md @@ -0,0 +1,105 @@ +# UDeque + +## Introduction + +UDeque is class which is simplifying working with deque. +It has many methods, properties, magic methods. + +Firstly, import `UDeque` from `ufpy` +```python +from ufpy import UDeque +``` + +## Create UDeque + +For creating UDeque you should use this code: +```python +d = UDeque((1, 2, 3, 4, 5)) +``` + +## Get end/begin + +To get end you can use: +```python +d[0] # 1 +d.end() # 1 +``` + +To get begin you can use: +```python +d[1] # 5 +d.begin() # 5 +``` + +> [!NOTE] +> 0 - first element +> 1 - last element + +## Add end/begin + +To add end you can use +```python +d.addend(6) # [1, 2, 3, 4, 5, 6] +``` + +To add begin you can use +```python +d.addbegin(0) # [0, 1, 2, 3, 4, 5, 6] +``` + +## Set end/begin + +To set end you can use +```python +d.setend(7) # [0, 1, 2, 3, 4, 5, 7] +``` + +To set begin you can use +```python +d.setbegin(7) # [7, 1, 2, 3, 4, 5, 7] +``` +> [!NOTE] +> Also you can use indexes: +> ```python +> d[0] = 1 # [1, 1, 2, 3, 4, 5, 7] +> d[1] = 1 # [1, 1, 2, 3, 4, 5, 1] +> ``` +> But it is undesirable to use + +## Delete end/begin + +For deleting end use +```python +d.popend() # [7, 1, 2, 3, 4, 5] +``` + +For deleting begin use +```python +d.popbegin(7) # [1, 2, 3, 4, 5] +``` + +> [!NOTE] +> To delete end or begin you also can use `del`: +> ```python +> del d[0] # [2, 3, 4, 5] +> del d[1] # [2, 3, 4] +> ``` +> But it is undesirable to use + +## Get length of deque + +You can get length of dict using inbuilt `len()` function + +```python +d2 = UDeque((1, 2, 3, 4, 5)) +len(d2) # 5 +``` + +## Reserve +To reserve deque's end and begin use inbuilt `reserved()` function +```python +reserved(d2) # [5, 1] +``` + +> [!NOTE] +> `reserved()` returns `[end, begin]` From ec04cc1f4715e13bbe2abea7857109011be01c37 Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Mon, 1 Jul 2024 11:49:34 +0300 Subject: [PATCH 04/16] Demo test --- ufpy/udeque.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 ufpy/udeque.py diff --git a/ufpy/udeque.py b/ufpy/udeque.py new file mode 100644 index 0000000..0bb38a3 --- /dev/null +++ b/ufpy/udeque.py @@ -0,0 +1,88 @@ +from __future__ import annotations +from typing import TypeVar, Generic, Iterable, Any + +__all__ = ( + "UDeque" +) + +VT = TypeVar("VT") + +class UDeque(Generic[VT]): + def __init__(self, __list: Iterable[VT] = []): + self.__list = list(__list) + + def addend(self, value: VT): + self.__list.append(value) + + def addbegin(self, value: VT): + self.__list.insert(0, value) + + def popend(self): + return self.__list.pop() + + def popbegin(self): + return self.__list.pop(0) + + def setend(self, value: VT): + self[1] = value + + def setbegin(self, value: VT): + self[0] = value + + def end(self) -> VT: + if self: + return self.__list[-1] + else: + return None + + def begin(self) -> VT: + if self: + return self.__list[0] + else: + return None + + def copy(self) -> UDeque: + return UDeque(self.__list.copy()) + + def is_empty(self) -> bool: + return len(self) == 0 + + def reversed(self) -> UDeque: + return self.__reversed__() + + def __len__(self) -> int: + return len(self.__list) + + def __nonzero__(self) -> bool: + return not self.is_empty() + + def __getitem__(self, index: Any) -> VT: + if isinstance(index, int): + if index in (0, 1): + return self.__list[index] + else: + raise IndexError(f"{index} out of range (0, 1)") + + def __setitem__(self, index: int, value: VT): + if isinstance(index, int): + if index == 0: + self.__list[index] = value + elif index == 1: + self.__list[-index] = value + else: + raise IndexError(f"{index} out of range (0, 1)") + + def __delitem__(self, index: int): + if isinstance(index, int): + if index == 0: + del self.__list[index] + elif index == 1: + del self.__list[-index] + else: + raise IndexError(f"{index} out of range (0, 1)") + + def __reversed__(self) -> UDeque: + return UDeque((self.end(), self.begin())) + + def __str__(self) -> str: + return str(self.__list) \ No newline at end of file From 13704038f492eb1b0c8ef5ea67ecbeb03d6629f2 Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Mon, 1 Jul 2024 11:50:26 +0300 Subject: [PATCH 05/16] Demo examples --- examples/udeque.md | 106 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 examples/udeque.md diff --git a/examples/udeque.md b/examples/udeque.md new file mode 100644 index 0000000..5b96859 --- /dev/null +++ b/examples/udeque.md @@ -0,0 +1,106 @@ +# UDeque + +## Introduction + +UDeque is class which is simplifying working with deque. +It has many methods, properties, magic methods. + +Firstly, import `UDeque` from `ufpy` +```python +from ufpy import UDeque +``` + +## Create UDeque + +For creating UDeque you should use this code: +```python +d = UDeque((1, 2, 3, 4, 5)) +``` + +## Get end/begin + +To get end you can use: +```python +d[0] # 1 +d.end() # 1 +``` + +To get begin you can use: +```python +d[1] # 5 +d.begin() # 5 +``` + +> [!NOTE] +> 0 - first element +> 1 - last element + +## Add end/begin + +To add end you can use +```python +d.addend(6) # [1, 2, 3, 4, 5, 6] +``` + +To add begin you can use +```python +d.addbegin(0) # [0, 1, 2, 3, 4, 5, 6] +``` + +## Set end/begin + +To set end you can use +```python +d.setend(7) # [0, 1, 2, 3, 4, 5, 7] +``` + +To set begin you can use +```python +d.setbegin(7) # [7, 1, 2, 3, 4, 5, 7] +``` +> [!NOTE] +> Also you can use indexes: +> ```python +> d[0] = 1 # [1, 1, 2, 3, 4, 5, 7] +> d[1] = 1 # [1, 1, 2, 3, 4, 5, 1] +> ``` +> But it is undesirable to use + +## Delete end/begin + +For deleting end use +```python +d.popend() # [7, 1, 2, 3, 4, 5] +``` + +For deleting begin use +```python +d.popbegin(7) # [1, 2, 3, 4, 5] +``` + +> [!NOTE] +> To delete end or begin you also can use `del`: +> ```python +> del d[0] # [2, 3, 4, 5] +> del d[1] # [2, 3, 4] +> ``` +> But it is undesirable to use + +## Get length of deque + +You can get length of dict using inbuilt `len()` function + +```python +d2 = UDeque((1, 2, 3, 4, 5)) +len(d2) # 5 +``` + +## Reserve +To reserve deque's end and begin use inbuilt `reserved()` function +```python +reserved(d2) # [5, 1] +``` + +> [!NOTE] +> `reserved()` returns `[end, begin]` +Что написать ещё \ No newline at end of file From 3e9d81135a97642c96447ef2f06794eb07b80704 Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Mon, 1 Jul 2024 18:18:14 +0300 Subject: [PATCH 06/16] UPair DEMOTEST --- examples/upair.md | 72 +++++++++++++++++++++++++++++++++++++++++++++++ ufpy/upair.py | 53 ++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 examples/upair.md create mode 100644 ufpy/upair.py diff --git a/examples/upair.md b/examples/upair.md new file mode 100644 index 0000000..4ddae93 --- /dev/null +++ b/examples/upair.md @@ -0,0 +1,72 @@ +# UPair + +## Introduction + +UDeque is class which is simplifying working with deque. +It has many methods, properties, magic methods. + +Firstly, import `UPair` from `ufpy` +```python +from ufpy import UPair +``` + +## Create UPair + +For creating UPair you should use this code: +```python +p = UPair(first="first", second="second") +``` + +## Get first/second + +To get the first value you can use: +```python +p[0] # first +p.first # first +``` + +To get the second value you can use: +```python +p[1] # second +p.second # second +``` + +> [!NOTE] +> 0 - the first value +> 1 - the second value + +## Set first/second + +To set the first value you can use +```python +p.first = "FIRST" # FIRST +``` + +Use the same way to set the second value + +> [!NOTE] +> Also you can use indexes: +> ```python +> p[0] = "FIRST" +> p[1] = "SECOND" +> ``` +> But it is undesirable to use + +## Get length of pair + +You can get the length of the pair using the built-in len() function. + +```python +p2 = UPair(1, 2) +len(p2) # 2 +``` + +> [!NOTE] +> If `first == None` and `second == "second"` `len()` returns 1 +> If `first == None` and `second == None` `len()` returns 0 + +## Reserve +To reserve the pair's the first and the second value, use the built-in `reserved()` function +```python +reserved(p2) # ["second", "first"] +``` \ No newline at end of file diff --git a/ufpy/upair.py b/ufpy/upair.py new file mode 100644 index 0000000..3d8998c --- /dev/null +++ b/ufpy/upair.py @@ -0,0 +1,53 @@ +from __future__ import annotations +from typing import Generic, TypeVar, Any + +__all__ = ( + "UPair" +) + +VT = TypeVar("VT") + +class UPair(Generic[VT]): + def __init__(self, __first: VT = None, __second: VT = None): + self.first = __first + self.second = __second + + def is_empty(self) -> bool: + return len(self) == 0 + + def reversed(self) -> UPair: + return self.__reversed__() + + def __setattr__(self, __name: str, __value: Any): + if __name in ("first", "second"): + self.__dict__[__name] = __value + else: + raise KeyError(f"Key not found: {__name}") + + def __getitem__(self, index: int): + if isinstance(index, int): + if index in (0, 1): + return [self.first, self.second][index] + else: + raise IndexError(f"{index} out of range (0, 1)") + + def __setitem__(self, index: int, value: VT): + if isinstance(index, int): + if index == 0: + self.first = value + elif index == 1: + self.second = value + else: + raise IndexError(f"{index} out of range (0, 1)") + + def __len__(self) -> int: + return sum(map(lambda var: 1 if var else 0, (self.first, self.second))) + + def __nonzero__(self) -> bool: + return not self.is_empty() + + def __str__(self) -> str: + return str([self.first, self.second]) + + def __reversed__(self) -> UPair: + return UPair(self.first, self.second) \ No newline at end of file From 535e603013a677a8db0107c00b6f515e92f7b420 Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Mon, 1 Jul 2024 18:20:40 +0300 Subject: [PATCH 07/16] Fixed 11 line: **__list -> *__list --- ufpy/udeque.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ufpy/udeque.py b/ufpy/udeque.py index 602b485..428da64 100644 --- a/ufpy/udeque.py +++ b/ufpy/udeque.py @@ -8,7 +8,7 @@ VT = TypeVar("VT") class UDeque(Generic[VT]): - def __init__(self, **__list): + def __init__(self, *__list): self.__list = list(__list) def addend(self, value: VT) -> VT: @@ -101,4 +101,8 @@ def __reversed__(self) -> UDeque: return UDeque((self.end(), self.begin())) def __str__(self) -> str: - return str(self.__list) \ No newline at end of file + return str(self.__list) + +d = UDeque() +d2 = UDeque(1, 2, 3) +print(d2.end()) \ No newline at end of file From c63432f206f5e665647fc56eb3760d40b54f4a3f Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Mon, 1 Jul 2024 18:26:47 +0300 Subject: [PATCH 08/16] Fixed __all__ --- ufpy/udeque.py | 2 +- ufpy/upair.py | 2 +- ufpy/uqueue.py | 0 3 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 ufpy/uqueue.py diff --git a/ufpy/udeque.py b/ufpy/udeque.py index 428da64..dabd9dc 100644 --- a/ufpy/udeque.py +++ b/ufpy/udeque.py @@ -2,7 +2,7 @@ from typing import TypeVar, Generic, Any __all__ = ( - "UDeque" + "UDeque", ) VT = TypeVar("VT") diff --git a/ufpy/upair.py b/ufpy/upair.py index 3d8998c..af34878 100644 --- a/ufpy/upair.py +++ b/ufpy/upair.py @@ -2,7 +2,7 @@ from typing import Generic, TypeVar, Any __all__ = ( - "UPair" + "UPair", ) VT = TypeVar("VT") diff --git a/ufpy/uqueue.py b/ufpy/uqueue.py new file mode 100644 index 0000000..e69de29 From 6e2634b9ff51199b59cdadb5961c8c29164eaf8b Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Mon, 1 Jul 2024 19:00:15 +0300 Subject: [PATCH 09/16] Added uqueue --- examples/uqueue.md | 0 ufpy/uqueue.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 examples/uqueue.md diff --git a/examples/uqueue.md b/examples/uqueue.md new file mode 100644 index 0000000..e69de29 diff --git a/ufpy/uqueue.py b/ufpy/uqueue.py index e69de29..84cbfb5 100644 --- a/ufpy/uqueue.py +++ b/ufpy/uqueue.py @@ -0,0 +1,55 @@ +from __future__ import annotations +from typing import Generic, TypeVar, Any + +__all__ = ( + "UQueue", +) + +VT = TypeVar("VT") + +class UQueue(Generic[VT]): + def __init__(self, *__list): + self.__list: list[VT] = list(__list) + self.__index = 0 + + def pop(self) -> VT: + return self.__list.pop(0) + + def push(self, value: VT) -> VT: + self.__list.append(value) + return value + + @property + def head(self) -> None: + return self.__list[-1] if self else None + + def copy(self) -> UQueue: + return UQueue(self.__list.copy()) + + def set_head(self, value: VT) -> VT: + if self: + self.__list[-1] = value + return value + else: + raise IndexError("Index out of range") + + def is_empty(self) -> bool: + return len(self) == 0 + + def __len__(self) -> int: + return len(self.__list) + + def __nonzero__(self) -> bool: + return not self.is_empty() + + def __str__(self) -> str: + return str(self.__list) + + def __iter__(self) -> UQueue: + return self + + def __next__(self) -> VT: + if self.is_empty(): + raise StopIteration + else: + return self.__list.pop(0) \ No newline at end of file From aa3d9d96f3cc4faf80a0f2ee63dd4755265ec9cd Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Mon, 1 Jul 2024 19:12:48 +0300 Subject: [PATCH 10/16] Demo test --- examples/uqueue.md | 78 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/examples/uqueue.md b/examples/uqueue.md index e69de29..6a42837 100644 --- a/examples/uqueue.md +++ b/examples/uqueue.md @@ -0,0 +1,78 @@ +# UQueue +## Introduction + +UQueue is class which is simplifying working with queue. +It has many methods, properties, magic methods. + +Firstly, import `UQueue` from `ufpy` +```python +from ufpy import UQueue +``` + +## Create UQueue + +For creating UQueue you should use this code: +```python +q = UQueue(1, 2, 3, 4, 5) +``` + +## Get head + +To get head of the queue use: +```python +q.head # 5 +``` + +## Add value + +To add value you can use +```python +q.push(6) # [1, 2, 3, 4, 5, 6] +``` + +## Set head + +To set end you can use +```python +q.set_head(7) # [1, 2, 3, 4, 5, 7] +``` + +## Delete value + +For deleting the first element use +```python +q.pop() # [2, 3, 4, 5, 7] +``` + +## Get length of queue + +You can get the length of the queue using the built-in len() function. + +```python +q2 = UQueue(1, 2, 3, 4, 5) +len(q2) # 5 +``` + +## Reserve +To reserve the queue's end and beginning, use the built-in `reserved()` function +```python +reserved(q2) # [5, 4, 3, 2, 1] +``` + +## Iteration +`UQueue()` suppots iteration: +```python +for var in q2: + print(var) +# 1 +# 2 +# 3 +# 4 +# 5 +``` + +> [!NOTE] +> After using `for` your queue will be empty: +> ```python +> print(q2) # [] +> ``` \ No newline at end of file From a94d52604fe55a2dd35af257a88df9fa555196fc Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Mon, 1 Jul 2024 19:13:32 +0300 Subject: [PATCH 11/16] Fixed some words --- examples/upair.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/upair.md b/examples/upair.md index 4ddae93..436ee6a 100644 --- a/examples/upair.md +++ b/examples/upair.md @@ -2,7 +2,7 @@ ## Introduction -UDeque is class which is simplifying working with deque. +UDeque is class which is simplifying working with pair. It has many methods, properties, magic methods. Firstly, import `UPair` from `ufpy` From dee4e381d21a4fb4cd95ec5e0ad3cf96d0f8a594 Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Mon, 1 Jul 2024 19:13:54 +0300 Subject: [PATCH 12/16] Fixed some words --- examples/udeque.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/udeque.md b/examples/udeque.md index fbfb590..53034ff 100644 --- a/examples/udeque.md +++ b/examples/udeque.md @@ -1,5 +1,4 @@ # UDeque - ## Introduction UDeque is class which is simplifying working with deque. @@ -14,7 +13,7 @@ from ufpy import UDeque For creating UDeque you should use this code: ```python -d = UDeque((1, 2, 3, 4, 5)) +d = UDeque(1, 2, 3, 4, 5) ``` ## Get end/begin @@ -91,7 +90,7 @@ d.popbegin(7) # [1, 2, 3, 4, 5] You can get the length of the deque using the built-in len() function. ```python -d2 = UDeque((1, 2, 3, 4, 5)) +d2 = UDeque(1, 2, 3, 4, 5) len(d2) # 5 ``` From 59c54f407423a514177971164eff7ad3a0d12030 Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Mon, 1 Jul 2024 19:14:41 +0300 Subject: [PATCH 13/16] Little fix --- ufpy/uqueue.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ufpy/uqueue.py b/ufpy/uqueue.py index 84cbfb5..0872b32 100644 --- a/ufpy/uqueue.py +++ b/ufpy/uqueue.py @@ -52,4 +52,7 @@ def __next__(self) -> VT: if self.is_empty(): raise StopIteration else: - return self.__list.pop(0) \ No newline at end of file + return self.__list.pop(0) + + def __reserved__(self) -> UQueue: + return UQueue(*self.__list) \ No newline at end of file From feae10427cfba586b37402255a99d1f4ceb68f86 Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Mon, 1 Jul 2024 19:21:15 +0300 Subject: [PATCH 14/16] Added iteration --- ufpy/udeque.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ufpy/udeque.py b/ufpy/udeque.py index dabd9dc..a876a3a 100644 --- a/ufpy/udeque.py +++ b/ufpy/udeque.py @@ -103,6 +103,12 @@ def __reversed__(self) -> UDeque: def __str__(self) -> str: return str(self.__list) -d = UDeque() -d2 = UDeque(1, 2, 3) -print(d2.end()) \ No newline at end of file + def __iter__(self) -> UDeque: + return self + + def __next__(self) -> VT: + if self.is_empty(): + raise StopIteration + else: + value = self.popbegin() + return value \ No newline at end of file From 5ec7eb728df99eca93cdd8c7f7b6d112adace3c0 Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Mon, 1 Jul 2024 19:23:15 +0300 Subject: [PATCH 15/16] Added iteration documentation --- examples/udeque.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/examples/udeque.md b/examples/udeque.md index 53034ff..88596c6 100644 --- a/examples/udeque.md +++ b/examples/udeque.md @@ -102,3 +102,21 @@ reserved(d2) # [5, 1] > [!NOTE] > `reserved()` returns `[end, begin]` + +## Iteration +`UDeque()` suppots iteration: +```python +for var in d2: + print(var) +# 1 +# 2 +# 3 +# 4 +# 5 +``` + +> [!NOTE] +> After using `for` your deque will be empty: +> ```python +> print(d2) # [] +> ``` \ No newline at end of file From 01e3382c6aa4e893bc5179546cabe7ec7600ee57 Mon Sep 17 00:00:00 2001 From: TheHakerTech Date: Tue, 2 Jul 2024 17:27:49 +0300 Subject: [PATCH 16/16] Fixed --- examples/udeque.md | 2 +- examples/uqueue.md | 9 ++++++- ufpy/udeque.py | 60 ++++++++++++++++++---------------------------- ufpy/uqueue.py | 22 +++++++++++------ 4 files changed, 47 insertions(+), 46 deletions(-) diff --git a/examples/udeque.md b/examples/udeque.md index 88596c6..f1916b5 100644 --- a/examples/udeque.md +++ b/examples/udeque.md @@ -115,7 +115,7 @@ for var in d2: # 5 ``` -> [!NOTE] +> [!WARNING] > After using `for` your deque will be empty: > ```python > print(d2) # [] diff --git a/examples/uqueue.md b/examples/uqueue.md index 6a42837..1727319 100644 --- a/examples/uqueue.md +++ b/examples/uqueue.md @@ -44,6 +44,13 @@ For deleting the first element use q.pop() # [2, 3, 4, 5, 7] ``` +> [!NOTE] +> To delete end or begin you also can use `del [-1]`: +> ```python +> del q[-1] # [2, 3, 4, 5] +> ``` +> But it is undesirable to use + ## Get length of queue You can get the length of the queue using the built-in len() function. @@ -71,7 +78,7 @@ for var in q2: # 5 ``` -> [!NOTE] +> [!WARNING] > After using `for` your queue will be empty: > ```python > print(q2) # [] diff --git a/ufpy/udeque.py b/ufpy/udeque.py index a876a3a..ce979a2 100644 --- a/ufpy/udeque.py +++ b/ufpy/udeque.py @@ -9,47 +9,41 @@ class UDeque(Generic[VT]): def __init__(self, *__list): - self.__list = list(__list) + self.__list: list[VT] = list(__list) def addend(self, value: VT) -> VT: - if self: - self.__list.append(value) - return value - else: - raise IndexError("index out of range") + self.__list.append(value) + return value def addbegin(self, value: VT) -> VT: - if self: - self.__list.insert(0, value) - return value - else: - raise IndexError("index out of range") + self.__list.insert(0, value) + return value def popend(self) -> VT: if self: return self.__list.pop() else: - raise IndexError("index out of range") + raise IndexError("list index out of range") def popbegin(self) -> VT: if self: return self.__list.pop(0) else: - raise IndexError("index out of range") + raise IndexError("list index out of range") def setend(self, value: VT) -> VT: if self: self[-1] = value return value else: - raise IndexError("index out of range") + raise IndexError("list index out of range") def setbegin(self, value: VT) -> VT: if self: self[0] = value return value else: - raise IndexError("index out of range") + raise IndexError("list index out of range") def end(self) -> Any: return self.__list[-1] if self else None @@ -73,29 +67,22 @@ def __nonzero__(self) -> bool: return not self.is_empty() def __getitem__(self, index: Any) -> VT: - if isinstance(index, int): - if index in (0, 1): - return self.__list[index] - else: - raise IndexError(f"{index} out of range (0, 1)") + if index in (0, -1): + return self.__list[index] + else: + raise IndexError(f"{index} out of range") def __setitem__(self, index: int, value: VT): - if isinstance(index, int): - if index == 0: - self.__list[index] = value - elif index == 1: - self.__list[-index] = value - else: - raise IndexError(f"{index} out of range (0, 1)") + if index in (0, -1): + self.__list[index] = value + else: + raise IndexError(f"{index} out of range") def __delitem__(self, index: int): - if isinstance(index, int): - if index == 0: - del self.__list[index] - elif index == 1: - del self.__list[-index] - else: - raise IndexError(f"{index} out of range (0, 1)") + if index in (0, -1): + del self.__list[index] + else: + raise IndexError(f"{index} out of range") def __reversed__(self) -> UDeque: return UDeque((self.end(), self.begin())) @@ -105,10 +92,9 @@ def __str__(self) -> str: def __iter__(self) -> UDeque: return self - + def __next__(self) -> VT: if self.is_empty(): raise StopIteration else: - value = self.popbegin() - return value \ No newline at end of file + return self.popbegin() \ No newline at end of file diff --git a/ufpy/uqueue.py b/ufpy/uqueue.py index 0872b32..6cf2a36 100644 --- a/ufpy/uqueue.py +++ b/ufpy/uqueue.py @@ -10,7 +10,6 @@ class UQueue(Generic[VT]): def __init__(self, *__list): self.__list: list[VT] = list(__list) - self.__index = 0 def pop(self) -> VT: return self.__list.pop(0) @@ -27,11 +26,8 @@ def copy(self) -> UQueue: return UQueue(self.__list.copy()) def set_head(self, value: VT) -> VT: - if self: - self.__list[-1] = value - return value - else: - raise IndexError("Index out of range") + self.__list.insert(-1, value) + return value def is_empty(self) -> bool: return len(self) == 0 @@ -55,4 +51,16 @@ def __next__(self) -> VT: return self.__list.pop(0) def __reserved__(self) -> UQueue: - return UQueue(*self.__list) \ No newline at end of file + return UQueue(*self.__list) + + def __getitem__(self, index: int) -> VT: + if index == -1: + return self.__list[index] + else: + raise IndexError(f"{index} out of range") + + def __delitem__(self, index: int): + if index == -1: + del self.__list[index] + else: + raise IndexError(f"{index} out of range") \ No newline at end of file