Starting from:

$25

Programming Assignment 2- WRITING A PYTHON MODULE (LIST MANIPULATION FUNCTIONS) Solution

PART I SPECIFICATION – WRITING A PYTHON MODULE (LIST MANIPULATION FUNCTIONS)
You are required to write a list_function.py module (containing only the functions listed below).  This file is provided for you (on the course website) however, you will need to modify this file by writing code that implements the functions listed below.  Please read the slides on modules available on the course website if you would like more information on modules.

 

You are required to implement a Python module containing the following functions:

 

Write a function called length(my_list) that takes a list as a parameter and returns the length of the list. You must use a loop in your solution.  You must not use built-in functions, list methods or string methods in your solution.
 

Write a function called to_string(my_list, sep=', ') that takes a list and a separator value as parameters and returns the string representation of the list (separated by the separator value) in the following form:
 

item1, item2, item3, item4

 

The separator value must be a default argument.  i.e. sep=', '

 

You must use a loop in your solution.  You must not use built-in functions (other than the range() and str() functions), slice expressions, list methods or string methods in your solution.  You may use the concatenation (+) operator to build the string.  You must return a string from this function.

 

Write a function called find(my_list, value) that takes a list, and a value as parameters. The function searches for the value in the list and returns the index at which the first occurrence of value is found in the list. The function returns -1 if the value is not found in the list.  
 

Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and an insert_position as parameters. The function returns a copy of the list with the value inserted into the list (my_list) at the index specified by insert_position.  Check for the insert_position value exceeding the list (my_list) bounds.  If the insert_position is greater than the length of the list, insert the value at the end of the list.  If the insert_position is less than or equal to zero, insert the value at the start of the list.  You must use a loop(s) in your solution.  You may make use of the list_name.append(item) method in order to build the new list. You must not use built-in functions (other than the range() function), slice expressions, list methods (other than the append() method) or string methods in your solution.
 

Write a function called remove_value(my_list, remove_position) that takes a list and a remove_position as parameters. The function returns a copy of the list with the item at the index specified by remove_position, removed from the list.  Check for the remove_position value exceeding the list (my_list) bounds.  If the remove_position is greater than the length of the list, remove the item at the end of the list.  If the remove_position is less than or equal to zero, remove the item stored at the start of the list. You must use a loop in your solution.  You may make use of the list_name.append(item) method in order to build the new list.  You must not use built-in functions (other than the range() function), slice expressions, list methods (other than the append() method) or string methods in your solution. 
 

Write a function called reverse(my_list, number=-1) that takes a list and a number as parameters. The function returns a copy of the list with the first number of items reversed.  The number parameter must be a default argument.  If the default argument for number is given in the function call, only the first number of items are reversed.  If the default argument for number is not provided in the function call, then the entire list is reversed.  Check for the number value exceeding the list bounds (i.e. is greater than the length of the list).  If the number value exceeds the list bounds, then make the number value the length of the list (i.e. the entire list is reversed).  If the number value entered is less than two, then return a copy of the list with no items reversed.  You must use a loop(s) in your solution.  You may make use of the list_name.append(item) method in order to build the new list. You must not use built-in functions (other than the range() function), slice expressions, list methods (other than the append() method) or string methods in your solution.  
 

Reverse example:

 

a)
numList = [1, 2, 3, 4, 5, 6, 7]

number = 4  The call to reverse(numList, number) should return the new list [4, 3, 2, 1, 5, 6, 7]. 

 

 

b)
numList = [1, 2, 3, 4, 5, 6, 7]

                            The call to reverse(numList) should return the new list [7, 6, 5, 4, 3, 2, 1]. 

 

 

Please note:

You must test your functions to ensure that they are working correctly.  So you do not have to write your own test file, one has been provided for you.  The assign2_partI_test_file.py file is a test file that contains code that calls the functions contained in the list_function.py module.  Please do not modify the test file.

