Geschreven door studenten die geslaagd zijn Direct beschikbaar na je betaling Online lezen of als PDF Verkeerd document? Gratis ruilen 4,6 TrustPilot
logo-home
Tentamen (uitwerkingen)

More Python Exercises - Sample solutions _ Supplemental Notebooks_

Beoordeling
-
Verkocht
-
Pagina's
30
Cijfer
A+
Geüpload op
14-12-2022
Geschreven in
2022/2023

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!)")

Meer zien Lees minder
Instelling
CSE 6040X
Vak
CSE 6040X










Oeps! We kunnen je document nu niet laden. Probeer het nog eens of neem contact op met support.

Geschreven voor

Instelling
CSE 6040X
Vak
CSE 6040X

Documentinformatie

Geüpload op
14 december 2022
Aantal pagina's
30
Geschreven in
2022/2023
Type
Tentamen (uitwerkingen)
Bevat
Vragen en antwoorden

Onderwerpen

  • cse 6040x
$12.49
Krijg toegang tot het volledige document:

Verkeerd document? Gratis ruilen Binnen 14 dagen na aankoop en voor het downloaden kan je een ander document kiezen. Je kan het bedrag gewoon opnieuw besteden.
Geschreven door studenten die geslaagd zijn
Direct beschikbaar na je betaling
Online lezen of als PDF

Maak kennis met de verkoper

Seller avatar
De reputatie van een verkoper is gebaseerd op het aantal documenten dat iemand tegen betaling verkocht heeft en de beoordelingen die voor die items ontvangen zijn. Er zijn drie niveau’s te onderscheiden: brons, zilver en goud. Hoe beter de reputatie, hoe meer de kwaliteit van zijn of haar werk te vertrouwen is.
Nechemia17 Campbell University
Bekijk profiel
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
140
Lid sinds
4 jaar
Aantal volgers
124
Documenten
1880
Laatst verkocht
1 maand geleden
BEST HOMEWORK HELP AND TUTORING ,ALL KIND OF QUIZ or EXAM WITH GUARANTEE OF A

WELCOME TO MY WORLD, GET ALL KIND OF EXAMS ON THIS PAGE, COMPLETE TEST BANKS, SUMMARIES,STUDY GUIDES, PROJECT PAPERS, ASSIGNMENTS, CASE STUDIES AND YOU CAN ALSO COMMUNICATE WITH THE SELLER FOR ANY PRE WORK EXAMS, I ASSURE YOU A SATISFACTORY WORK IF YOU WILL USE MY WORK.

3.9

20 beoordelingen

5
11
4
1
3
5
2
1
1
2

Populaire documenten

Recent door jou bekeken

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via Bancontact, iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo eenvoudig kan het zijn.”

Alisha Student

Veelgestelde vragen