Question: 1. Implement the method insert_times in the ArrayList class which takes parameters idx, n, x, and inserts n copies of x into the list at

1.

Implement the method insert_times in the ArrayList class which takes parameters idx, n, x, and inserts n copies of x into the list at index idx. You may assume that 0 idx the length of the list, and n > 0.

E.g., calling insert_times(3, 5, 'a') on an ArrayList with contents [0,1,2,3,4,5,6,7] should result in the updated ArrayList [0,1,2,a,a,a,a,a,3,4,5,6,7]

Your implementation may only use the following methods on the built-in list:

  • self.data[i] for getting and setting values at an existing, positive index i
  • len(self.data) to obtain the number of slots
  • self.data.append(None) to grow the list by one slot at a time
  • del self.data[len(self.data)-1] to delete the last slot in a list

2.

Implement the method remove_all in the ArrayList class which takes the parameter x and removes all occurences of x from the list.

E.g., calling remove_all('a') on an ArrayList with contents ['a', 'b', 'r', 'a', 'c', 'a', 'd', 'a', 'b', 'r', 'a'] should result in the updated ArrayList ['b','r','c','d','b','r']

Your implementation may only use the following methods on the built-in list:

  • self.data[i] for getting and setting values at an existing, positive index i
  • len(self.data) to obtain the number of slots
  • self.data.append(None) to grow the list by one slot at a time
  • del self.data[len(self.data)-1] to delete the last slot in a list

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!