O'Reilly logo

A Concise Introduction to Programming in Python by Mark J. Johnson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 15

Files

Working with large blocks of text in a file opens up a whole new range of possibilities for interesting programs. The following program solves Jumble puzzles, which ask the solver to figure out an unknown word given a scrambled set of its letters.

One of the difficulties in solving this problem is that long words have so many different reorderings, called permutations. The strategy employed here is to create a unique signature for each word, so that a word has the same signature as all of its permutations. This program also finds anagrams, since typing in an unscrambled word will find all of its anagrams.

Listing 15.1: Solve Jumble

 1 # solvejumble.py
 2
 3 def signature(word):
 4 chars = list(word)
 5 chars.sort()
 6 return ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required