CSC 221: Introduction to Programming
Fall 2011

HW 2: Python Functions

This assignment will involve writing several Python functions and performing experiments with those functions. You should begin by downloading a copy of hw2.pytxt and renaming it, where lastname is your last name (e.g., Your functions should be defined in this file, and should all follow the style of existing functions (e.g., meaningful names, doc strings).

Note: this work must be entirely your own, with no outside assistance other than the instructor.

Part 1: Mach Speed

The top section of contains two functions for converting between feet and meters: feetToMeters and metersToFeet. You are to build upon these by adding functions related to Mach speed.

  1. Define two new Python functions, fpsToMach and machToFps, that convert between feet/second and Mach speed. At Standard Sea Level conditions, Mach 1 (i.e., the speed of sound) is 1,116 feet/second. Each increase of 1,116 feet/second is another Mach level. For example, Mach 2 is 2,232 feet/second.

  2. Define two new Python functions, mphToMach and machToMph, that convert between miles/hour and Mach speed. You should make use of your fpsToMach and machToFps functions here. For example, to convert from miles/hour to Mach speed, first convert from miles/hour to feet/second, then convert to Mach speed using the fpsToMach function you just wrote. Note that there are 5,280 feet in a mile and 3,600 seconds in an hour.

  3. Define four new Python functions that perform similar conversions, only using the metric system. That is, mpsToMach and machToMps should convert between meters/second and Mach speed, and kphToMach and machToKph should convert between kilometers/hour and Mach speed. Again, you should make use of the provided functions (feetToMeters and metersToFeet) as well as your previously defined functions.

Part 2: Random Sentences

The bottom section of contains several functions for generating random sentences. The article, noun, and verb functions each return a randomly selected word of that type. The sentence function calls each of these other functions to construct a simple sentence consisting of an article, noun, and verb (in that order). For example, "the man kicked." This simple sentence structure can be defined by a grammar rule:

sentence <-- article + noun + verb

  1. Augment each of the article, noun, and verb functions to incorporate at least 6 words of each type.

  2. Now consider the following grammar rules that define a more complex sentence structure. sentence <-- nounPhrase + verbPhrase nounPhrase <-- article + adjective + noun + prepPhrase verbPhrase <-- verb + nounPhrase prepPhrase <-- preposition + article + noun

    Update your code to generate sentences based on these grammar rules. In particular, you must define new functions called adjective, preposition, nounPhrase, verbPhrase, and prepPhrase that return the appropriate type of word or phrase. For example, the call sentence() might produce "the big man by a tree hit a green ball above some woman" or "some nice woman beside the ball liked a big man by the house". Note: there should be exactly one space between words.

  3. The grammar rules in the previous exercise leave no options when constructing a sentence. Every sentence generated using these rules will have the exact same structure, even the same number of words (13). In practice, sentences and phrases can be defined with optional parts. For example, the adjective and prepositional phrase in a noun phrase are not necessary, since "the man", "the nice man", "the man by the tree", and "the nice man by the tree" are equally valid. Similarly, a verb phrase does not always require a noun phrase after the verb. Consider the following generalized grammar rules, where the notation ?PART_OF_SPEECH? denotes an optional part of speech: sentence <-- nounPhrase + verbPhrase nounPhrase <-- article + ?adjective? + noun + ?prepPhrase? verbPhrase <-- verb + ?nounPhrase? prepPhrase <-- preposition + article + noun

    From looking at these grammar rules, what is the fewest number of words possible in a sentence? Similarly, what is the greatest number of words possible in a sentence? Justify your answers.

  4. In order to generate sentences based on these more flexible grammar rules, you must modify the nounPhrase and verbPhrase functions so that they sometimes include the optional parts of speech and sometimes they don't. The choice function, which randomly chooses from a list of options, will prove useful here. In particular, whenever you want an optional part of speech, you can call the choice function with two options to select from: the part of speech and the empty string "". For example, the following call will either return an adjective or the empty string. choice([adjective(), ""])

    Note: as before, there should be exactly one space between words in the sentence.

  5. Using the new versions of your grammar functions, generate 20 random sentences and report them.