Question

5. Write a new function called "listinsert" based on the following IPO # function: listinsert #...

5. Write a new function called "listinsert" based on the following IPO # function: listinsert # INPUT: a list, a location (integer) and a data # element (can be a string, float, Boolean or # int) # PROCESSING: inserts the supplied data element into the # list at the desired location # OUTPUT: return a new copy of the list that contains # the inserted element Make sure that you DO NOT use any list methods, such as the "insert" or "append" methods, in your program. Note that your original list should not change when you run this function. Here is a sample running of this program: mylist = [10, 20, 30] x = listinsert(mylist, 1, 999) print (x) print (mylist) >> [10, 999, 20, 30] >> [10, 20, 30]

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def listinsert(lst, index, item):
    return lst[0:index] + [item] + lst[index:]


mylist = [10, 20, 30]
x = listinsert(mylist, 1, 999)
print(x)
print(mylist)

Add a comment
Know the answer?
Add Answer to:
5. Write a new function called "listinsert" based on the following IPO # function: listinsert #...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write the following functions, as defined by the given IPO comments. Remember to include the IPO...

    Write the following functions, as defined by the given IPO comments. Remember to include the IPO comments in your submission. Be sure to test your code -- you can use the examples given for each function, but you should probably test additional examples as well -- and then comment-out or remove your testing code from your submission!. We will be testing your functions using a separate program, and if your own testing code runs when we load your file it...

  • Python homework Write a function called insert_value(my_list, value, insert_position) that takes a list, a value and...

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

  • 3. Write Python statements that will do the following: a) Input a string from the user....

    3. Write Python statements that will do the following: a) Input a string from the user. *** Print meaningful messages along with your output.* b) Print the length of the String. Example input: The sky is blue. Correct output: The length of the string is 16 Incorrect output: 16 c) Print the first character of the string. d) Print the last character of the string. 4. Save the program. Double-check the left edge for syntax errors or warning symbols before...

  • Write a function insertNode which inserts the given value into a new LLNode at the head...

    Write a function insertNode which inserts the given value into a new LLNode at the head of the given linked list. Updates the head of the linked list using a double pointer. Example call to insert 10 at the head of the linked list: insertNode( &pHead, 10 ); Complete the function below. void insertNode(LLNode **ppHead, int x){

  • Write a function called FirstElements for adding 100 random integers between 100 to 1000 to myList....

    Write a function called FirstElements for adding 100 random integers between 100 to 1000 to myList. using C program Project: List management Goa l: creating, updating and displaying a list using a menu. Instruction: Create an array called myList. Write a function called FirstElements for adding 100 random integers between 100 to 1000 to myList Initialize the first 100 elements of myList by calling the FirstElements function. Create a menu to display the following options and a number corresponding to...

  • Write a program that asks the user to input 10 integers of an array. The program...

    Write a program that asks the user to input 10 integers of an array. The program then inserts a new value at position (or index) given by the user, shifting each element right and dropping off the last element. For example, in the sample input/output below, the program will insert the value 30 at index 3. All the previous numbers of the array starting from position 3 (i.e., 7 1 20 9 23 8 22 will be shifted one position...

  • Create a new Java Application that test MyStack by having the following methods in main class:...

    Create a new Java Application that test MyStack by having the following methods in main class: 1. A method to generate a number of element between two given values and save them in a stack 2. A method to print a stack (10 elements per line). The original stack should remain as is after the print 3. A method to exchange the first element and the last element in a stack 4. A Boolean method to search for a value...

  • using: class MyQueue<T> { private java.util.LinkedList<T> list; public MyQueue() { list = new java.util.LinkedList<T>(); } public...

    using: class MyQueue<T> { private java.util.LinkedList<T> list; public MyQueue() { list = new java.util.LinkedList<T>(); } public void enqueue(T data) { list.add(data); } public T dequeue() { return list.remove(0); } public T peek() { return list.get(0); } public int size() { return list.size(); } public boolean isEmpty() { return list.isEmpty(); } } class MyQueueTest { public static void main(String[] args) { MyQueue<Integer> queue = new MyQueue<Integer>(); queue.enqueue(3); queue.enqueue(2); queue.enqueue(7); queue.enqueue(1); while (!queue.isEmpty()) { System.out.println(queue.dequeue()); } } } please solve the following:...

  • Python Question? Write a function called reverse(user_list, num = -1) that takes a list and a...

    Python Question? Write a function called reverse(user_list, num = -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. Conditions: 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...

  • For this computer assignment, you are to write a C++ program to implement a class for...

    For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT