Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need some help with Python problem: I'm trying to write a faster implementation of a codes: The old one: The one I wrote and faster:

Need some help with Python problem:

I'm trying to write a faster implementation of a codes:

The old one:

image text in transcribed

The one I wrote and faster:

image text in transcribed

But, when I test them they have different results, two functions (do_insertions_simple) and (do_insertions_fast) have different results when I insert two lists. My test case: l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] and insertions = [(0, 'a'), (2, 'b'), (2, 'b'), (7, 'c')]. The result was r1: ['a', 0, 'b', 'b', 1, 2, 3, 'c', 4, 5, 6, 7, 8, 9], r2: ['a', 'b', 'b', 'c']. They should match.

image text in transcribed

Please revise my codes or write another faster implementation function for me thank you!

[58] def do_insertions_simple(l, 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 [81] def do_insertions_fast(1, insertions): output_list = list(1) ist = output_list.insert for i, x in insertions: if i > len(output_list): ist(i,x) else: output_list[i:] = [x] return output_list Correctness First, let's check that you compute the right thing. import string 1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] insertions = [(@, 'a'), (2, 'b'), (2, 'b'), (7, 'C')] rl = do_insertions_simple(1, insertions) r2 = do_insertions_fast(1, insertions) print("r1:", rl) print("r2:", r2) assert_equal(ri, r2) is_correct = False for - in range (20): 1, insertions = generate_testing_case(list_len=100, num_insertions-20) r1 = do_insertions_simple(1, insertions) r2 = do_insertions_fast(1, insertions) assert_equal(ri, r2) is_correct = True D r1: ['a', 0, 'b', 'b', 1, 2, 3, 'C', 4, 5, 6, 7, 8, 9] r2: ['a', 'b', 'b', 'c'] AssertionError Traceback (most recent call last) in () 6 print("r1:", ri) 7 print("r2:", r2) ----> 8 assert_equal(ri, r2) 10 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', 'b', 'b', 'c'] First differing element 1: First list contains 10 additional elements. First extra element 4: - ['a', 0, 'b', 'b', 1, 2, 3, 'c', 4, 5, 6, 7, 8, 9] + ['a', 'b', 'b', 'c'] SEARCH STACK OVERFLOW

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

Students also viewed these Databases questions