INTRODUCTION

The assignment is intended to provide you with the opportunity to put into practice what you have learnt by applying your knowledge and skills to the implementation of a Python module (that contains functions that operate on lists).

You are required to implement a Python module that contains functions that manipulate lists. Please ensure that you read sections titled 'Specification' below for further details.

This document is a kind of specification of the required end product that will be generated by implementing the assignment. Like many specifications, it is written in English and hence will contain some imperfectly specified parts.

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 however, you will need to modify this file by writing code that implements the functions listed below.

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

1. 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.

2. 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.

3. Write a function called compare_lists(my_list1, my_list2) that takes two lists as parameters and returns True if the lists are the same (identical) and False otherwise. You may assume that the elements of the list can be compared using the comparison operators ==, !=, etc. You must use a loop in your solution. You must not use built-in functions (other than the range() function), list methods or string methods in your solution. Hint: Check the lengths of both lists to ensure that they are the same length before continuing to determine whether they are identical.

4. 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.

5. 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.

6. 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.

7. 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.

For 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].

PRACTICAL REQUIREMENTS

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 list_function.py module (containing the functions listed below). This is provided for you - you will need to modify this file.
  • Functions (length, to_string, compare_lists, find, insert_value, remove_value and reverse) implemented adhering to the assignment specifications.
  • The supplied assign2_partI_test_file.py file. This is provided for you - please DO NOT modify this file.
  • Well constructed while loops. (Marks will be lost if you use break statements or the like in order to exit from loops).
  • Well constructed for loops. (Marks will be lost if you use break statements or the like in order to exit from loops).
  • Appropriate if/elif/else statements.
  • 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().
    • List method append() to create/build new lists. i.e. list_name.append(item).
    • Concatenation (+) operator to create/build new strings.
    • Comparison operators (==, !=, <, >, etc).
    • Access the individual elements in a list with an index (one element only). i.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. i.e. list_name[start:end].
    • List methods (other than the append() method. 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.

STAGES

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

compare_lists Test
In function compare_lists()
['a', 'b', 'c'] ['a', 'b', 'b'] None
In function compare_lists()
['a', 'b', 'b'] ['a', 'b', 'b'] None
In function compare_lists()
['a', 'b', 'c'] ['a', 'b', 'c', 'd'] 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()
  • compare_lists()
  • 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' towards the end of this document) and if necessary, modify your functions so that:

SAMPLE OUTPUT - PART I

Start Testing!

length Test
List length: 7
List length: 0

to_string Test
List is: r, i, n, g, i, n, g
List is: r-i-n-g-i-n-g
List is:

compare_lists Test
['a', 'b', 'c'] ['a', 'b', 'b'] False
['a', 'b', 'b'] ['a', 'b', 'b'] True
['a', 'b', 'c'] ['a', 'b', 'c', 'd'] False

find Test
3
-1

insert_value Test
['one', 'two', 'three', 'four', 'five']
['p', 'i', 't']
['s', 'p', 'i', 't']
['s', 'p', 'i', 't', 's']

remove_value Test
['r', 'i', 'g']
['i', 'n', 'g']
['r', 'i', 'n']

reverse Test
['d', 'u', 'd', 'e']
['b', 'o', 'r', 'e', 'd']
['d', 'u', 'd', 'e']

End Testing!

Starter Code

#
# PSP Assignment 2 - Test File
# Study Period 3, 2021
#
# DO NOT MODIFY THIS FILE!
#

import list_function

print("nStart Testing!")

str_list1 = ['r', 'i', 'n', 'g', 'i', 'n', 'g']
str_list2 = ['r', 'e', 'd']
empty = []

print("nlength Test")
print("List length:", list_function.length(str_list1))
print("List length:", list_function.length(empty))

print("nto_string Test")
string = list_function.to_string(str_list1)
print("List is:", string)
string = list_function.to_string(str_list1, sep='-')
print("List is:", string)
print("List is:", list_function.to_string(empty))

print("ncompare_lists Test")
str_list3 = ['a','b','c']
str_list4 = ['a','b','b']
str_list5 = ['a','b','c','d']
print(str_list3, str_list4, list_function.compare_lists(str_list3,str_list4))
print(str_list4, str_list4, list_function.compare_lists(str_list4,str_list4))
print(str_list3, str_list5, list_function.compare_lists(str_list3,str_list5))

print("nfind Test")
print(list_function.find(str_list1, 'g'))
print(list_function.find(str_list1, 'z'))

print("ninsert_value Test")
str_list6 = ['one','three','four', 'five']
new_list = list_function.insert_value(str_list6, 'two', 1)
print(new_list)
str_list7 = ['i', 't']
str_list7 = list_function.insert_value(str_list7, 'p', 0)
print(str_list7)
str_list7 = list_function.insert_value(str_list7, 's', -1)
print(str_list7)
str_list7 = list_function.insert_value(str_list7, 's', 7)
print(str_list7)

print("nremove_value Test")
str_list8 = ['r','i','n','g']
new_list = list_function.remove_value(str_list8, 2)
print(new_list)
new_list = list_function.remove_value(str_list8, -1)
print(new_list)
new_list = list_function.remove_value(str_list8, 10)
print(new_list)

print("nreverse Test")
str_list9 = ['e', 'd', 'u', 'd']
str_list10 = ['r', 'o', 'b', 'e', 'd']
new_list = list_function.reverse(str_list9, 4)
print(new_list)
new_list = list_function.reverse(str_list10, 3)
print(new_list)
new_list = list_function.reverse(str_list9, -1)
print(new_list)

print("nEnd Testing!n")
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.