In: Computer Science
python
Write a Loop class. The contents of a loop object are represented internally as a simple python list with the following additional instance variables:
loop - a python list containing the elements of the loop list
head - which stores the index of the first element of the loop list
current - which stores the index of the last element of the loop list
size - which stores how many actual elements are in the loop
max - which stores the max number of elements that can be in a loop
Note that current and head will be the same when the list is empty, and when the list contains a single element.
And methods:
__init__(self, max) - which constructs an empty loop object;
__str__(self) - which returns a string representing the loop object as described below;
add(element) - which adds the element to the end of the loop (i.e. at the position of current + 1)
empty() - which says whether there are any elements in the loop;
count() - which says how many elements currently are in the loop;
delete() - which returns the first item from the loop (i.e. at the position of head) and removes it from the loop;
max() - returns the max number of elements allowed in the loop.
__init__ initializes the empty locations in the loop list to None. Assume that max is always greater than the number of elements to be in the loop at any one time. When an element is being added to an empty loop, it always goes into the first location (index 0) in the loop list and head and current are reset to 0.
If an attempt is made to delete() from an empty loop then delete() returns None. When an element is deleted from the loop its value is changed to None. Remember, your code needs to properly maintain the correct values of loop, head, current and size.
__str__ formats the loop object data as follows:
<max> <size> <head> <current> <loop list>
For example, after:
x = Loop(4)
x.add(1)
x.add(3)
x.delete()
print(x)
"print(x)" prints out:
4 1 1 1 [None, 3, None, None]
For example:
Test | Result |
---|---|
x = Loop(5) print(x) |
5 0 0 0 [None, None, None, None, None] |
x = Loop(5) x.add(3) print(x) |
5 1 0 0 [3, None, None, None, None] |
x = Loop(3) x.add(3) x.add(2) print(x) |
3 2 0 1 [3, 2, None] |
x = Loop(3) x.add(3) x.add(2) x.delete() print(x) |
3 1 1 1 [None, 2, None] |
x = Loop(3) x.add(2) x.add(5) x.add(3) x.delete() x.add(7) print(x) |
3 3 1 0 [7, 5, 3] |
'''
Python version : 2.7
Python program to create and test Loop class
'''
class Loop:
def __init__(self, max):
self.max = max
self.loop = [None]*max
self.head = 0
self.current = 0
self.size = 0
def __str__(self):
return str(self.max)+"
"+str(self.size)+" "+str(self.head)+" "+str(self.current)+"
"+str(self.loop)
def add(self,element):
# if empty loop, insert in index
0
if self.empty():
self.loop[0] =
element
self.head =
0
self.current =
0
self.size +=
1
# if full loop, nothing
happens
elif self.size == self.max:
return
else:
# get the next
index for insertion
self.current =
(self.current + 1)%self.max
# insert the
element
self.loop[self.current] = element
self.size += 1 #
increment the size
def empty(self):
return self.size == 0
def count(self):
return self.size
def delete(self):
# if loop is not empty
if not self.empty():
item =
self.loop[self.head] # get the first element of the loop
self.loop[self.head] = None # set the element to None
self.size -= 1 #
decrement the size
# if loop is
empty after deletion, reset head and current to 0
if
self.empty():
self.head = 0
self.current = 0
else: # update head to next element
self.head = (self.head + 1)%self.max
return item #
return item
return None
def max(self):
return self.max
# test the class
def main():
x = Loop(5)
print(x)
x = Loop(5)
x.add(3)
print(x)
x = Loop(3)
x.add(3)
x.add(2)
print(x)
x = Loop(3)
x.add(3)
x.add(2)
x.delete()
print(x)
x = Loop(3)
x.add(2)
x.add(5)
x.add(3)
x.delete()
x.add(7)
print(x)
#call the main function
main()
#end of program
Code Screenshot:
Output: