Answered step by step
Verified Expert Solution
Link Copied!

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 image text in transcribed

image text in transcribedimage text in transcribed

[] 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) in (). 7 print("r1:", r1) 8 print("r2:", r2) -> 9 assert_equal(ri, r2) 10 11 is_correct = False 3 frames usr/lib/python3.6/unittest/case.py in fail(self, msg) 668 def fail(self, msg=None): 669 *** Fail immediately, with the given message." --> 670 raise self. failureException(msg) 671 672 def assertFalse(self, expr, msg=None): AssertionError: Lists differ: ['a', 0, 'b', 'b', 1, 2, 3, 'c', 4, 5, 6, 7, 8, 9] != ['a'] First list contains 13 additional elements. First extra element 1: - ['a', 0, 'b', 'b', 1, 2, 3, 'c', 4, 5, 6, 7, 8, 9] + ['a'] Type here to search [] 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) in (). 7 print("r1:", r1) 8 print("r2:", r2) -> 9 assert_equal(ri, r2) 10 11 is_correct = False 3 frames usr/lib/python3.6/unittest/case.py in fail(self, msg) 668 def fail(self, msg=None): 669 *** Fail immediately, with the given message." --> 670 raise self. failureException(msg) 671 672 def assertFalse(self, expr, msg=None): AssertionError: Lists differ: ['a', 0, 'b', 'b', 1, 2, 3, 'c', 4, 5, 6, 7, 8, 9] != ['a'] First list contains 13 additional elements. First extra element 1: - ['a', 0, 'b', 'b', 1, 2, 3, 'c', 4, 5, 6, 7, 8, 9] + ['a'] Type here to search

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

4. Devise an interview strategy from the interviewers point of view

Answered: 1 week ago