Question

In: Computer Science

Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations Counting...

  1. Using Python list tools, create the standard stack (push, pop) and queue (enqueue, dequeue) operations
  2. Counting Letter Challenge: Create a function that...
    1. Takes in a string as parameter
    2. Counts how often each letter appears in the string
    3. Returns a dictionary with the counts
    4. BONUS: make it so lowercase and uppercase letter count for the same letter

Solutions

Expert Solution

STACK IMPLEMENTATION USING LIST:

# class to represent the stack using list
class Stack:

     # constructor to initialize the object
     def __init__(self):
         self.item_lst = []

     # method to push an element into the stack
     def push(self, item):
         self.item_lst.append(item)

     # method to remove the top element from the stack
     def pop(self):
         return self.item_lst.pop()

     # method to retrieve the top element of the stack
     def peek(self):
         return self.item_lst[len(self.item_lst)-1]

     # method to find the size of the stack
     def size(self):
         return len(self.item_lst)

     # method to find the emptiness of the stack
     def isEmpty(self):
         return self.item_lst == []

# testing
if __name__=='__main__':

    # creating stack object
    s=Stack()

    # checking for empty
    print(s.isEmpty())

    # adding elements into the stack
    s.push(44)
    s.push(55)
    s.push(60)

    # displaying the size of the stack
    print(s.size())  
    s.push(4.55)

    # calling various functions of the stack
    print(s.pop())  
    print(s.size())
    print(s.peek())

SCREENSHOT FOR CODING:

SCREENSHOT FOR OUTPUT:

-----------------

QUEUE IMPLEMENTATION USING LIST:

# class to represent the QUEUE using list
class Queue:

    # constructor to initialize the object
    def __init__(self):
        self.item_lst = []

    # method to add a element in the queue
    def enqueue(self, i):
        self.item_lst.insert(0,i)

    # method to remove a element from the queue
    def dequeue(self):
        return self.item_lst.pop()

    # method to find the size of the queue
    def size(self):
        return len(self.item_lst)

    # method to find the emptiness of the queue
    def isEmpty(self):
        return self.item_lst == []
    
# testing
if __name__=='__main__':

    # creating queue object
    q=Queue()

    # adding elements to the queue
    q.enqueue(47)
    q.enqueue(52)
    q.enqueue(62)

    # displaying the size of the queue
    print(q.size())

    # removing the first element from the queue
    print(q.dequeue())
    q.enqueue(50)

SCREENSHOT FOR CODING:

SCREENSHOT FOR OUTPUT:

                              -----------------------------

CODE FOR LETTER COUNT FUNCTION:

# function to count letters in a string
def count_letter(s):

    # dictionary to store the letter count
    d={}

    # looping through every character in the string
    for c in s:

        # changing to lowercase
        c=c.lower()

        # adding the character to the dictionary
        if c not in d:
            d[c]=1
        else:
            d[c]+=1

    # returning the dictionary
    return d

# testing
if __name__=='__main__':

    print(count_letter('python'))
    print(count_letter('Python ProgramMing'))

SCREENSHOT FOR CODING:

SCREENSHOT FOR OUTPUT:


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
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H...
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal = top; top...
Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with...
Discuss the relative efficiency of the enqueue and dequeue operations for an array-based queue implemented with a fixed-front approach as opposed to a floating-front approach.
5 marks] A MinStack supports three main operations: the standard Stack operations push(x) and pop() and...
5 marks] A MinStack supports three main operations: the standard Stack operations push(x) and pop() and the non-standard min() operation which returns the minimum value stored on the stack. The zip file gives an implementation SlowMinStack that implements these operations so that push(x) and pop() each run in O(1) time, but  min()runs in Θ(n) time. For this question, you should complete the implementation of FastMinStack that implements all three operations in O(1) time per operation. As part of your implementation, you...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method...
Java queue linked list /* * Complete the enqueue(E val) method * Complete the dequeue() method * Complete the peek() method * No other methods/variables should be added/modified */ public class A3Queue {    /*    * Grading:    * Correctly adds an item to the queue - 1pt    */    public void enqueue(E val) {        /*        * Add a node to the list        */    }    /*    * Grading:   ...
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'.
True False The enqueue and dequeue operations in a priority queue take O(lg n) time, while...
True False The enqueue and dequeue operations in a priority queue take O(lg n) time, while linked list and array implementations take O(1) time. A binary heap is a (nearly) complete binary tree. Heaps usually use a linked node structure for implementation. When implementing heaps as arrays, you can leave a blank space at the front. If you do, the parent of a node at index i can be found at index floor(i/2). When implementing heaps as arrays, if you...
Task 3: Implement a queue through circular linked list. Develop enqueue, dequeue, status, isempty and isfull...
Task 3: Implement a queue through circular linked list. Develop enqueue, dequeue, status, isempty and isfull functions. Test your code in main function with 10 elements Note: for each task, you are supposed to create three files as specified in task1, i.e., implementation file, interface file and file containing point of entry. in C++
# 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):...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT