Question

1. Make a function make_list_of_lists(n, m) that creates and returns a list of n lists of...

1. Make a function make_list_of_lists(n, m) that creates and returns a list of n lists of size m, where each of the m elements in a sublist are randomly generated integers between 1 and 9. For example, you might obtain

l = make_list_of_lists(2,2)
>>> print(l)
[[1, 4], [5, 7]]

Big Hint: You can do this using numpy along the lines of the following code:

import numpy
>>> l = list(numpy.random.randint(a,b,c))

where you need to sort out what the values of a, b and c do to create the list you want.

2. Once you have this function working, generate a second function called deep_copy_list_of_lists that makes a genuine copy of an input list, i.e., it does not alias the list or its elements. Use only the tools we have learned in lecture to do this.

3. Prove that your deep copy function works by using your list generating function to create a random list of lists with three (3) lists with five (5) elements (n = 3, m = 5), using your deep copy function to copy it (preserving the original list), and then changing some of the numbers in the copied list by indexing each number directly, showing that the original list has not changed. Print the output of each of these operations and include this output as a comment in your submission. Do NOT query the user for input.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

# do comment if any problem arises
# Code
import numpy


def make_list_of_lists(n, m):
# create list using numpy
l = list(numpy.random.randint(low=1,high=10,size=[n, m]))
return l

def deep_copy_list_of_lists(l):
# create a copy of given list
copy=[[0]*len(l[0]) for i in range(len(l))]
for i in range(len(l)):
for j in range(len(l[0])):
copy[i][j]=l[i][j]
return copy
  
# this function prints given list
def print_list(l):
for i in range(len(l)):
for j in range(len(l[0])):
print(l[i][j],end=" ")
print()
  
# testing above function
l=make_list_of_lists(3, 5)
# creating copy
copy=deep_copy_list_of_lists(l)
print("Changing first element of original list")
l[0][0]=22
print("\nCopy: \n")
print_list(copy)
print("\nOriginal: \n")
print_list(l)

Screenshot:

Output:

Add a comment
Know the answer?
Add Answer to:
1. Make a function make_list_of_lists(n, m) that creates and returns a list of n lists of...
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
  • use python 2a. Union (1 points) Make a union function which takes in 2 lists as...

    use python 2a. Union (1 points) Make a union function which takes in 2 lists as parameters and returns a list. It should return a list that contain all elements that are in either list. Your results list should contain no duplicates. For example, if the two lists are [1,5,6,5, 2] and [3,5, 1,9] then the result list is (1,5,6,2,3,9). Note that the list sizes don't have to be equal. Note that the input lists could have duplicates, but your...

  • Write a function called avg_max that takes a list of lists (sublists may be empty) and...

    Write a function called avg_max that takes a list of lists (sublists may be empty) and return the average of the largest item in each sublist as a float. If a sublist is empty, do not consider it for the purposes of computing average. You may assume that everything inside a non-empty sublist is a number. You may assume that at least one sublist will be non-empty. Write a function called avg max that takes a list of lists (sublists...

  • 1. Question: Write a function that takes a list L and returns a random sublist of...

    1. Question: Write a function that takes a list L and returns a random sublist of size N of that list. Assume that the indexes must be in increasing order. That is, you cannot go backwards Example L [1, 2, 3, 4, 5] 5 The function should return one of these lists: [2, 3, 4] [2, 3, 5] [2, 4, 5] [3, 4, 5]

  • python Make another code block that creates a list using range( ) with 8 values starting...

    python Make another code block that creates a list using range( ) with 8 values starting with 1. print out your list Create a set from each of the three lists. Find the elements in common between the three sets.

  • USING SCHEME PROGRAMMING LANGUAGE define a function addSubList that processes a list of lists of numbers...

    USING SCHEME PROGRAMMING LANGUAGE define a function addSubList that processes a list of lists of numbers and adds up all sub-lists. The output of your function is a flat list of numbers. (You can assume that your function only receives valid input). Example: (define Q '(1 2 (3 4) 1 5 (7 8))) ;;;;;;;;;;;;;;;;;;;;; ; (addSubList Q) ; => '(1 2 7 1 5 15) ;;;;;;;;;;;;;;;;;;;

  • C++ You're given the pointer to the head nodes of two linked lists. Compare the data...

    C++ You're given the pointer to the head nodes of two linked lists. Compare the data in the nodes of the linked lists to check if they are equal. The lists are equal only if they have the same number of nodes and corresponding nodes contain the same data. Either head pointer given may be null meaning that the corresponding list is empty. Input Format You have to complete the int CompareLists (Node headA, Node* head B) method which takes...

  • I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on)...

    I need the following merge-sort assignment completed using the "ArrayList<Comparable>" (the part I'm really stuck on) with comments: import java.util.ArrayList; public class Mergesort { /** * Sorts list given using the mergesort algorithm * @param list the list to be sorted * @param first the index of the first element of the list to be sorted * @param last the index of the last element of the list to be sorted */ public static void mergesort(ArrayList<Comparable> list, int first, int...

  • c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked...

    c++ please Given the following skeleton of an unsorted list class that uses an unsorted linked list: template<class ItemType> struct NodeType {                 ItemType item;                 NodeType* next; }; template<class ItemType> class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign operator with deep copy                 bool IsThere(ItemType item) const; // return true of false to indicate if item is...

  • Valentine’s Day Question(JAVA) Suppose you had a list with 30 valentines and you stored their names...

    Valentine’s Day Question(JAVA) Suppose you had a list with 30 valentines and you stored their names in an array (you hopeless romantic). Then if you had 5 people ask to be your valentine, you would need to make a new array of size 35 (30 + 5) and copy over the original valentines before being able to add the new valentines to the array. This question uses the same logic. Write a program that has a method called doubleSize. The...

  • More Practice: Writing Lists& Strings . Write a function average_ evens that takes a list of...

    More Practice: Writing Lists& Strings . Write a function average_ evens that takes a list of numbers as parameters, and adds all the even numbers together and returns their average using a list Example function call: print (average_evens (I-2, -3, 4, 0, 1, 2,3 Outputs: 0 Write a function match that takes two strings as a parameter and returns how many letters the strings have in common. You should treat upper and lower case letters as the same letter ('A,...

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