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.

124 Chapter 12. Tuples['deltas', 'desalt', 'lasted', 'salted', 'slated', 'staled']['retainers', 'ternaries']['generating', 'greatening']['resmelts', 'smelters', 'termless']Hint: you might want to build a dictionary that maps from a set of letters to a list of wordsthatcanbespelledwiththoseletters. Thequestionis,howcanyourepresentthesetoflettersinawaythat can beused as akey?2. Modifythepreviousprogramsothatitprintsthelargestsetofanagramsfirst,followedbythesecond largest set,and soon.3. In Scrabble a “bingo” is when you play all seven tiles in your rack, along with a letter on theboard, to form an eight-letter word. What set of 8 letters forms the most possible bingos?Hint: there areseven.4. Two words form a “metathesis pair” if you can transform one into the other by swappingtwo letters 3 ; for example, “converse” and “conserve.” Write a program that finds all of themetathesispairsinthedictionary. Hint: don’ttestallpairsofwords,anddon’ttestallpossibleswaps.You can download asolutionfromthinkpython.com/code/anagram_sets.py.Exercise 12.5 Here’sanother Car Talk Puzzler 4 :What is the longest English word, that remains a valid English word, as you remove itslettersone at atime?Now,letterscanberemovedfromeitherend,orthemiddle,butyoucan’trearrangeanyof the letters. Every time you drop a letter, you wind up with another English word.If you do that, you’re eventually going to wind up with one letter and that too is goingto be an English word—one that’s found in the dictionary. I want to know what’s thelongest word and how many lettersdoes ithave?I’m going to give you a little modest example: Sprite. Ok? You start off with sprite,you take a letter off, one from the interior of the word, take the r away, and we’re leftwith the word spite, then we take the e off the end, we’re left with spit, we take the soff, we’releftwithpit,it,and I.Writeaprogram tofind allwords that can bereduced inthisway, and then find thelongest one.This exercise isalittlemorechallenging than most, sohere are somesuggestions:1. You might want to write a function that takes a word and computes a list of all the words thatcan beformed by removing one letter. These arethe “children” of theword.2. Recursively, a word is reducible if any of its children are reducible. As a base case, you canconsider the empty stringreducible.3. The wordlist I provided, words.txt, doesn’t contain single letter words. So you might wanttoadd “I”, “a”, and theempty string.4. To improve theperformance of your program, you might want tomemoize thewords thatareknown tobereducible.You can see mysolutionatthinkpython.com/code/reducible.py.3 This exercise is inspired byanexampleatpuzzlers.org.4 www.cartalk.com/content/puzzler/transcripts/200651.

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

Saved successfully!

Ooh no, something went wrong!