diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 8bf39132c..15c319751 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -64,3 +64,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her - K. Shudipto Amin - Peanutbutter_Warrior - Thijs Raymakers +- Michael Ciccotosto-Camp diff --git a/contents/stacks_and_queues/code/python/queue.py b/contents/stacks_and_queues/code/python/queue.py new file mode 100644 index 000000000..ec68a0839 --- /dev/null +++ b/contents/stacks_and_queues/code/python/queue.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +__author__ = "Michael Ciccotosto-Camp" + +from typing import TypeVar, Generic + + +T = TypeVar("T") + + +class Queue(Generic[T]): + def __init__(self) -> None: + self.__list: list[T] = list() + + def dequeue(self) -> T: + return self.__list.pop(0) + + def enqueue(self, element: T) -> int: + self.__list.append(element) + return len(self) + + def front(self) -> T: + return self.__list[0] + + def __len__(self) -> int: + return len(self.__list) + + def __str__(self) -> str: + return str(self.__list) + + +def main() -> None: + int_queue: Queue[int] = Queue() + + int_queue.enqueue(4) + int_queue.enqueue(5) + int_queue.enqueue(9) + + print(int_queue.dequeue()) + print(len(int_queue)) + print(int_queue.front()) + + +if __name__ == "__main__": + main() diff --git a/contents/stacks_and_queues/code/python/stack.py b/contents/stacks_and_queues/code/python/stack.py new file mode 100644 index 000000000..5f890baa3 --- /dev/null +++ b/contents/stacks_and_queues/code/python/stack.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python3 + +__author__ = "Michael Ciccotosto-Camp" + +from typing import TypeVar, Generic + + +T = TypeVar("T") + + +class Stack(Generic[T]): + def __init__(self) -> None: + self.__list: list[T] = [] + + def pop(self) -> T: + return self.__list.pop() + + def push(self, element: T) -> int: + self.__list.append(element) + return len(self) + + def top(self) -> T: + return self.__list[-1] + + def __len__(self) -> int: + return len(self.__list) + + def __str__(self) -> str: + return str(self.__list) + + +def main() -> None: + int_stack: Stack[int] = Stack() + + int_stack.push(4) + int_stack.push(5) + int_stack.push(9) + + print(int_stack.pop()) + print(len(int_stack)) + print(int_stack.top()) + + +if __name__ == "__main__": + main() diff --git a/contents/stacks_and_queues/stacks_and_queues.md b/contents/stacks_and_queues/stacks_and_queues.md index b695f53bd..e63f72a41 100644 --- a/contents/stacks_and_queues/stacks_and_queues.md +++ b/contents/stacks_and_queues/stacks_and_queues.md @@ -7,6 +7,7 @@ In *stacks*, data follows *Last In, First Out* (LIFO), which basically means tha In *Queues*, data follows *First In, First Out* (FIFO), which means that whichever element you put in first will be the first element you take out. Imagine a queue of people. It would be unfair if the first person in line for groceries were not the first person to receive attention once the attendant finally shows up. For the most part, though, queues and stacks are treated the same way. There must be a way to: + 1. look at the first element (`top()`) 2. to remove the first element (`pop()`) 3. to push elements onto the data structure (`push()`) @@ -14,6 +15,7 @@ For the most part, though, queues and stacks are treated the same way. There mus The notation for this depends on the language you are using. Queues, for example, will often use `dequeue()` instead of `pop()` and `front()` instead of `top()`. You will see the language-specific details in the source code under the algorithms in this book, so for now it's simply important to know what stacks and queues are and how to access elements held within them. ## Example Code + Here is a simple implementation of a stack: {% method %} {% sample lang="ts" %} @@ -24,6 +26,8 @@ Here is a simple implementation of a stack: [import, lang:"cpp"](code/cpp/stack.cpp) {% sample lang="rust" %} [import, lang:"rust"](code/rust/Stack.rs) +{% sample lang="python" %} +[import, lang:"python"](code/python/stack.py) {% endmethod %} Here is a simple implementation of a queue: @@ -36,9 +40,10 @@ Here is a simple implementation of a queue: [import, lang:"cpp"](code/cpp/queue.cpp) {% sample lang="rust" %} [import, lang:"rust" ](code/rust/Queue.rs) +{% sample lang="python" %} +[import, lang:"python"](code/python/queue.py) {% endmethod %} - ## License ##### Code Examples @@ -54,4 +59,5 @@ The text of this chapter was written by [James Schloss](https://github.com/leios ##### Pull Requests After initial licensing ([#560](https://github.com/algorithm-archivists/algorithm-archive/pull/560)), the following pull requests have modified the text or graphics of this chapter: + - none