QUIZ 2 Warm-up Questions

.pdf

School

Pennsylvania State University *

*We aren’t endorsed by this school

Course

132

Subject

Computer Science

Date

Apr 3, 2024

Type

pdf

Pages

5

Uploaded by ChancellorEagle4123

Report
CMPSC-132: Practice Exercises Question 1: Write the Python code for finding the second-to-last (item before the last) node in a Singly Linked list in which the last node is indicated by a next reference of None. If there is no second-to-last node, return None. Question 2 : Assume you have a Doubly Linked List with the head node and at least one other internal node C which is not the last node. You may assume that each node has a next pointer and prev pointer. Write few lines of code to accomplish the following. You may NOT swap data to accomplish any of the following operations. Remember that for each operation, you need to manipulate at least two pointers, next and prev . a) Delete the head Node b) Swap head and C nodes, you cannot swap data Question 3: Recall the Stack data structure discussed in Module 5. You were asked to implement such structure in Homework 3. Such data structure contained a reference to the top node in the stack and can perform the operations len, isEmpty, push, pop and peek only. Write the Python code for the function smallest_at_top(stack_object) that takes a Stack object as a parameter and determines which value in stack_object is the smallest and move that Node to the top of the stack while leaving the remainder of the stack in its original order. For example, if the stack contains the elements 23, 72, 94, 3, 1, 60 (where 23 is the top Node and 60 is the bottom Node) then a call to smallest_at_top (stack_object) will update the original stack to 1, 23, 72, 94, 3, 60 (where 1 is the top Node and 60 is the bottom Node) and returns nothing. Your code may only declare Stack or scalar variables (int, float) if needed. You are not allowed to declare other variables such as Python lists, linked lists, dictionaries, etc., or use other methods or Classes from the Python library. You can assume the stack contains unique numerical values. When the stack is empty, smallest_at_top (stack_object) returns None def smallest_at_top(stack_object): ''' >>> myStack=Stack() >>> myStack.push(55) >>> myStack.push(98) >>> myStack.push(-2.5) >>> myStack.push(3) >>> myStack.push(156) >>> myStack.push(9) >>> myStack.push(56.5) >>> myStack Top:Node(56.5) Stack: 56.5 9 156 3 -2.5 98 55 >>> smallest_at_top(myStack) >>> myStack Top:Node(-2.5) Stack: -2.5 56.5 9 156 3 98 55 '''
Question 4: Write the implementation for the pop() method for a Doubly Linked List with a) a reference to the tail node and b) no reference to the tail node. The Node class is defined as: class Node: def __init__(self, value): self.value = value self.next = None self.previous = None def __str__(self): return f"Node({self.value})" __repr__ = __str__ Question 5: Below is the partial implementation of a FIFO Queue using only Stacks. The constructor, isEmpty and enqueue methods have been implemented for you and should not be modified. Write the implementation of the dequeue method for the Queue class using only stack_1 and stack_2 operations only. You are not allowed to use Python lists. class Queue: def __init__(self): self.stack_1=Stack() self.stack_2=Stack() def isEmpty(self): if self.stack_1.isEmpty() and self.stack_2.isEmpty(): return True return False def enqueue(self, value): self.stack_1.push(value) def dequeue(self):
Question 6: Consider a real-time stock trading platform where traders need to quickly respond to market changes. In such a platform, each trader might have a list of buy or sell orders they've placed, which need to be processed in a specific order (LIFO - Last In, First Out) as market conditions change. However, traders also need to keep track of their most advantageous (minimum) price point among their active orders to make informed decisions swiftly. Design a class `StockTrader` that supports all three of these operations without requiring any looping. Hint : consider having two stacks, one that stores the stocks in the order they come in, and one that keeps a "running minimum". Question 7: Write the method double_them that takes a queue and replaces every node in the queue with two occurrences of that node. For example, suppose a queue contains the following values: 3 -> 7.5 -> 1 -> 14-> 9, when 3 is the head and 9 the tail. After the method ends the queue should contain the following values: 3 -> 3 -> 7.5 -> 7.5 -> 1 -> 1 -> 14-> 14 -> 9 ->9 (3 is head and 9 tail). Notice that you must preserve the original order. You may use one queue for additional storage. Question 8: Complete the definition of the get_sum method in the BinaryTree class. This method returns the sum of the values contained in all of the Nodes of the binary tree with root node . It does not modify the original tree. You can assume the values of the nodes are numeric. You are not allowed to copy values of the Nodes into other data structures class Node: def __init__(self, value): self.value = value self.left = None self.right = None def __str__(self): return ("Node({})".format(self.value)) __repr__ = __str__ class BinaryTree: def __init__(self): self.root = None def get_sum(self, start): ''' For the binary tree shown in the picture: >>> x.get_sum(x.root) 55 >>> x.get_sum(x.root.left) 31 >>> x.get_sum(x.root.left.right) 22 ''' Question 9: Write the implementation of the __contains__ method in the BinaryTree class that returns True if value is present in the tree, False otherwise. Note that the tree is not necessarily a binary search tree. Question 10: Write the implementation of the reverse method in the LinkedList class discussed and implemented in Module 5. This method reverses the list in-place (modifies the original list).
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help