USING PYTHON: Write a function called insert_list() that has two parameters. The
ID: 674636 • Letter: U
Question
USING PYTHON:
Write a function called insert_list() that has two parameters. The first parameter is a list of strings (which may be empty), and is in increasing sequential order (based on string comparison). The second parameter is a string. The function insert_list() should return a new list with the string inserted in the correct position in the list (based on string comparison). Required parameter testing: your function should verify what is passed to the parameters. If the list contains anything but strings, or is not sorted in increasing sequential order, or if the parameter to be inserted is not a string, your function should output a meaningful error/warning message
CISC106, Fall 2015
such as “insert_list: error – list parameter is not in increasing order”, or “insert_list: error – value to be inserted must be an string.” In the case of an invalid parameter, your function should return None. Parameter testing is a significant part of this problem, and your code should include comments that help the TAs understand this part of your code.
Note: the list may have duplicate values (e.g., ['Zoo', 'Zoo', 'ball', 'egg Beater', 'haT', 'haT', 'pencil'] is a valid list), or may be empty. The value to be inserted may already be in the list, may be less than the smallest value in the list, or may be greater than the largest value in the list. Strings may have any combination of printable characters and spaces in them. For this problem you may use built-in list methods, except you may NOT use the sort() method
Explanation / Answer
# -*- coding: UTF-8 -*- def insert_list(strings,new): pos=0 got=0 if(len(strings)==0): return [new] if(type(new)!=str): print('insert_list: error – value to be inserted must be an string ') return None for i in range(0,len(strings)-1): if(type(strings[i])!=str): print("insert_list: error – list parameter are not all strings ") return None if(type(strings[i+1])!=str): print("insert_list: error – list parameter are not all strings ") return None if(strings[i]>strings[i+1]): print("insert_list: error – list parameter is not in increasing order ") return None if(strings[i]>=new and got==0): pos=i got=1 if(strings[-1]>=new and got==0): pos=len(strings)-1 got=1 if(strings[-1]Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.