Menu

Need Help Python Problem M Trying Write Faster Implementation Codes Old One One Wrote Fast Q43853521

Need some help with Python problem:

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

The old one:

[58] def do_insertions_simple(l, insertions): Performs the insertions specified into l. @param 1: list in which to do the

The one I wrote and faster:

[81] def do_insertions_fast(1, insertions): output_list = list(1) ist = output_list.insert for i, x in insertions: if i > len

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

Correctness First, lets check that you compute the right thing. import string 1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] insertions

Please revise my codes or write another faster implementationfunction 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) <ipython-input-87-54e0c44a8801> in <module>() 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 Show transcribed image text [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

Expert Answer


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

OR