Chapter 9: Case Study – Word Play

 

 

Think Python Chapter 9: Case Study / Word Play

Think Python Chapter 9: Case Study / Word Play

Hi there!

 

I talked to my mentor (yes, after all this time I just found one!) about my increased frustration in solving certain problems from the book. For instance, I spend two weeks trying to solve by my own the exercises from chapter 4. It drove me literary crazy! Plus, I had the impression I was stagnating and start thinking about … maybe I am not good enough for this! … I know, hard stuff!

 

My mentor’s advice was: “Try to solve the problems in a given time (15, 30, 60 minutes). Then, peek at the answers and spend some time comparing your logic with the answer from the book.”

So, with this piece of advice, let us start Chapter 9.

9.1 Reading word lists

 

Python can read plain text files.

 

The built-in function open takes the name of the file as a parameter and returns a file object you can use to read the file.

 

file object: A value that represents an open file.

 

 

Open text files with Python

Open text files with Python

Python has several opening modes:

  • ‘r’ indicates that the file is open for reading
  • ‘w’ indicates that the file is open for writing

 

The file object provides several methods for reading including readline , reads characters from the file until it gets to a newline and returns the result as a string.

 

Assume that the file “names.txt” includes this list:

alexandra

alibaba

alysson

bethany

catherine

eleanor

francesca

giovanna

 

The first word in the ‘names’ list is “alexandra”.

 

Read text files with Python

Read text files with Python

 

We can get rid of the whitespace with the string method strip.

 

>>> fin = open ( ‘names.txt’ )

>>> line = fin.readline ( )

>>> name = line.strip ( )

>>> print name

alibaba

 

A file object can also be part of a for loop:

 

fin = open ( ‘names.txt’ )

for line in fin:

name = line.strip ( )

print name

 

9.3 Reading word lists

 

In this section Professor Downey points out that the exercises from Section 9.2 could be solved with the search pattern in Section 8.6.

problem recognition: It basically means not to re-invent the wheel. “[You] recognize the problem you are working on as an instance of a previously-solved problem, and apply a previously-developed solution.

 

9.4 Looping with indices

When we work with indices, many ways lead to Rome.

 

Looping with indices

Looping with indices

9.5 Debugging

So far, the programs we have written can be verified by hand (computing or searching /finding ourselves). However, we should start testing programs in a more systematic way.

 

Let us test the program “has_no_e”:

  • Test no1) Test words with an “e” at the beginning
  • Test no2) Test words with an “e” at the end
  • Test no3) Test words with an “e” somewhere in the middle
  • Test no4) Test long words
  • Test no5) Test short words
  • Test no6) Test very short words
  • Test no7) Perform the special case test. This test can

 

Special case: A test tat is typical or non-obvious (and less likely to be handled correctly).

 

 

***

Acknowledgments :

These notes represent my understanding from the book Think Python: How to Think Like a Computer Scientist written by Allen B. Downey.

Part of the chapter is transcribed and all the quotes unless specified otherwise come directly from his book.

Thank you Professor Downey for making this knowledge available.

 

Also, I would like to thank the open source community for their valuable contribution in making resources on programming available.

Thank you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s