From e38863ba4231ffd87bac828ab5219fc8df2e6247 Mon Sep 17 00:00:00 2001 From: lucionathan Date: Sat, 26 Oct 2019 13:21:15 -0300 Subject: [PATCH 1/2] queue in python --- Data Structures/Queue/queue.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Data Structures/Queue/queue.py diff --git a/Data Structures/Queue/queue.py b/Data Structures/Queue/queue.py new file mode 100644 index 00000000..cccd8ade --- /dev/null +++ b/Data Structures/Queue/queue.py @@ -0,0 +1,31 @@ +class Queue: + + def __init__(self): + self._array = [] + + def add(self, element): + self._array.append ( element ) + + def peek(self): + ans = None + if not self.isEmpty: + ans = self._array[0] + return ans + + + @property + def isEmpty(self): + return len ( self._array ) == 0 + + + def poll(self): + ans = None + if not self.isEmpty: + ans = self._array[0] + self._leftShift () + self._array.pop() + return ans + + def _leftShift(self): + for i in range ( len ( self._array ) - 1 ): + self._array[i] = self._array[i + 1] From b2adc5e81cf0f4f617ae529a1fbf1c56f7a51c7d Mon Sep 17 00:00:00 2001 From: lucionathan Date: Sat, 26 Oct 2019 13:27:11 -0300 Subject: [PATCH 2/2] counting in py --- Algorithms/Counting_Sort/! | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Algorithms/Counting_Sort/! diff --git a/Algorithms/Counting_Sort/! b/Algorithms/Counting_Sort/! new file mode 100644 index 00000000..e6775eb7 --- /dev/null +++ b/Algorithms/Counting_Sort/! @@ -0,0 +1,30 @@ +import Sorting.Util +class CountingSort: + def sort(self,array): + smaller = min(array) + + keyValue = 0 + if smaller < 0: keyValue = 1 - smaller + + lenArray = len(array) + tempArray = [0] * lenArray + + for i in range(lenArray): + tempArray[i] = array[i] + keyValue + + bigger = max(tempArray) + smaller = min(tempArray) + size = (bigger - smaller) + 2 + + contArray = [0] * size + + for i in range(len(tempArray)): + contArray[tempArray[i] - smaller + 1] += 1 + + for i in range(1, len(contArray)): + contArray[i] += contArray[i - 1] + + + for i in range(len(tempArray)): + array[contArray[tempArray[i] - smaller + 1] -1] = tempArray[i] + contArray[tempArray[i] - smaller + 1] -= 1