Answered step by step
Verified Expert Solution
Question
1 Approved Answer
32. In this question you will demonstrate your understanding of references, and the Node ADT. Consider the following script, which uses the Node ADT: import
32. In this question you will demonstrate your understanding of references, and the Node ADT. Consider the following script, which uses the Node ADT: import node as node y = node.create (3, None) t = node.create (1, None) node.set_next (y, t) x = y y = node.get_next (y) t = node.create (4, None) node.set_next (y, t) y = node.get_next (y) In the space below, draw the Global Python frame, and the heap, showing the nodes created and all the variables, and all the references, as it would appear at the end of the script. You may use the simplified diagrams for nodes, as we did in class. You may also find it useful to work through the code above, with a rough diagram, and then draw out the final state more clearly below it. You have enough space here. Draw a box around your final answer! 31. In this question you will demonstrate your understanding of the Queue and Stack ADTS. Do not violate the ADT Principle in this question. We want to design a function called duplicate. This function is to have one parameter, a Stack created by Stack.create(), and it must return a new Stack. The function's purpose is to duplicate the given stack, so by the end of the function, there will be two distinct queues containing the same elements in the same order. The new stack has to be distinct, but still contain the same elements as the given stack. (a) Complete the documentation of the interface of the function by adding information to the template below. Purpose: 3) Pre-conditions: Post-conditions: Return: 6) (b) Define the function duplicate () in the space below. You do not need to give a doc- string here, because you did that above. We've imported the ADTs for you. You may not use a Python list anywhere in this question. import Stack as Stack import Queue as Queue ) 33. In this question you will demonstrate your skill in writing test scripts. In Chapter 9, we studied the evaluation of post-fix expressions as an application of stacks and queues. The evaluation function had the following interface: def postfix_evaluate (expr_string): Purpose: Evaluate a postfix expression. Pre-conditions: expr_list: a string representing a post-fix expression Post-Conditions: none Return: the value of the expression Using the form below, design 3 test cases. You must indicate the inputs, the expected outputs, and the reason for your test. Test case inputs: Expected result: Reason: 9. Write a function named find_missing that checks if a given list contains all the val- ues 1,2,...,n, where n is the length of the list. For example, find_missing ( [3,1,2]) returns [], and find_missing ([1,5,0]) returns [2,3]. This is related to functions you wrote in Assignments 1 and 2. You may solve this any way you like; the only requirement is that it is consistent with the interface given below as a doc-string. To answer this question, just give the body of the function, below the doc-string. def find_missing (alist): Purpose: Return a list of numbers 1 ... N that do not appear in alist Here, N is the length of alist. Pre-conditions: alist: a list of integers Post-conditions: None Return: A list of the numbers in 1 ... N that do not appear in alist. 30. In this question you will demonstrate your understanding of the Queue and Stack ADTs. Do not violate the ADT Principle in this question. We want to design a function called drop. This function is to take two arguments: one is a Queue created by Stack.create (), and the other is a number. The purpose of the function drop() is to remove all the values in the queue less than the given number, assuming that the queue contains numbers in increasing order. For example, suppose that the function is given a queue containing the numbers 1 2 3 4 and the number 3; the function drop() will remove all the numbers strictly less than 3, leaving the values |3|4|in the queue. (a) Complete the documentation of the interface of the function by adding information to the template below. Purpose: (3) Pre-conditions: Post-conditions: Return: (4) (b) Define the function drop() in the space below. You do not need to give a doc-string here, because you did that above. We've imported the ADTs for you. You may not use a Python list anywhere in this question. import Stack as Stack import Queue as Queue 32. In this question you will demonstrate your understanding of references, and the Node ADT. Consider the following script, which uses the Node ADT: import node as node y = node.create (3, None) t = node.create (1, None) node.set_next (y, t) x = y y = node.get_next (y) t = node.create (4, None) node.set_next (y, t) y = node.get_next (y) In the space below, draw the Global Python frame, and the heap, showing the nodes created and all the variables, and all the references, as it would appear at the end of the script. You may use the simplified diagrams for nodes, as we did in class. You may also find it useful to work through the code above, with a rough diagram, and then draw out the final state more clearly below it. You have enough space here. Draw a box around your final answer! 31. In this question you will demonstrate your understanding of the Queue and Stack ADTS. Do not violate the ADT Principle in this question. We want to design a function called duplicate. This function is to have one parameter, a Stack created by Stack.create(), and it must return a new Stack. The function's purpose is to duplicate the given stack, so by the end of the function, there will be two distinct queues containing the same elements in the same order. The new stack has to be distinct, but still contain the same elements as the given stack. (a) Complete the documentation of the interface of the function by adding information to the template below. Purpose: 3) Pre-conditions: Post-conditions: Return: 6) (b) Define the function duplicate () in the space below. You do not need to give a doc- string here, because you did that above. We've imported the ADTs for you. You may not use a Python list anywhere in this question. import Stack as Stack import Queue as Queue ) 33. In this question you will demonstrate your skill in writing test scripts. In Chapter 9, we studied the evaluation of post-fix expressions as an application of stacks and queues. The evaluation function had the following interface: def postfix_evaluate (expr_string): Purpose: Evaluate a postfix expression. Pre-conditions: expr_list: a string representing a post-fix expression Post-Conditions: none Return: the value of the expression Using the form below, design 3 test cases. You must indicate the inputs, the expected outputs, and the reason for your test. Test case inputs: Expected result: Reason: 9. Write a function named find_missing that checks if a given list contains all the val- ues 1,2,...,n, where n is the length of the list. For example, find_missing ( [3,1,2]) returns [], and find_missing ([1,5,0]) returns [2,3]. This is related to functions you wrote in Assignments 1 and 2. You may solve this any way you like; the only requirement is that it is consistent with the interface given below as a doc-string. To answer this question, just give the body of the function, below the doc-string. def find_missing (alist): Purpose: Return a list of numbers 1 ... N that do not appear in alist Here, N is the length of alist. Pre-conditions: alist: a list of integers Post-conditions: None Return: A list of the numbers in 1 ... N that do not appear in alist. 30. In this question you will demonstrate your understanding of the Queue and Stack ADTs. Do not violate the ADT Principle in this question. We want to design a function called drop. This function is to take two arguments: one is a Queue created by Stack.create (), and the other is a number. The purpose of the function drop() is to remove all the values in the queue less than the given number, assuming that the queue contains numbers in increasing order. For example, suppose that the function is given a queue containing the numbers 1 2 3 4 and the number 3; the function drop() will remove all the numbers strictly less than 3, leaving the values |3|4|in the queue. (a) Complete the documentation of the interface of the function by adding information to the template below. Purpose: (3) Pre-conditions: Post-conditions: Return: (4) (b) Define the function drop() in the space below. You do not need to give a doc-string here, because you did that above. We've imported the ADTs for you. You may not use a Python list anywhere in this question. import Stack as Stack import Queue as Queue
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started