Advertisement
Guest User

ringbuffer exercise #1

a guest
Nov 14th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1. #! /usr/bin/env python3
  2. #-*- coding: utf-8 -*-
  3.  
  4. """ This is a test script.
  5. """
  6.  
  7. # #################  class definition  ######################################
  8.  
  9. class ringBuf:
  10.     def __init__(self, items):
  11.         self.maxItems = items
  12.         self.items = 0
  13.         self.newItems = 0
  14.         self.ringData = [0 for i in range (items)]
  15.         self.readP = 0
  16.         self.writeP = 0
  17.  
  18.     def readItem(self):
  19.         if self.newItems == 0:
  20.             raise StopIteration
  21.         else:
  22.             thisItem = self.ringData[self.readP]
  23.             self.newItems -= 1
  24.             self.readP = (self.readP + 1) % self.maxItems
  25.             return thisItem
  26.  
  27.     def writeItem(self, item):
  28.         self.ringData[self.writeP] = item
  29.         self.writeP = (self.writeP + 1) % self.maxItems
  30.         if self.newItems < self.maxItems:
  31.             self.newItems += 1
  32.         else:
  33.             self.readP = (self.readP + 1) % self.maxItems
  34.         if self.items < self.maxItems:
  35.             self.items += 1
  36.  
  37.     def getList(self):
  38.         thisList = [self.ringData[self.writeP]]
  39.         thisList.clear()
  40.         thisReadP = (self.maxItems + self.writeP - self.items) % self.maxItems
  41.         for i in range(self.items):
  42.             thisList.append(self.ringData[thisReadP])
  43.             thisReadP = (thisReadP + 1) % self.maxItems
  44.         return thisList
  45.  
  46.     def readPos(self):
  47.         return self.readP
  48.  
  49.     def writePos(self):
  50.         return self.writeP
  51.  
  52.     def itemCount(self):
  53.         return self.items
  54.  
  55.     def maxItems(self):
  56.         return self.maxItems
  57.  
  58.     def newItems(self):
  59.         return self.newItems
  60.  
  61.  
  62. # #################  here comes the main code  ##############################
  63.  
  64. ringInst = ringBuf(7)
  65.  
  66. while True:
  67.     for i in range(3):
  68.         print(str(i + 1) + ". Ganzzahl: ", end = "")
  69.         number = int(input())
  70.         ringInst.writeItem(number)
  71.     print("Whole ring listed: ", str(ringInst.getList()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement