## CSC 221: Introduction to Programming Fall 2013

### HW 3: Python Functions

This assignment will involve writing several Python functions. You should save all of your functions in a single Python module named `lastnameFuncs` (where `lastname` is your last name). The module should have a comment block at the top, containing the file name, your name, the date, and a brief description. Each function should also have a doc string that describes it behavior.

This assignment is to be completed on your own, with assistance from the instructor as needed.

## 1. HW2 Rewrites

For the first part of this assignment, you will rewrite code from HW2 in function form. In particular, you will repackage your code into the following functions:

• `expectedRecord`: takes two inputs, the runs scored and runs allowed by a team, and returns the expected win-loss record for that team (as calculated by Bill James' Pythagorean formula). For example, `expectedRecord(649, 577)` would return the string "90-72".
• `relativeTime`: takes two inputs, a distance (in light years) and a velocity (in percentage of light speed), and returns the relative time for traveling that distance at that speed. For example, `relativeTime(309, 50)` would return 267.60184976939155.
• `timeTable`: takes one input, a distance (in light years), and prints a table of the relative times for traveling that distance at various percentages of light speed (10%, 25%, 50%, 75%, 90%, and 99%). This function should call the `relativeTime` function.

## 2. Random Sequences

The following Python function can be used to generate a random 3-letter sequence:

import random ALPHABET = "abcdefghijklmnopqrstuvwxyz" def randomSequence(): """Returns a random 3-letter sequence.""" seq = random.choice(ALPHABET) + random.choice(ALPHABET) + random.choice(ALPHABET) return seq
• Modify this function so that it has a parameter specifying the desired length of the random sequence. For example, `randomSequence(5)` should return a random 5-letter sequence, whereas `randomSequence(7)` should return a random 7-letter sequence.

Using your `randomSequence` function, it should be possible to approximate the number of words in the English language of a given length. For example, if you generated 10,000 random 4 letter sequences and determined that 100 of those sequences were words, you could estimate a 1/100 ratio of words to sequences. Since there are 264 = 456,976 different 4-letter sequences, you can estimate the number of 4-letter words as round((1/100) * 456,976) = 4,570.

• Download the dictionary.py module and save it in the same folder as your code module. This module contains a function that determines whether a letter sequence is an actual word in a (rather extensive) online dictionary. For example, after importing this module, `dictionary.isWord("banana")` would return `True` whereas `dictionary.isWord("bununu")` would return `False`.
• Utilizing this helper function, write a function named `howManyWords` that takes a single input, the word length, and returns an approximation of how many words there are of that length. Your function should use the above approach: generating 10,000 random sequences, counting the number of words (using `dictionary.isWord`), and multiplying the word-to-sequence ratio by the number of possible sequences of that length.