Question

In: Computer Science

# ArrayStack # TODO: push and pop using array stack with doubling technique. import numpy as...

# ArrayStack
# TODO: 
push and pop using array stack with doubling technique.

import numpy as np
from future import print_function

# Definisi class ArrayStack
class ArrayStack(object):
        def __init__(self):
                self.data = np.zeros(20, dtype=np.int)
                self.count = 0
                
        def isEmpty(self):
                pass # ubah saya
                
        def size(self):
                pass # ubah saya
                
        def push(self, obj):
                pass # ubah saya
                
        def pop(self):
                pass # ubah saya
                
#--------------------------
# End of class

if __name__ == "__main__":
        stack = ArrayStack()
        randn = np.random.permutation(1000)
        for i in range(1000):
                stack.push(randn[i])
        ####
        for i in range(100):
                print(stack.pop())
        ####

Solutions

Expert Solution

import numpy as np


# from future import print_function


# Definisi class ArrayStack
class ArrayStack(object):
    def __init__(self):
        self.data = np.zeros(20, dtype=np.int)
        self.count = 0

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

    def size(self):
        return self.count

    def push(self, obj):
        if self.count == len(self.data):
            newData = np.zeros(2 * self.count, dtype=np.int)
            for i in range(len(self.data)):
                self.data[i] = newData[i]
            self.data = newData

        self.data.put(self.count, obj)
        self.count += 1

    def pop(self):
        elem = self.data[self.count - 1]
        np.delete(self.data, self.count - 1)
        self.count -= 1
        return elem


# --------------------------
# End of class

if __name__ == "__main__":
    stack = ArrayStack()
    randn = np.random.permutation(1000)
    for i in range(1000):
        stack.push(randn[i])
    ####
    for i in range(100):
        print(stack.pop())
    ####

Related Solutions

Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the java and give the explanation
All code should be in Python 3. Implement the Stack Class, using the push, pop, str,...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str, init methods, and the insurance variable 'list'.
JAVA Stack - Implementation. You will be able to use the push, pop and peek of...
JAVA Stack - Implementation. You will be able to use the push, pop and peek of Stack concept. Post-Fix calculator - When an arithmetic expression is presented in the postfix form, you can use a stack to evaluate the expression to get the final value. For example: the expression 3 + 5 * 9 (which is in the usual infix form) can be written as 3 5 9 * + in the postfix. More interestingly, post form removes all parentheses...
Write a function that returns the largest value in a stack (only use push and pop)
Write a function that returns the largest value in a stack (only use push and pop)
Consider an ADT with the same operators as a stack (push(), pop(), size(), empty()), except that...
Consider an ADT with the same operators as a stack (push(), pop(), size(), empty()), except that the pop() operator returns the largest element rather than the element most recently pushed. For this to make sense, the objects on the stack must be totally ordered (e.g. numbers). Describe array and linked list implementations of the ADT. Your implementations should be as time and space efficient as you can manage. Give the running times for the CRUD operators.
UsePython (import numpy as np) use containers (Branching if statement, while loop, for loop, numpy Array)...
UsePython (import numpy as np) use containers (Branching if statement, while loop, for loop, numpy Array) Implement an algorithm to guess a random number that the computer generates. The random number must be an integer between 1 and 1000 (inclusive). For each unsuccessful attempt, the program must let the user know whether to deal with a higher number or more. low. There is no limit on the number of attempts, the game only ends when the user succeeds. The user...
Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations Counting...
Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations Counting Letter Challenge: Create a function that... Takes in a string as parameter Counts how often each letter appears in the string Returns a dictionary with the counts BONUS: make it so lowercase and uppercase letter count for the same letter
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
Problem A. The pseudocode as below illustrates the basic push() and pop() operations of an array-based...
Problem A. The pseudocode as below illustrates the basic push() and pop() operations of an array-based stack, where top is initialized as 0 when the stack is empty. Assuming that at a given moment, SIZE is equal to 20, top is equal to 8, this code is used in a concurrent environment (top and stack[] are in shared memory section), process P0 is about to call push() and process P1 is about to call pop() concurrently a. Which variable has...
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3....
Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3. isEmpty 4. PEEK 5. Size */ #include <stdio.h> #define CAPACITY 1000 //Two stacks .. for each stack we need // 1. An Array that can hold capacity of elements // 2. A top initialzied to -1 (signifying that the stak is empty at the start) //NOTE : THESE STACKS ARE OF TYPE CHAR :( ... so you need to FIX IT!!!! to int and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT