Python Problem Need Rewrite Function Faster Implementation Original Codes Slow One Instruc Q43853844
Python problem:
Need to rewrite a function and have a faster implementation:
Original Codes(slow one)
Instructions:
My Current Codes:(not fast enough)
Thank you! Take your time!
specifying that x should be inserted at position i in the list. The list insertions is sorted according to i (the problem would be markedly more difficult if this were not true, but you can consider on your own how to solve it in that case). The function do_insertions_simple performs all insertions, and returns the result, without modifying the original list 1. [4] def do_insertions_simple(1, insertions): “” “Performs the insertions specified into l. @param l: 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 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 greater 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 i 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): output_list = list(1) ist = output_list.insert app = output_list.append for i, x in insertions: if i > len(output_list): app(x) else: ist(i,x) return output_list Show transcribed image text specifying that x should be inserted at position i in the list. The list insertions is sorted according to i (the problem would be markedly more difficult if this were not true, but you can consider on your own how to solve it in that case). The function do_insertions_simple performs all insertions, and returns the result, without modifying the original list 1. [4] def do_insertions_simple(1, insertions): “” “Performs the insertions specified into l. @param l: 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
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 greater 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 i 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): output_list = list(1) ist = output_list.insert app = output_list.append for i, x in insertions: if i > len(output_list): app(x) else: ist(i,x) return output_list
Expert Answer
Answer to Python problem: Need to rewrite a function and have a faster implementation: Original Codes(slow one) Instructions: My C…
OR