Question
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:
The one I wrote and faster:
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.
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)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