Question: Starter File : a5q1: #a5q1 import node as node def to_string(node_chain): Purpose: Create a string representation of the node chain. E.g., [ 1 |

Starter File :
a5q1:
#a5q1 import node as node
def to_string(node_chain): """ Purpose: Create a string representation of the node chain. E.g., [ 1 | *-]-->[ 2 | *-]-->[ 3 | / ] Pre-conditions: :param node_chain: A node-chain, possibly empty Post_conditions: None Return: A string representation of the nodes. """ # special case: empty node chain if node_chain is None: result = 'EMPTY' else: # walk along the chain walker = node_chain value = node.get_data(walker) # print the data result = '[ ' + str(value) + ' |' while walker is not None: walker = node.get_next(walker) value = node.get_data(walker) # represent the next with an arrow-like figure result += ' *-]-->[ '+str(value)+' |'
# at the end of the chain, use '/' result += ' / ]'
return result
a5q2.py :
import node as node
def count_chain(node_chain): """ Purpose: Counts the number of nodes in the node chain. Pre-conditions: :param node_chain: a node chain, possibly empty Return: :return: The number of nodes in the node chain. """ return 0
def contains_duplicates(node_chain):
"""
Purpose:
Returns whether or not the given node_chain contains one or more duplicate data values.
Pre-conditions: :param node_chain: a node-chain, possibly empty Return:
:return: True if duplicate data value(s) were found,
False otherwise
""" return False
def insert_at(node_chain, value, index):
"""
Purpose:
Insert the given value into the node-chain so that
it appears at the the given index.
Pre-conditions: :param node_chain: a node-chain, possibly empty
:param value: a value to be inserted
:param index: the index where the new value should appear
Assumption: 0
where n is the number of nodes in the chain
Post-condition:
The node-chain is modified to include a new node at the
given index with the given value as data.
Return
:return: the node-chain with the new value in it
""" return None
a5q2-testing.py :
# test script
import a5q1 as a5q1 import a5q2 as a5q2 import node as node
#################################################################################################### #### UNIT TEST CASE: count_chain() #### args_in = None expected = 0 reason = 'Empty node chain'
result = a5q2.count_chain(args_in) if result != expected: print('Test failed: count_chain(): got', str(result), 'expected', str(expected), '--' + reason)
#### UNIT TEST CASE: count_chain() #### args_in = node.create(1) expected = 1 reason = 'node chain with one node'
result = a5q2.count_chain(args_in) if result != expected: print('Test failed: count_chain(): got', str(result), 'expected', str(expected), '--' + reason)
#### UNIT TEST CASE: count_chain() #### args_in = node.create(1, node.create('two')) expected = 2 reason = 'node chain with two nodes'
result = a5q2.count_chain(args_in) if result != expected: print('Test failed: count_chain(): got', str(result), 'expected', str(expected), '--' + reason)
#### UNIT TEST CASE: count_chain() #### args_in = node.create(1, node.create('two', node.create(3))) expected = 3 reason = 'node chain with three nodes'
result = a5q2.count_chain(args_in) if result != expected: print('Test failed: count_chain(): got', str(result), 'expected', str(expected), '--' + reason)
#################################################################################################### #### UNIT TEST CASE: contains_duplicates() #### args_in = None expected = False reason = 'Empty node chain'
result = a5q2.contains_duplicates(args_in) if result != expected: print('Test failed: contains_duplicates(): got', str(result), 'expected', str(expected), '--' + reason)
#### UNIT TEST CASE: contains_duplicates() #### args_in = node.create(1) expected = False reason = 'node chain with one node'
result = a5q2.contains_duplicates(args_in) if result != expected: print('Test failed: contains_duplicates(): got', str(result), 'expected', str(expected), '--' + reason)
#### UNIT TEST CASE: contains_duplicates() #### args_in = node.create('two', node.create('two')) expected = True reason = 'node chain with two nodes that are duplicates'
result = a5q2.contains_duplicates(args_in) if result != expected: print('Test failed: contains_duplicates(): got', str(result), 'expected', str(expected), '--' + reason)
#### UNIT TEST CASE: contains_duplicates() ####
Purpose: To practice working with node chains created using the Node ADT. Degree of Difficulty: Easy to Moderate In this question you'll write three functions for node-chains that are a little more challenging. On Moodle. you can find a starter fle called a5q2.py, with all the functions and doc-strings in place, and your job is to write the bodies of the functions You will also find a test script named a5q2 testing.py. It has a bunch of test cases pre-written foryou. Read it carefully! Use to_string() from Q1 to help you test and debug your functions. (a) (6 points) Implement the function count_chain). The interface for the function is: def count chain (node_chain): Purpose Counta the number of nodes in the node chain Pre conditions: :param node_chain: a node chain , pOSsibly empty Return: return: The number of nodes in the node chain Note carefully that the function is not to do any console output. A demonstration of the n of the function is as follows: empty_chain None chain node.create (1, node. create (2, node. create (3))) print'empty chain has', count_chain (empty-chain) elements') print ('chain has', count_chain (chain) 'elements) The output from the demonstration is as follows: empty chain has 0 elenents chain has 3 elenents (b) (6 points) Implement the function contains duplicatea). The interface for the function is: def contains_duplicates (node_chain): Purpose: Returns whether or not the given node_chain contains one or more duplicate data values Pre conditions: :param node_chain: a node - chain, possibly empty Return: return: True if duplicate data value (s) ere found, False otherwise For this question, you are NOT allowed to use a List to store data values, instead consider using two walkers and a nested loop. This operation should simply return True if we find ANY duplicate values, or False otherwise. Pseudocode for the algorithm follows on the next page. Purpose: To practice working with node chains created using the Node ADT. Degree of Difficulty: Easy to Moderate In this question you'll write three functions for node-chains that are a little more challenging. On Moodle. you can find a starter fle called a5q2.py, with all the functions and doc-strings in place, and your job is to write the bodies of the functions You will also find a test script named a5q2 testing.py. It has a bunch of test cases pre-written foryou. Read it carefully! Use to_string() from Q1 to help you test and debug your functions. (a) (6 points) Implement the function count_chain). The interface for the function is: def count chain (node_chain): Purpose Counta the number of nodes in the node chain Pre conditions: :param node_chain: a node chain , pOSsibly empty Return: return: The number of nodes in the node chain Note carefully that the function is not to do any console output. A demonstration of the n of the function is as follows: empty_chain None chain node.create (1, node. create (2, node. create (3))) print'empty chain has', count_chain (empty-chain) elements') print ('chain has', count_chain (chain) 'elements) The output from the demonstration is as follows: empty chain has 0 elenents chain has 3 elenents (b) (6 points) Implement the function contains duplicatea). The interface for the function is: def contains_duplicates (node_chain): Purpose: Returns whether or not the given node_chain contains one or more duplicate data values Pre conditions: :param node_chain: a node - chain, possibly empty Return: return: True if duplicate data value (s) ere found, False otherwise For this question, you are NOT allowed to use a List to store data values, instead consider using two walkers and a nested loop. This operation should simply return True if we find ANY duplicate values, or False otherwise. Pseudocode for the algorithm follows on the next page
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
