Introduction to Computer Science: The Way of the Program — Homework 7

Due by 11:59pm Tuesday, October 24

Reading

AutoTester Program

Download and unzip the folder assign7_files.zip, which contains the autotester file hw7tester.py and a few additional supporting files for this assignment, which you should use to verify that your programs work correctly. Put these files in the same location as the file containing your Python code for this assignment and put the line from hw7tester import * at the top of your file.


Programming Exercises

  1. Include your Pig Latin functions from Lab 7, and any helper functions that are needed for your code to work. Your Pig Latin functions do not need to handle capitalization and punctuation correctly — although if they do, that's great (see the Extra Credit section below). You do not need to include the ball-bouncing program. Checklist:


  2. Also complete the function spell() from Lab 7. Hint: have your program first create two lists of words, one containing the words of the file to be spell-checked, and the other containing all of the words in the dictionary (from webster2.txt). An easy way to create a list of the words of a file is to just call f.read().split(), where f is the file pointer (yes, you can combine multiple method calls like this in Python). Then cycle through each candidate word to be checked and use the in operator to see if the word appears in the list of dictionary words; or, in the case of candidate words ending in "s" or "ed", if their root form appears in the dictionary.


  3. A person is eligible to be a U.S. senator if they are at least 30 years old and have been a U.S. citizen for at least 9 years. To be a U.S. representative these numbers are 25 and 7, respectively. Write a program called congress() that accepts a person's age and years of citizenship as input — in that order — and outputs their eligibility for the Senate and House in the format shown below. For example:

    >>> congress()
    What is your age? 37
    How many years have you been a U.S. citizen? 37
    You could run for the Senate or the House of Representatives.
    
    >>> congress()
    What is your age? 27
    How many years have you been a U.S. citizen? 10
    You could run for the House of Representatives.
    
    >>> congress()
    What is your age? 30
    How many years have you been a U.S. citizen? 5
    Sorry, you'd better stay out of politics.
    

  4. Write a function called allEven(numlist) that takes a non-empty list of positive numbers as input and returns True if all numbers in the list are even, or else False. Be sure to include square brackets around the numbers when calling allEven, as shown in the examples below, since it expects a list.


  5. Write a function called isValid(month, day, year) that takes a month number, a day number, and a year as input parameters, and checks whether or not the date is valid, using the isLeapYear(year) and numDays(month, year) functions that we wrote in class as helpers. Your isValid function should return either True or False. To be a valid date, month must be in the range 1-12, day must be in the range 1 to the number of days in the month, and year must be greater than zero. For example:


EXTRA CREDIT PROBLEMS (OPTIONAL)

You should finish the other problems first before working on these.

  1. Write a new version of your translateWord function from Lab 7 called translateWord2(word) that correctly handles capitalization and punctuation. For simplicity, you may assume that a capital letter will only occur at the beginning of a word (not in the middle), and that a punctuation mark will only occur at the end of a word (such as a trailing comma or period). Hint: first check if the word is capitalized or if it ends in a punctuation mark. If so, use variables to remember the relevant information. Then strip off the punctuation mark and/or de-capitalize the word. Then, after turning it into its Pig Latin form, restore the capitalization and/or punctuation mark before returning the final word. For example:


  2. In mathematics, a complex number is a combination of an ordinary "real" number and a so-called "imaginary" number based on the special constant i, which is defined to be the square root of -1. For example, the complex number 3+2i is the sum of the real value 3 and the imaginary value 2i, and the complex number -4-5i is the sum of -4 and -5i. A complex number like 3+0i having an imaginary part of 0i is equivalent to an ordinary real number and is written as "3", without the imaginary part. Likewise, numbers like 0+3i are written as just "3i", without the real part or leading + sign. By convention, if the imaginary part is +1i or -1i, it is written as "i" or "-i", without the leading 1.

    Write a function called formatComplex(a, b) that takes two ordinary numbers a and b representing the complex number a+bi, and returns a string version of the number (containing no spaces) formatted according to the rules outlined above. You should use if/elif/else statements in your code to correctly handle each case. The table below shows the correct formatting for various examples of complex numbers. Hint: you can use the function str(num) to turn an ordinary number into a string.

       Function call        String returned
    ---------------------------------------
    formatComplex(0, 0)         '0'
    formatComplex(3, 0)         '3'
    formatComplex(-5, 0)        '-5'
    formatComplex(0, 1)         'i'
    formatComplex(0, -1)        '-i'
    formatComplex(0, 3)         '3i'
    formatComplex(0, -5)        '-5i'
    formatComplex(3, 2)         '3+2i'
    formatComplex(3, -2)        '3-2i'
    formatComplex(1, -1)        '1-i'
    formatComplex(-2, 1)        '-2+i'
    

  3. Write a program called calendar(month, year) that prints out a nicely formatted calendar for any month and year from January 1900 or later. You should use the functions isLeapYear(year) and numDays(month, year) as helpers, and define the following additional helper functions:


Turning in Your Homework