CSC 221: Introduction to Programming
Fall 2012

HW 3: Python Functions

This assignment will involve writing several Python functions and performing experiments with those functions. You should begin by downloading a copy of hw3.pytxt and renaming it lastnameHW3.py, where lastname is your last name (e.g., reedHW3.py). 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: Weather Formulas

The first section of hw3.py contains several weather-related functions. You are to build upon these by defining your own functions.

  1. The provided windChill function calculates the wind chill index (the temperature experienced by an exposed human in winter). You are to define a related function, windChillC, that similarly calculates the wind chill but with input and output temperatures in degrees Celsius. Note: your function should make use of the existing windChill function by converting the input temperature into degrees Fahrenheit, calling windChill to calculate the wind chill index, and then converting the it back to degrees Celsius.

  2. Define two new Python functions, dewPoint and dewPointC, that calculate the dew point (the temperature at which water vapor condenses into dew). The dewPoint function should take two inputs, the temperature (in degrees Fahrenheit) and the relative humidity (a percentage between 0 and 100), and return the dew point (in degrees Fahrenheit) as defined by the following formula:

    The dewPointC function should similarly calculate the dew point, but with the input and output temperatures in degrees Celsius. Note: as before, it should make use of the existing dewPoint and temperature conversion functions.


Part 2: Random Sentences

The second section of hw3.py 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 from the random module, 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 random.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. random.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.


Part 3: Turtle Letters

The third section of hw3.py contains several functions for drawing shapes in turtle graphics. The O function displays a capital 'O' as a series of turtle movements, while the o function displays a lowercase 'o'. In class you will be assigned a letter of the alphabet, and must define similar functions that display that letter (both uppercase and lowercase versions). For example, if you were assigned the letter A, you would need to define drawing functions named A and a.

There are very specific conditions that your letter drawing functions must follow.