Leap of faith

Following the flow of execution is one way to read programs, but it can quickly become labyrinthine. An alternative is what I call the leap of faith. When you come to a function call, instead of following the flow of execution, you assume that the function works correctly and returns the right result. In fact, you are already practicing this leap of faith when you use built-in functions. When you call math.cos or math.exp, you don't examine the bodies of those functions. You just assume that...

Exercise 12.3 Write A Function Called Most Frequent That Takes A String And Prints The Letters In Decreasing Order Of

Exercise 12.3 Write a function called most_frequent that takes a string and prints the letters in decreasing order of frequency. Find text samples from several different languages and see how letter frequency varies between languages. Compare your results with the tables at wikipedia.org wiki Letter_frequencies. 1. Write a program that reads a word list from a file see Section 9.1 and prints all the sets of words that are anagrams. Here is an example of what the output might look like 'deltas',...

Write A Function Named Avoids That Takes A Word And A String Of Forbidden Letters

There are solutions to these exercises in the next section. You should at least attempt each one before you read the solutions. Exercise 9.2 In 1939 Ernest Vincent Wright published a 50,000 word novel called Gadsby that does not contain the letter e. Since e is the most common letter in English, that's not easy to do. In fact, it is difficult to construct a solitary thought without using that most common symbol. It is slow going at first, but with caution and hours of training you can gradually...

Exercise 6.4 Draw A Stack Diagram For The Following Program

Exercise 6.4 Draw a stack diagram for the following program. What does the program print prod a z, z print z, prod return prod def a x, y x x 1 return x y sum x y z pow b sum 2 return pow Exercise 6.5 The Ackermann function, A m,n , is defined3 Write a function named ack that evaluates Ackerman's function. Use your function to evaluate ack 3, 4 , which should be 125. What happens for larger values of m and n Exercise 6.6 A palindrome is a word that is spelled the same backward and forward, like...

Word frequency analysis

As usual, you should at least attempt the following exercises before you read my solutions. Exercise 13.1 Write a program that reads a file, breaks each line into words, strips whitespace and punctuation from the words, and converts them to lowercase. Hint The string module provides strings named whitespace, which contains space, tab, newline, etc., and punctuation which contains the punctuation characters. Let's see if we can make Python swear gt gt gt import string gt gt gt print...

Reading word lists

For the exercises in this chapter we need a list of English words. There are lots of word lists available on the Web, but the one most suitable for our purpose is one of the word lists collected and contributed to the public domain by Grady Ward as part of the Moby lexicon project1. It is a list of 113,809 official crosswords that is, words that are considered valid in crossword puzzles and other word games. In the Moby collection, the filename is 113809of.fic I include a copy of this file,...