Vous êtes sur la page 1sur 2

CS 110

Fall 2019 Name:

Laboratory 4
Functional Decomposition

Use Atom and a UNIX shell to answer the following. As you work, demonstrate/discuss with a TA or
professor and have them mark this sheet at each checkpoint.
Log in to gemini. Enter the following commands
cs110
start Lab4
cd ~/110/Lab4

Run the Atom text editor on your local machine. From the toolbar, select File and Open File. In the location
bar at the top of the window, type (replacing username with your username):
\\gemini.cs.hamilton.edu\username\110\Lab4 and hit Enter. Open Lab4.py.

1. Look through and run Lab4.py. It generates and prints a random sentence by using a grammar,
which defines which parts of speech can appear in what order. The particular grammar used here can
be visualized as

Note that some parts of the grammar, such as noun phrase, are repeated multiple times in the grammar.
Each part of the grammar that is made up of smaller parts is implemented in the program as a function.
Thus the sentence function calls the noun_phrase and verb_phrase functions, and combines the
Staff Check:
strings they return. This breakup of a program into smaller functions is called functional decomposition.

2. Add a few new interesting words to the nouns, verbs, and prepositions lists.
Show us a sentence, and ask if you have questions about how the program works.

3. Create a function that takes a sentence and capitalizes the first letter of the first word (hint: use the
string.upper() method). Then, have it randomly add either a period or exclamation point to the end Staff Check:
of the sentence before returning the sentence. Call this function in your main function before printing
the sentence.
Let’s see a well-formatted sentence.

1
CS 110 Laboratory 4
Fall 2019 Functional Decomposition
Staff Check:
4. Make it so your program asks the user how many sentences to print and prints that many generated
sentences, instead of just printing one sentence.
Show us some sentences.

5. Write a function random_adjectives that takes an integer as its argument and returns that number
of randomly chosen adjectives, separated by spaces. It should work with 0, 1, or more as its argument.
You will need to create a list of adjectives.

6. Change the noun_phrase function so that it includes between 0 and 4 (inclusive) random adjectives Staff Check:
before each noun. Hint: random.randint(x, y) generates a random integer between x and y inclu-
sive.
Let’s see a sentence with adjectives.

7. Write a function repeated_word that takes a sentence as an argument, and returns True if the sentence
has any word in it twice in a row, and False otherwise. Hint: you’ll find it useful to use the split()
method on strings, which breaks a string into a list of words. Staff Check:
For each sentence you generate, print ***The previous sentence has repeated words*** after
sentences with repeated words in them.
Show us some nice nice nice sentences.
Staff Check:
8. Make your sentences properly use “an” before words that start with vowels and “a” before words that
start with consonants.
Oooh, nice articles!

9. Challenge Problem: Create a new grammar for sentences that have a different structure—be creative! Staff Check:
You’ll likely want to map out the grammar like in the diagram for Part 1. Then, implement your new
grammar with a whole new set of functions.
Let’s see the new sentences.

Congratulations! You’ve completed your fourth lab! Scan your lab sheet using the scanner before your leave.

How to scan your document Place your lab face down (front page down) with the top of the page in the
scanner. Press the lighted blue button. When you are done, take your lab sheet with you.
If you finish before the lab time is up, you should work on the current project.

Vous aimerez peut-être aussi