mercredi 28 novembre 2018

Random generator using stack data structure

class Stack:
     def __init__(self):
         self.container = []  

     def isEmpty(self):
         return self.size() == 0   

     def push(self, item):
         self.container.append(item)  

     def peek(self) :
         if self.size()>0 :
             return self.container[-1]
         else :
             return None

     def pop(self):
         return self.container.pop()

     def size(self):
         return len(self.container)

s = Stack()
s.isEmpty()
s.push("Cat")
s.push("Dog")
s.push("Horse")
s.push("Snake")
s.push("Lion")
s.push("Fish")
s.push("Bear")
s.push("Tiger")

These are my codes using stack. I am having problems trying to come up with a code that can randomly generate only 3 out of the 8 animals as the output using stack data structure only.

Output Example:

Dog
Snake
Tiger




Aucun commentaire:

Enregistrer un commentaire