+1 (315) 557-6473 

Create a Program to Implement Function Manipulations in Python Assignment Solution.


Instructions

Objective
Write a program to implement function manipulations in python.

Requirements and Specifications

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 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.
  • 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 python assignment 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.
Source Code
#
# PSP Assignment 2 - Provided file (list_function.py).
# Remove this and place appropriate file comments here.
#
# Modify this file to include your code solution.
#
#
# Write your function definitions in this file.
# Place your own comments in this file also.
#
# Function length() - Takes a list as a parameter and returns the length of the list
def length(my_list):
count = 0
for item in my_list:
count += 1
return count
# Function to_string() - Takes a list and a separator value as parameters and
# returns the string representation of the list (separated by the separator value)
def to_string(my_list, sep=', '):
string = ""
for i in range(length(my_list)):
string += str(my_list[i])
if i + 1 < length(my_list):
string += sep
return string
# Function compare_lists() - Takes two lists as parameters and returns True if
# the lists are the same (identical) and False otherwise
def compare_lists(my_list1, my_list2):
if length(my_list1) != length(my_list2):
return False
for i in range(length(my_list1)):
if my_list1[i] != my_list2[i]:
return False
return True
# Function find() - searches for the value in the list and returns the
# # index at which the first occurrence of value is found in the list
def find(my_list, value):
for i in range(length(my_list)):
if my_list[i] == value:
return i
return -1
# Function insert_value() - returns a copy of the list with the value
# inserted into the list (my_list) at the index specified by insert_position
def insert_value(my_list, value, insert_position):
new_list = []
# Adjust position
if insert_position >= length(my_list):
insert_position = length(my_list)
elif insert_position < 0:
insert_position = 0
# Copy values from start until before insert position
position = 0
while position < insert_position:
new_list.append(my_list[position])
position += 1
# Do insert
new_list.append(value)
# Copy values from insert position to end of list
while position < length(my_list):
new_list.append(my_list[position])
position += 1
return new_list
# Function remove_value() - Returns a copy of the list with the item at the
# index specified by remove_position, removed from the list
def remove_value(my_list, remove_position):
new_list = []
# Adjust position
if remove_position >= length(my_list):
remove_position = length(my_list) - 1
elif remove_position < 0:
remove_position = 0
# Copy items except the removed position
for i in range(length(my_list)):
if i != remove_position:
new_list.append(my_list[i])
return new_list
# Function reverse() - Returns a copy of the list with the first number
# of items reversed
def reverse(my_list, number=-1):
new_list = []
# Adjust indices properly
if number < 0 or number > length(my_list):
number = length(my_list)
# Copy the list
for item in my_list:
new_list.append(item)
# Reverse only the specific portion
start = 0
end = number - 1
while start < end:
# Swap the letters between start and end
temp = new_list[start]
new_list[start] = new_list[end]
new_list[end] = temp
start += 1
end -= 1
return new_list