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
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:   ...
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'.
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++
C++ language We are given a Queue data structure that supports standard operations like Enqueue() and...
C++ language We are given a Queue data structure that supports standard operations like Enqueue() and Dequeue(): Enqueue(element): add a new element at the tail of the queue; Dequeue(): delete the element at the head of the queue. Show how to implement a stack using two queues. Analyze the running time of the stack operations: Push and Pop.
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT