Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Problem: Need to write a faster implementation function: Original Function: Instruction: Test: l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Python Problem:

Need to write a faster implementation function:

Original Function:

image text in transcribed

Instruction:image text in transcribedTest:

l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

insertions = [(0, 'a'), (2, 'b'), (2, 'b'), (7, 'c')]

(Both functions should have the same results but the new one should implement a lot faster)

[ ] def do_insertions_simple(1, insertions): "*"Performs the insertions specified into 1. @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. list(1) for i, x in insertions: r.insert(i, x) return r For this homework assignment, you will write a faster implementation of do_insertions_simple (which we will call do_insertions_fast). We won't need anything extra (no special modules, no advanced algorithms, no Numpy) in order to obtain a considerable speedup. Let's think about what makes do_insertions_simple slow, and about how we can rewrite the whole thing in a faster way. The biggest problem with do_insertions_simple is that it calls insert once for every element of the insertions list -- or 10,000 times, in our test case above. Insertion into a list is expensive, because it requires all the elements after the insertion point to be rearranged. So, insertions early in the list are especially expensive. Appending to the end of a list, on the other hand, is very cheap, because nothing has to be rearranged. So, how can we make it that we use insert as little as possible, and instead use append whenever possible? Here's one strategy for implementing do_insertions_fast: 1. Create a new, empty list. We will use this list to build up the output. 2. For each pair (i, x) in insertions, do the following: o If i is less than the current length of the output list, insert x into the output list at the appropriate position. o Otherwise, do as follows: Find out how many list elements should fall between the current end of the output list and the place x should go. Take only those elements from the original list 1, and concatenate them to the output list. Then append x to the end of the output list. 3. Finally, after all insertions are done, concatenate any remaining elements of 1 onto the output list. 4. Return the output list. 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. [] def do_insertions_fast(1, insertions): """Implement here a faster version of do_insertions_simple """ # YOUR CODE HERE raise NotImplementedError()

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

Databases Theory And Applications 27th Australasian Database Conference Adc 20 Sydney Nsw September 28 29 20 Proceedings Lncs 9877

Authors: Muhammad Aamir Cheema ,Wenjie Zhang ,Lijun Chang

1st Edition

3319469215, 978-3319469218

More Books

Students also viewed these Databases questions

Question

Identify the potential uses of molecular genetics research.

Answered: 1 week ago