Answered step by step
Verified Expert Solution
Question
1 Approved Answer
in python On Moodle, you will find a starter file called a5q3.py. It has a broken implementation of the function to_string, which is a function
in python
On Moodle, you will find a starter file called a5q3.py. It has a broken implementation of the function to_string, which is a function used by the rest of the assignment. You will also find a test script named a5q3_testing.py. It has a bunch of test cases pre-written for you. Read it carefully! Debug and fix the func- tion to_string(). The error in the function is pretty typical of novice errors with this kind of programming task. The interface for the function is: Purpose: Create a string representation of the node chain. [1| +-]--> [ 21 *-]-->[ 3/] Pre-conditions: param node_chain: A node-chain, possibly empty (none) Post_conditions: E.g., None Return: A string representation of the nodes. # special case: empty node chain if node_chain is None: Note carefully that the function does not do any console output. It should return a string that represents the node chain Here's how it might be used: empty-chain = None chain = N.node (1, N.node (2, N.node (3))) print('empty-chain --->', to-string (empty-chain)) print('chain >, to_string(chain)) Here's what the above code is supposed to do when the function is working empty-chain ---> EMPTY chain [ 1 | +-]--> [ 2 | +-]-->[ 31/1 Notice that the string makes use of the characters '[ + ]--> to reflect the chain of references. The function also uses the character / to abbreviate the value None that indicates the end of a chain. Note especially that the empty chain is represented by the string 'EMPTY'. #### UNIT TEST CASES test_item = 'to_string('. data_in = None expected = 'EMPTY' reason = 'Empty node chain' result = a5q1. to_string(data_in) if result != expected: print('Test failed: {}: got "{}" expected "{}" -- {}'.format(test_item, result, expected, reason)) I data_in = N.node(1) expected = '[ 17 ]' 'node chain with one node' reason = result = a5q1.to_string(data_in) if result != expected: print('Test failed: {}: got "{}" expected "{}" -- {}'.format(test_item, result, expected, reason)) data_in = N.node(1, N.node('two')) expected = '[ 1 ] *-]-->[ two |/ ]' reason = 'node chain with two nodes' result = a5q1.to_string(data_in) if result != expected: print('Test failed: {}: got "{}" expected "{}" -- {}'.format(test_item, result, expected, reason)) import node as N def to string(node chain: # special case: empty node chain if node_chain is None: result = 'EMPTY' else: # walk along the chain walker = node_chain value = walker.get_data() # print the data result = '[ {} l'.format(str(value)) while walker is not None: walker = walker.get_next() value = walker.get_data() # represent the next with an arrow-like figure result += *-]-->[ {} l'.format(str(value)) # at the end of the chain, use '/' result += '/]' return resultStep 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