Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

rotating circular queue in python

def circularArrayRotation(a, k, queries):
    from collections import deque 
    items = deque(a) 
    items.rotate(k)
    ret_list = []
    for q in queries:
        #print(items[q])
        ret_list.append(items[q])
    return ret_list
Comment

circular queue class python

""" Circular Queue
1. Below is a class implementation

2. Essential features: 
      - [Enqueue] puts items into the list via (tail)
      - [Dequeue] removes items from the list via (head)
      - Wraparound using modulus sign to keep the index...
        ...within the range of the queue
    
3. This is an implementation that doesnt require head and tail to...
   ...have -1 as the default value for an empty list

This is my attempt at shortening the CircularQueue class
Check out the source for more info!
"""
class CircularQueue:
    def __init__(self, max_size, start=0):
        self._queue = [None]*max_size
        self._max_size = max_size
        self._size = 0
        self._head = self._tail = start
    
    def empty(self):
        return self._size == 0
    
    def full(self):
        return self._size == self._max_size
    
    def update_tail(self):
        self._tail = (self._tail + 1) % self._max_size
    
    def update_head(self):
        self._head = (self._head + 1) % self._max_size
        
    def enqueue(self, data): # enqueue
        if self.full():
            return "Full"

        # Put item into queue
        self._queue[self._tail] = data
        self._size += 1
        self.update_tail()
        
        # To check
        return data
    
    def dequeue(self): # dequeue
        if self.empty():
            return "Empty"
        
        # Remove item from queue
        data = self._queue[self._head]
        self._queue[self._head] = None
        self._size -= 1
        self.update_head()
        
        # To check
        return data
    
    def head_at(self):
        return self._head
    
    def tail_at(self):
        return self._tail
    
    def max_size_of(self):
        return self._max_size
    
    def size_of(self):
        return self._size
    
    def display(self):
        return self._queue
    
# Test 
from random import randrange
def CQ_Test(CQ):
    # Create random list
    lst = [randrange(10, 100) for _ in range(10)]
    print(f"List: {lst}", 
          sep = " | ", end = "

")
    
    # Create Empty CQ with length less than lst
    len_CQ = len(lst) - 1
    CQ = CQ(len_CQ)
    print(f"Enq:   ",
          f"used: {CQ.usedslots()}",
          f"H,T: {CQ.head_at()}, {CQ.tail_at()}",
          f"CQ: {CQ.display()}", 
          sep = " | ")   ## Display Empty CQ
    
    # Fill CQ completely
    for item in lst:
        print(f"Enq: {CQ.enq(item)}",
              f"used: {CQ.usedslots()}",
              f"H,T: {CQ.head_at()}, {CQ.tail_at()}",
              f"CQ: {CQ.display()}",
              sep = " | ")
    print()
    
    # Remove half of CQ
    mid = len_CQ // 2
    for i in range(mid):
        print(f"Deq: {CQ.deq()}", 
              f"used: {CQ.usedslots()}",
              f"H,T: {CQ.head_at()}, {CQ.tail_at()}", 
              f"CQ: {CQ.display()}",
              sep = " | ")
    print()
    
    # Fill CQ back to full
    for i in range(mid, len_CQ - 1):
        print(f"Enq: {CQ.enq(lst[i])}", 
              f"used: {CQ.usedslots()}",
              f"H,T: {CQ.head_at()}, {CQ.tail_at()}",
              f"CQ: {CQ.display()}",
              sep = " | ")
    print()    
    
    # Remove all in CQ
    for i in range(len_CQ + 1):
        print(f"Deq: {CQ.deq()}", 
              f"used: {CQ.usedslots()}",
              f"H,T: {CQ.head_at()}, {CQ.tail_at()}",
              f"CQ: {CQ.display()}",
              sep = " | ")
CQ_Test(CQ)

"""Latest Edits 
   
1. Removed: 
    def usedslots(self):
      return sum(_ is None for _ in self.q)

- The reason I removed the method size() is that it was not time efficient. 
- Instead, updating self.used at enq() and deq() is better.
- It could have just been self.q.count(None) instead lol
"""
Comment

PREVIOUS NEXT
Code Example
Python :: dockerfile example 
Python :: minmax python 
Python :: python search list 
Python :: __call__() python 
Python :: python def example 
Python :: # keys in python 
Python :: create a virtual environment python 3 
Python :: python move files 
Python :: sequence python 
Python :: keras callbacks 
Python :: map python 
Python :: heroku procfile 
Python :: pivot tables pandas explicación 
Python :: telegram bot carousel 
Python :: tadjust margines automatically matplotlib 
Python :: Python OrderedDict - LRU 
Python :: supercharged python 
Python :: boto3 upload to digital digitalocean folder 
Python :: delete everything from list that matches string 
Python :: airflow find trigger type 
Python :: print prime nos from 1 to n 
Python :: showing typle results with for loop in py 
Python :: pytorch get intersection between two masks 
Python :: is 2 an even number 
Python :: imitate meaning 
Python :: print("ola") 
Python :: how to use displacy 
Python :: How to pass a data frame as parameter to a SQL query in Python? 
Python :: f2 polar or nonpolar 
Python :: torch.nn.Linear(in_features, out_features, bias=True) discription 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =