Write a random number generator that generates random numbers between 1 and 6 (simulates a dice). Write a Python program to implement a stack and queue using a list data-structure.
Answers
Let's tackle each of the three things separately. The code has docstrings in the Sphinx format, so documentation is there too. The screenshots are attached, so that should explain where the code starts and ends.
A screenshot showing the execution of all the three programs is also attached.
Dice - Random Number Generator
We basically have to generate a random number from 1 to 6. The function we would be using is . The endpoints 1 and 6 are inclusive.
Now, we can just write down a simple print statement with the above line slapped in. That works too. Here, we create a Class.
The Code
from random import rand int
class Dice():
def __init__(self):
self.value = None
def roll(self):
"""Rolls the dice, prints a random number from 1 to 6 and returns it
:return: A random number from 1 to 6
:rtype: int
"""
self.value = rand int(1, 6)
print(f"Dice Rolled. Value = {self.value}")
return self.value
dice = Dice()
# Roll the dice 10 times
for i in range(10):
dice.roll()
For both Stack and Queue, we implement two operations: and. The push method adds an element to the data structure, the pop method removes an element from the data structure and returns it.
Both are implemented using Python lists. We normally add elements using the method of lists. The place where the data structures differ is what element gets removed.
Stack
A Stack is a data structure which follows the LIFO (Last In First Out) order. Elements can be inserted and removed only from one side. So, the pop method would return the last element that was inserted.
We also implement a simple error check in case the stack or queue is empty.
The Code
class Stack():
"""A class to implement the stack data structure"""
def __init__(self):
self.stack = []
def push(self, item):
"""Pushes an item onto the stack
:param item: The item to be pushed onto the stack
:type item: Any
"""
self.stack.append(item)
def pop(self):
"""Pops the last entered item from the stack and returns it
:raises IndexError: If the Stack is empty
:return: The last entered item from the stack
:rtype: type(item)
"""
if len(self.stack) == 0:
raise IndexError("Stack is empty")
return self.stack.pop()
numbers = Stack()
numbers.push(1)
numbers.push(3)
numbers.push(5)
print(numbers.pop())
numbers.push(10)
print(numbers.pop())
print(numbers.pop())
print(numbers.pop())
Queue
A Queue is a data structure which follows the FIFO (First In First Out) order. Elements can be inserted and removed from opposite ends. So, the pop method would return the first element that was inserted.
The Code
class Queue():
"""A class to implement the queue data structure"""
def __init__(self):
self.queue = []
def push(self, item):
"""Pushes an item onto the queue
:param item: The item to be pushed onto the queue
:type item: Any
"""
self.queue.append(item)
def pop(self):
"""Pops the last entered item from the queue and returns it
:raises IndexError: If the Queue is empty
:return: The last entered item from the queue
:rtype: type(item)
"""
if len(self.queue) == 0:
raise IndexError("Queue is empty")
return self.queue.pop(0)
numbers = Queue()
numbers.push(1)
numbers.push(3)
numbers.push(5)
print(numbers.pop())
numbers.push(10)
print(numbers.pop())
print(numbers.pop())
print(numbers.pop())