• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 3 out of 30 pages
Exam (elaborations)

More Python Exercises - Sample solutions _ Supplemental Notebooks_

Document preview thumbnail
Preview 3 out of 30 pages

Fast searching in ordered collections. This problem consists of just a single exercise, worth ten (10) points. It is about an elementary principle of computer science algorithm design, which is that one can often do things faster if one exploits structure in the input data. Suppose you are given a list of already sorted numbers. In [ ]: Suppose you now want to know whether a certain value exists in this list. A simple way to do that in Python is as follows. In [ ]: This method works fine and is reasonably fast on small lists. However, if the list is very large, this method can be wasteful, computationally speaking. That's because it does not take advantage of the fact that A is already ordered. In such a case, it should be easier to determine whether the element exists. (How?) Exercise 0 (3 + 7 == 10 points). Write a function, ordered_contains(S, x), that takes an already sorted list, S, as input, and determines whether it contains x. But there is one more condition: your method must be at least ten times faster than contains() for "large" lists! In particular, there are two test codes for this exercise. The first one checks that your procedure does, indeed, return the correct result by comparing its output to contains(), which we will assume is correct. Correctness is worth three (3) points of partial credit out of ten (10). The second test cell checks whether your implementation is faster than contains() for a relatively large, but ordered, list. If your implementation is slower for smaller lists, that is okay! Hint. If you can find a standard Python library routine to help you, by all means, use it! In [ ]: def ordered_contains(S, x): # You may assume that `S` is sorted ### BEGIN SOLUTION return ordered_contains 2(S, x) # Version 0: Use Python's built-in `bisect()` routine def ordered_contains 0(S, x): from bisect import bisect i = bisect(S, x) return i 0 and S[i-1] == x def contains(A, x): """Returns True only if the value `x` exists in `A`.""" return x in A print("A contains 32: {}".format(contains(A, 32))) print("A contains 7: {}".format(contains(A, 7))) print("A contains -10: {}".format(contains(A, -10))) A = [2, 16, 26, 32, 52, 71, 80, 88] # These are already sorted: assert A == sorted(A) 8/19/2020 More Python Exercises - Sample solutions | Supplemental Notebooks: More Python Exercises | CSE6040x Courseware | edX # Version 1: A manual implementation of binary search. ## This is the algorithm that this exercise is intended to # "introduce." # ## However, for a variety of implementation reasons, this # implementation is unlikely to beat Version 0, above. # Any ideas why not? # THRESHOLD 1 = 128 # An "engineering" constant - use to tune speed def ordered_contains 1(S, x): if len(S) = THRESHOLD 1: return contains(S, x) midpoint = int(len(S) / 2) if x S[midpoint]: return ordered_contains 1(S[:midpoint], x) if x S[midpoint]: return ordered_contains 1(S[midpoint+1:], x) return True # Found it! # The following method improves on the above. Why is it faster? THRESHOLD 2 = 8 # An "engineering" constant - use to tune speed def ordered_contains 2(S, x, l=0, r=None): if r is None: r = len(S) if (r-l) = THRESHOLD 2: return contains(S[l:r], x) midpoint = int((l+r) / 2) if x S[midpoint]: return ordered_contains 2(S, x, l, midpoint) if x S[midpoint]: return ordered_contains 2(S, x, midpoint+1, r) return True # Found it! ### END SOLUTION print("A contains 32: {}".format(ordered_contains(A, 32))) print("A contains 7: {}".format(ordered_contains(A, 7))) print("A contains -10: {}".format(ordered_contains(A, -10))) print("n(Did those results match the earlier example?)") In [ ]: In [ ]: # Test cell: `test_is_faster` (7 points) # Test cell: `test_is_correct` (1 point) from random import randint, sample def gen_list(n, v_max, v_min=0): return sample(range(v_min, v_max), n) def gen_sorted_list(n, v_max, v_min=0): return sorted(gen_list(n, v_max, v_min)) def check_case(S, x): msg = "`contains(S, {}) == {}` while `ordered_contains(S, {}) == {}`!" true_solution = contains(S, x) your_solution = ordered_contains(S, x) assert your_solution == true_solution, t(true_solution, your_solution) S = gen_sorted_list(13, 100) print("Checking your code on this input: S = {}".format(S)) check_case(S, S[0]) check_case(S, S[0]-1) check_case(S, S[-1]) check_case(S, S[-1]+1) for x in gen_list(50, 100, -100): check_case(S, x) print("n(Passed basic correctness checks.)") print("nTiming `contains()`...") x = randint(-100, 100) %timeit contains(S, x) print("nTiming `ordered_contains()`...") %timeit ordered_contains(S, x) print("n(This problem is small, so it's okay if your method is slower.)") print("n(Passed!)")


Document information

Uploaded on
December 14, 2022
Number of pages
30
Written in
2022/2023
Type
Exam (elaborations)
Contains
Questions & answers
$12.49

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
Nechemia17
3.9
(21)
Sold
143
Followers
125
Items
1877
Last sold
2 months ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions

Whoops! We can’t load your doc right now. Try again or contact support.