12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

100 Chapter 10. Listsnested list: A listthat isan element of another list.listtraversal: The sequential accessing ofeach element inalist.mapping: A relationship in which each element of one set corresponds to an element of anotherset. For example, alistisamapping from indices toelements.accumulator: A variable used inaloop toadd up oraccumulate aresult.augmented assignment: Astatementthatupdatesthevalueofavariableusinganoperatorlike+=.reduce: A processing pattern that traverses a sequence and accumulates the elements into a singleresult.map: A processing pattern that traverses asequence and performs an operation oneach element.filter: Aprocessingpatternthattraversesalistandselectstheelementsthatsatisfysomecriterion.object: Something a variable can refer to. An object has atype and avalue.equivalent: Having thesame value.identical: Being the sameobject (whichimplies equivalence).reference: The association between avariable and itsvalue.aliasing: A circumstance where twoor morevariables refer tothesame object.delimiter: A character or stringusedtoindicate where astringshould be split.10.15 ExercisesExercise 10.3 Write a function called is_sorted that takes a list as a parameter and returns TrueifthelistissortedinascendingorderandFalseotherwise. Youcanassume(asaprecondition)thattheelements of the listcan be compared withthe relational operators,etc.For example, is_sorted([1,2,2]) should return True and is_sorted(['b','a']) should returnFalse.Exercise 10.4 Two words are anagrams if you can rearrange the letters from one to spell the other.Writeafunction calledis_anagramthat takes twostringsand returnsTrueif they areanagrams.Exercise 10.5 The (so-called) Birthday Paradox:1. Write a function called has_duplicates that takes a list and returns True if there is anyelement that appears more than once. It should not modify theoriginal list.2. If there are 23 students in your class, what are the chances that two of you have the samebirthday? Youcanestimatethisprobabilitybygeneratingrandomsamplesof23birthdaysandcheckingformatches. Hint: youcangeneraterandombirthdayswiththerandintfunctionintherandommodule.You can read about this problem at wikipedia.org/wiki/Birthday_paradox, and you can seemysolution atthinkpython.com/code/birthday.py.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!