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.
View the step-by-step solution to:

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 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]

class ArrayList:

   def __init__(self):

       # don't remove or change this method!

       self.data = []


   def append(self, x):

       # don't remove or change this method!

       self.data.append(None)

       self.data[len(self.data)-1] = x


   def insert_times(self, idx, n, x):

       # add your code below

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

Top Answer

View the full answer
Capture.PNG

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

-

Educational Resources
  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question