12.07.2015 Views

Think Python - Denison University

Think Python - Denison University

Think Python - Denison University

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

80 Chapter 8. Stringsdef any_lowercase1(s):for c in s:if c.islower():return Trueelse:return Falsedef any_lowercase2(s):for c in s:if 'c'.islower():return 'True'else:return 'False'def any_lowercase3(s):for c in s:flag = c.islower()return flagdef any_lowercase4(s):flag = Falsefor c in s:flag = flag or c.islower()return flagdef any_lowercase5(s):for c in s:if not c.islower():return Falsereturn TrueExercise8.12 ROT13isaweakformofencryptionthatinvolves“rotating”eachletterinawordby13places 1 . Torotatealettermeanstoshiftitthroughthealphabet,wrappingaroundtothebeginningifnecessary, so’A’shiftedby 3is’D’and ’Z’shiftedby1is’A’.Write a function called rotate_word that takes a string and an integer as parameters, and thatreturns anew stringthat contains thelettersfrom theoriginal string“rotated” by thegiven amount.For example, “cheer” rotated by 7is“jolly” and “melon” rotated by-10 is“cubed”.Youmightwanttousethebuilt-infunctionsord,whichconvertsacharactertoanumericcode,andchr,which converts numeric codes tocharacters.Potentially offensive jokes on the Internet are sometimes encoded in ROT13. If you are not easilyoffended, find and decode someof them.1 Seewikipedia.org/wiki/ROT13.

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

Saved successfully!

Ooh no, something went wrong!