"A book to train programming skills to fight in the world" Python code answer example --3.1 Three stacks

"A book to train programming skills to fight in the world" Python code answer example --3.1 Three stacks

CHAP3. Stacks and queues

  1. Three stacks

Python code answer example

python



class FixedMultiStack:

    def __init__(self, stackSize):
        numberOfStacks = 3
        self.stackCapacity = stackSize
        self.values = [0] * (stackSize * numberOfStacks) 
        self.sizes = [0] * numberOfStacks

    def indexOfTop(self,stackNum):
        offset = stackNum * self.stackCapacity
        size = self.sizes[stackNum]
        return offset + size - 1

    def isFull(self,stackNum):
        return self.sizes[stackNum] == self.stackCapacity

    def push(self,stackNum,value):
        if self.isFull(stackNum):
            raise Exception("FullStackException")
        self.sizes[stackNum] = self.sizes[stackNum] + 1
        self.values[self.indexOfTop(stackNum)] = value

    def isEmpty(self,stackNum):
        return self.sizes[stackNum] == 0

    def pop(self,stackNum):
        if self.isEmpty(stackNum):
            raise Exception("EmptyStackException")
        topIndex = self.indexOfTop(stackNum)
        value = self.values[topIndex]
        self.values[topIndex] = 0
        self.sizes[stackNum] = self.sizes[stackNum] - 1
        return value

    def peek(self,stackNum):
        if self.isEmpty(stackNum):
            raise Exception("EmptyStackException")
        return self.values[self.indexOfTop(stackNum)]

FMS = FixedMultiStack(5)

print(FMS.values)

FMS.push(0,1)
FMS.push(1,6)
FMS.push(2,11)

print(FMS.values)

FMS.push(0,2)
FMS.push(0,3)
FMS.push(0,4)
FMS.push(0,5)

print(FMS.values)

FMS.pop(0)
FMS.pop(1)

print(FMS.values)

print(FMS.peek(0))
print(FMS.peek(2))

Recommended Posts

"A book to train programming skills to fight in the world" Python code answer example --3.1 Three stacks
"A book to train programming skills to fight in the world" Python code answer example --1.8 "0" matrix
"Book to train programming skills to fight in the world" Python code answer example --1.3 URLify
"Book to train programming skills to fight in the world" Python code answer example --2.6 palindrome
"A book to train programming skills to fight in the world" Python code answer example --1.9 Rotation of strings
"A book to train programming skills to fight in the world" Python code answer example --1.1 Duplicate character string
"Book to train programming skills to fight in the world" Python code answer example --2.4 Splitting the list
"Book to train programming skills to fight in the world" Python code answer example --2.7 intersecting nodes
"A book to train programming skills to fight in the world" Python code answer example --2.2 Returns the Kth from the back
"A book to train programming skills to fight in the world" Python code Solution example --1.6 String compression
"A book to train programming skills to fight in the world" Python code solution example --1.5 One-shot conversion
"A book to train programming skills to fight in the world" Python code Solution example --1.7 Matrix rotation
"Book to train programming skills to fight in the world" Python code answer example --1.4 Permutation of sentences
"A book to train programming skills to fight in the world" Python code Solution example --2.8 Loop detection
"A book to train programming skills to fight in the world" Python code answer example --1.2 Count the number of the same characters
"Book to train programming skills to fight in the world" Python code solution example --- Removed elements between 2.3
"Book to train programming skills to fight in the world" Python code Solution example --2.1 Remove duplicate elements
"A book to train programming skills to fight in the world" Python code Solution example --2.5 The sum of two numbers shown in the list
Programming to fight in the world ~ 5-5,5-6
Programming to fight in the world 5-3
Programming to fight in the world ~ 5-2
Programming to fight in the world-Chapter 4
An example of the answer to the reference question of the study session. In python.
[Kenchon book to Python] "Train your problem-solving skills! Algorithms and data structures" I tried to rewrite the posted code in Python! -table of contents-
I searched for the skills needed to become a web engineer in Python
[Kenchon book to Python]-Chapter 3- "Train your problem-solving skills! Algorithms and data structures" I rewrote the posted code to Python!
[Kenchon book to Python]-Chapter 2- "Train your problem-solving skills! Algorithms and data structures" I rewrote the posted code to Python!
[Kenchon book to Python]-Chapter 4-"Train your problem-solving skills! Algorithms and data structures" I rewrote the posted code to Python!
Run the output code with tkinter, saying "A, pretending to be B" in python
Try to solve the programming challenge book with python3
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
A Python program in "A book that gently teaches difficult programming"
How to use the __call__ method in a Python class
Change the standard output destination to a file in Python
How to get the last (last) value in a list in Python
Run the output code on the local web server as "A, pretending to be B" in python
How to determine the existence of a selenium element in Python
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
How to check the memory size of a variable in Python
I wrote the code to write the code of Brainf * ck in python
[Introduction to Python] How to use the in operator in a for statement?
How to check the memory size of a dictionary in Python
How to run the practice code of the book "Creating a profitable AI with Python" on Google Colaboratory
In the python command python points to python3.8
[Python] Explains how to use the range function with a concrete example
Sample code to get the Twitter API oauth_token and oauth_token_secret in Python 2.7
What kind of book is the best-selling "Python Crash Course" in the world?
Python code to determine the monthly signal of a relative strength investment
I made a program to check the size of a file in Python
What is the fastest way to create a reverse dictionary in python?
Tips for Python beginners to use the Scikit-image example for themselves 6 Improve Python code
How to sort by specifying a column in the Python Numpy array.
Part 1 I wrote an example of the answer to the reference problem of how to write offline in real time in Python
Spiral book in Python! Python with a spiral book! (Chapter 14 ~)
Write the test in a python docstring
Run the Python interpreter in a script
How to get a stacktrace in python
Try a functional programming pipe in Python
Get the EDINET code list in Python
How to display Hello world in python
Learn dynamic programming in Python (A ~ E)