PRACTICAL REQUIREMENTS  (PART I)
It is recommended that you develop this part of the assignment in the suggested stages.  

 

It is expected that your solution WILL include the use of:

The supplied py module (containing the functions listed below). This is provided for you – you will need to modify this file.
Functions (length, to_string, find, insert_value, remove_value and reverse) implemented adhering to the assignment specifications.
The supplied py file. This is provided for you – please DO NOT modify this file.
Well constructed while (Marks will be lost if you use break statements or the like in order to exit from loops).
Well constructed for (Marks will be lost if you use break statements or the like in order to exit from loops).
Appropriate if/elif/else
Output that strictly adheres to the assignment specifications.
Good programming practice:Consistent commenting and code layout. You are to provide comments to describe: your details, program description, all variable definitions, all functions, and every significant section of code.
Meaningful variable names.
 

Your solutions MAY make use of the following:Built-in functions range() and str(). o List method append() to create/build new lists.  e. list_name.append(item). o            Concatenation (+) operator to create/build new strings.
Comparison operators (==, !=, <, , etc). o Access the individual elements in a list with an index (one element only).  e. list_name[index].
Use of any of the functions you have written as part of the assignment. i.e. length() function.
 

Your solutions MUST NOT use:
Built-in functions (other than range() and str() functions).
Slice expressions to select a range of elements from a list. e. list_name[start:end].
List methods (other than the append() i.e. list_name.append(item)).
String methods.
Do not use break, or continue statements in your solution – doing so will result in a significant mark deduction. Do not use the return statement as a way to break out of loops. Do not use quit() or exit() functions as a way to break out of loops.
 

Please ensure that you use Python 3.7.2 or a later version (i.e. the latest version) in order to complete your assignments.  Your programs MUST run using Python 3.7.2 (or latest version).

STAGES  (PART I)
It is recommended that you develop this part of the assignment in the suggested stages.  Many problems in later stages are due to errors in early stages.  Make sure you have finished and thoroughly tested each stage before continuing.

 

The following stages of development are recommended:

 

Stage 1

You will need both the list_function.py and assign2_partI_test_file.py files for this assignment.  These have been provided for you.  Please download both of these files from the course website and ensure that they are in the same directory as each other.

 

Test to ensure that this is working correctly by opening and running the assign2_partI_test_file.py file.  If this is working correctly, you should now see the following output in the Python shell when you run your program:

 

Start Testing!

 

length Test

In function length() List length: None

In function length()

List length: None

 

to_string Test

In function to_string() List is: None

In function to_string() List is: None

In function to_string()

List is: None

 

find Test

In function find() None

In function find()

None

 

insert_value Test

In function insert_value() None

In function insert_value() None

In function insert_value() None

In function insert_value()

None

 

remove_value Test

In function remove_value() None

In function remove_value() None

In function remove_value()

None

 

reverse Test

In function reverse() None

In function reverse() None

In function reverse()

None

 

End Testing!

 

Stage 2

Implement one function at a time.  The following implementation order is a recommendation only:

length() – you may find this function in the lecture slides… : )
to_string()
find()
remove_value()
insert_value()
reverse()
 

Place the code that implements each function in the appropriate place in the list_function.py file.

For example, if you were implementing the length() function, you would place the code that calculates and returns the length of the list under the comment ‘Place your code here’ (within the length function definition) seen below.

 

# Function length() – place your own comments here…  : ) def length(my_list): 

    # This line will eventually be removed - used for development purposes only.     print("In function length()")

 

    # Place your code here

 

Test your function by running the assign2_partI_test_file.py test file to ensure each function is working correctly before starting on the next function.  

Compare your output with the sample output provided (at the end of this document) to ensure that your function is working as it should.

 

Stage 3

Finally, check the sample output (see section titled ‘Sample Output – Part I’ towards the end of this document) and if necessary, modify your functions so that:

    The output produced by your program EXACTLY adheres to the sample output provided.          Your program behaves as described in these specs and the sample output provided.

More products