The following rules apply to insert_value() and remove_value()
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.
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.
The following rules apply to reverse()
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.
- If the number value entered is less than two, then return a copy of the list with no items reversed.
I'm had a good crack at the remove and reverse functions and I think I fully understand what I'm trying to do, I just lack the python knowledge to implement it.
e.g. here I have 'pass' in the remove function and .reverse in the reverse function I know isn't right (and not allowed under the rules of only being able to use append and range anyhow)
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.
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.
The following rules apply to reverse()
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.
- If the number value entered is less than two, then return a copy of the list with no items reversed.
I'm had a good crack at the remove and reverse functions and I think I fully understand what I'm trying to do, I just lack the python knowledge to implement it.
e.g. here I have 'pass' in the remove function and .reverse in the reverse function I know isn't right (and not allowed under the rules of only being able to use append and range anyhow)
def insert_value(my_list, value, insert_position): new_list = [] list_length = length(my_list) list_index = 0 value_position = insert_position #if insert position is not valid. if insert_position <= 0: value_position = 0 elif insert_position > list_length - 1: value_position = list_length while list_index < list_length: if list_index == value_position: new_list.append(value) new_list.append(my_list[list_index]) list_index += 1 if value_position == list_length: new_list.append(value) return new_list def remove_value(my_list, remove_position): new_list = [] list_length = length(my_list) list_index = 0 value_position = remove_position #if remove position is not valid. if remove_position <= 0: value_position = 0 elif remove_position > list_length -1: value_position = list_length #iterate through the list_length while list_index < list_length: # remove/pass once iteration lands on the value position if list_index == value_position: pass new_list.append(my_list[list_index]) list_index += 1 # if at the end of the list, also remove/pass if value_position == list_length: pass return new_list def reverse(my_list, number=-1): new_list = [] list_length = length(my_list) list_index = 0 reverse_pos = number #if number value entered is less than 2, return copy of list (nothing reversed) if reverse_pos <= 1: my_list = new_list #if no number given, use the default value of -1 elif reverse_pos == -1: reverse_pos = -1 #if the position exceeds list length, make the reverse pos to be the length of the list. elif reverse_pos > list_length -1: reverse_pos = list_length -1 while list_index < list_length: #iterate through and once we reach the reverse_pos if list_index == reverse_pos: #reverse my_list at that point from the range of 0:reverse_pos new_list.reverse(my_list[0:list_index]) #append all values onto the new_list new_list.append(my_list[list_index]) if list_index > reverse_pos: #append all values onto the new_list new_list.append(my_list[list_index]) new_list.append(my_list[list_index]) list_index += 1 return new_listI'm using this to call in my file name called list_function.py
print("\ninsert_value Test") str_list3 = ['one','three','four', 'five', 'six'] new_list = list_function.insert_value(str_list3, 'two', 1) print(new_list) str_list4 = ['i', 't'] str_list4 = list_function.insert_value(str_list4, 'p', 0) print(str_list4) str_list4 = list_function.insert_value(str_list4, 's', -1) print(str_list4) str_list4 = list_function.insert_value(str_list4, 's', 7) print(str_list4) print("\nremove_value Test") str_list5 = ['r','i','n','g'] new_list = list_function.remove_value(str_list5, 2) print(new_list) new_list = list_function.remove_value(str_list5, -1) print(new_list) new_list = list_function.remove_value(str_list5, 10) print(new_list) print("\nreverse Test") str_list6 = ['e', 'd', 'u', 'd'] str_list7 = ['m', 'o', 'b', 'b', 'e', 'd'] new_list = list_function.reverse(str_list6, 4) print(new_list) new_list = list_function.reverse(str_list7, 3) print(new_list) new_list = list_function.reverse(str_list6) print(new_list)