A202 / I211 Assignment 3

Working with strings

Due Thursday, September 23rd, 3:30 PM

In lab

Individual (not team) work this week.

  1. Create a file named lab3.py with a function named multTable that prints the following multiplication table with the numbers right-justified in columns of width three, separated by a space. Use print formatting. Hint: use doubly-nested for loops..
    >>> multTable()
      1   2   3   4   5   6   7   8   9
      2   4   6   8  10  12  14  16  18
      3   6   9  12  15  18  21  24  27
      4   8  12  16  20  24  28  32  36
      5  10  15  20  25  30  35  40  45
      6  12  18  24  30  36  42  48  54
      7  14  21  28  35  42  49  56  63
      8  16  24  32  40  48  56  64  72
      9  18  27  36  45  54  63  72  81
    >>> 
  2. Extracting information from the web automatically often involves returning retrieving the characters between certain HTML tags. The title tag is important because it often indicates what a web page is about. Add to your file a function named getTitle that takes a string containing opening and closing HTML title tags and returns the title itself: the characters between the tags. The opening and closing tags are <title> and </title>, respectively. HTML tags are not case sensitive, so the word title may be in either upper or lower case, or even mixed case.
    >>> getTitle("<html><head><title>Elmer's Wonderful Web</title></head>")
    "Elmer's Wonderful Web"

    Hint: Make a local copy of the string that is converted to all lower (or upper) case. Search for the indices of the start and end of the title using this copy, and then use them to pick the slice of characters in the argument string that contains the substring to be returned.
     

  3. Add to your file a function named numbersToStrings that takes a list of numbers and returns a list of strings whose elements represent the corresponding elements of the number list. Hint: iterate over the elements of the number list building up the string list in an 'accumulator' variable.
    >>> numbersToStrings([1, 2, 3.5])
    ['1', '2', '3.5']

When you have completed the last exercise above, or 15 minutes before the lab ends, whichever comes first, submit your lab3.py file as lab 3 in Vincent, by carefully following the instructions on the course web's Vincent page, linked in the contents panel on the left.

If you have finished the in-lab portion of an assignment before the end of a lab session you may leave, but you are strongly encouraged to stay and start work on the main portion of the assignment. That way you can get help right away if you need it.

Assignment

  1. In a file named futvalTable.py (named after the last part of the assignment) start with the required assignment string and then define a function date that takes a string indicating a date in the common mmddyy format and returns a string indicating the same date in another common format: <Month> <day>, <year>, where <Month> is spelled out and capitalized, and <year> is four digits of the form 20yy).
     
    >>> date('091101')
    'September 11, 2001'
    

    In this program the indexes for selecting parts of the mmddyy string need not be considered magic numbers if they are part of assignments to well-named variables.

     

  2. In the same file, define a function initials that takes a string with a first, middle, and last name, and returns a string with the three initial letters of the three parts of the name, converted to lower case. Assume there are exactly two spaces in the whole name (so for example a last name with a space in it is not allowed).
    >>> initials('Martin Luther King')
    'mlk'
  3. Write a function percentToLetterGrade that takes a number from 0 to 100 and returns the corresponding letter in the traditional grade interpretation of 90-100 : A, 80-89 : B, 70-79 : C, 60-69: D, and 0-59: F. Do this by using the percent grade to index into a 101 character string. Since this string is a constant (always the same) it should be stored in a variable outside the function. Hint: use the string repetition operator in the construction of this string, which is a bit less work, and better style because it is easier to see that it is correct.
     
  4. At the end of your file insert the main method, and the call to main at the very end, from a futval.py solution to assignment 1. You may use either your solution or the solution posted on the course web. Modify it so that it prints the future value at the end of each period for the given number of years in a nicely formatted table. The table has a year column that is 5 characters wide followed by a 10 character wide column for each of the periods, with no additional spaces between the columns and numbers right justified in their columns. The table begins with two header lines: the first with just the word period centered over the period columns and the second with the word year right justified in the year column and the period number (numbering from 1) right justified in each of the period columns. For example, here are to trials separated by the usual IDLE shell restart output.:
    Enter the amount invested each year: 100
    Enter the annual interest rate: .05
    Enter the number of compounding periods per year: 6
    Enter the number of years: 20
                                     period                           
     year         1         2         3         4         5         6 
        0    100.83    101.67    102.52    103.38    104.24    105.11 
        1    206.81    208.54    210.28    212.03    213.80    215.58 
        2    318.21    320.86    323.53    326.23    328.95    331.69 
        3    435.29    438.91    442.57    446.26    449.98    453.73 
        4    558.34    562.99    567.69    572.42    577.19    582.00 
        5    687.68    693.41    699.19    705.02    710.89    716.81 
        6    823.62    830.49    837.41    844.38    851.42    858.52 
        7    966.50    974.56    982.68    990.87    999.13   1007.45 
        8   1116.68   1125.99   1135.37   1144.83   1154.37   1163.99 
        9   1274.52   1285.14   1295.85   1306.65   1317.54   1328.52 
       10   1440.43   1452.43   1464.53   1476.74   1489.04   1501.45 
       11   1614.80   1628.25   1641.82   1655.50   1669.30   1683.21 
       12   1798.07   1813.06   1828.16   1843.40   1858.76   1874.25 
       13   1990.70   2007.29   2024.02   2040.89   2057.89   2075.04 
       14   2193.17   2211.44   2229.87   2248.45   2267.19   2286.09 
       15   2405.97   2426.02   2446.24   2466.62   2487.18   2507.90 
       16   2629.64   2651.55   2673.65   2695.93   2718.39   2741.05 
       17   2864.72   2888.59   2912.66   2936.94   2961.41   2986.09 
       18   3111.81   3137.74   3163.89   3190.25   3216.84   3243.64 
       19   3371.51   3399.60   3427.93   3456.50   3485.30   3514.35
    >>> ================================ RESTART ================================
    >>> 
    Enter the amount invested each year: 1000
    Enter the annual interest rate: .07
    Enter the number of compounding periods per year: 4
    Enter the number of years: 10
                           period                 
     year         1         2         3         4 
        0   1017.50   1035.31   1053.42   1071.86 
        1   2108.12   2145.01   2182.55   2220.74 
        2   3277.10   3334.45   3392.81   3452.18 
        3   4530.09   4609.37   4690.03   4772.11 
        4   5873.12   5975.90   6080.48   6186.89 
        5   7312.66   7440.63   7570.84   7703.33 
        6   8855.64   9010.61   9168.30   9328.74 
        7  10509.50  10693.41  10880.55  11070.96 
        8  12282.20  12497.14  12715.84  12938.36 
        9  14182.29  14430.48  14683.01  14939.96
    >>> 
    

    Extra credit:  define two constants for the width of the year and period columns and use these in the format specifications (which then have to be assembled with string concatenations). This way the formatting of the entire table can be changed by just changing these two constants.

When you are done, submit your final futvalTable.py file as a3 using Vincent. As always, if you cannot finish all of the assignment, be sure to submit, before the due time, a version of your file that does as much as you can get working.