Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need some help with fixing Python codes Test codes: l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] insertions = [(0, 'a'),
Need some help with fixing Python codes
Test codes:
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
insertions = [(0, 'a'), (2, 'b'), (2, 'b'), (7, 'c')]
the result should match the function
[] def do_insertions_simple(1, insertions): ** Performs the insertions specified into l. @param 1: list in which to do the insertions. Is is not modified. @param insertions: list of pairs (i, x), indicating that x should be inserted at position i. r = list(1) for i, x in insertions: r.insert(i, x) return r e + Text 4. Keturn the output list. RAM I Disk In the below cell, write your implementation of do_insertions_fast. You are not required to use the strategy outlined above, but it's a pretty good one, and I recommend it using it, you should end up with something at least 100 times faster than do_insertions_simple! Finally, here's one more hint if you decide to use the above strategy: keep track of the number of elements in 1 that have gone into the output list as you go along. This number will be useful in implementing the above strategy. [59] def do_insertions_fast(1, insertions): **?"Implement here a faster version of do_insertions_simple *** output_list = list(1) isert - output_list.insert for i, x in insertions: if i > len(output_list): isert(i,x) else: output_list[i:]-[x] return output_list raise Not ImplementedError() Correctness First, let's check that you compute the right thing. [61] import string Disk - EI LLUITS r2 = do_insertions_fast(1, insertions) assert_equal(ri, r2) is_correct = True r1: ['a', 0, 'b', 'b', 1, 2, 3, 'c', 4, 5, 6, 7, 8, 9] r2: ['a'] - - - - - - - - - - AssertionError Traceback (most recent call last)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started