CS 3723
Programming Languages  
Fall 2014
Homework 3. Python REs
Week 3: Sep 8 - 12

Submit following directions at: submissions and rules at: rules. Deadlines are:
  • 2014-09-22  23:59:59 (that's Mon, 22 Sep 2014, 11:59:59 pm) for full credit.
  • 2014-09-26  23:59:59 (that's Fri, 26 Sep 2014, 11:59:59 pm) for 75% credit.


Python and Regular Expressions: Python is a fully object-oriented language. In particular, Python regular expressions are implemented in an object-oriented fashion. (Python does not have anything corresponding to the $0, $1, $2, ... variables that Perl uses.) See Regular Expressions for help with this homework.


Initial Python Program: Write, debug, and run your program. You must use regular expressions in your program:

  1. Suppose you have input data giving student names and student numbers in the following form:

    Input Data to Process
    17 Dennis A. Andrade  @00002222
    18 Isabel Carrera     @00555555
    19 Carlton W. Creech  @00444444
    20 Philip R. Hayden   @00044444
    21 Jason R. Luna      @00077777
    22 Manuel Neri        @00222222
    

    We want to rewrite these in a different way. This assumes that the last name is always followed by one or more blanks and then an '@' character. There may or may not be a middle initial. You should drop the initial number, write the student number without leading 0's, and write the last name first, followed by a comma, followed by a blank and the rest of the name, as shown below:

    Proper Output Data
    2222 Andrade, Dennis A.
    555555 Carrera, Isabel
    444444 Creech, Carlton W.
    44444 Hayden, Philip R.
    77777 Luna, Jason R.
    222222 Neri, Manuel
    

    Write a Python program using regular expressions to do this translation. [The comments above point to two minor problems that come up with this program: first to eliminate the leading zeros, and second the names with no middle initial. Be sure to submit even if you can't deal with these problems. You may ask me for a hint about these if you want.]


Two More Python Programs: Again these are to write, debug, and run. They each involve the representation of time and date. Python has an extensive datetime module, but you are not allowed to use it. (In a work environment, professional programmers tend to use library modules when they can. Companies don't want to pay their employees to "reinvent the wheel." This is an artificial learning environment.)

Consider the following two representations of date and time:

"American" style International styleComments
10:03 pm, April 20, 20042004-04-20 22:03:00 (random)
  8:04 am, January 4, 19981998-01-04 08:04:00 (random)
11:59 am, July 4, 20122012-07-04 11:59:00 1 min. < next entry
12:00 pm, July 4, 20122012-07-04 12:00:00 This is noon
11:59 pm, December 31, 20032003-12-31 23:59:00 1 min. < next entry
12:00 am, January 1, 20042004-01-01 00:00:00 This is midnight


  1. Write a Python program that translates International style dates to "American" style dates:
    1. Read a sequence of International style dates, one to a line, and translate these into the American style. If you wish you can hardwire the data into your program, as with the Python Debug Program.
    2. Use Python regular expressions as an essential part of the program. (Follow the model of the regular expression programs on the page Python Regular Expressions.)
    3. To help with the translation, you must use the following Python dictionary (which is very easy to use, see Python Dictionaries):

      Use of Python Dictionary
      % python
      >>> mon = {'01': 'January', '02': 'February', '03': 'March',
                 '04': 'April',   '05': 'May',      '06': 'June', 
                 '07': 'July',    '08': 'August',   '09': 'September', 
                 '10': 'October', '11': 'November', '12': 'December'}
      >>> mon['06']
      'June'
      # order does not matter, could also be:
      >>> mon = {'02': 'February', '10': 'October',   '01': 'January', 
                 '06': 'June',     '07': 'July',      '04': 'April', 
                 '03': 'March',    '08': 'August',    '05': 'May', 
                 '12': 'December', '09': 'September', '11': 'November'}
      >>> mon['08']
      'August'
      

    4. Notice that in the first sample date above, the hour of "22" must be changed to a "10" with "pm" at the end. Notice also that in the second sample date above, the "08" for hours and the "04" for days must be written in American style without the leading "0".
    5. Notice that the "American"style leaves off the seconds.
    6. You should use a function for the translation, again as with the Python Debug Program. In my program, I built up a single string as the answer and returned it from the function.
    7. Do not check for input errors.


  2. Write a program similar to the one in 2) above that will translate from American style to International style.
    1. This is similar code, but perhaps a little harder.
    2. Here the regular expression is harder to write and to debug. Check out Debugging Regular Expressions for some advice about doing the debugging.
    3. Assume some reasonable definition for the "American" style, as shown above.
    4. Since seconds are left off in the "American" style, you should put :00 back into the International style.
    5. As in 2 above, do not check for input errors.)
    6. As in 2 above, you must use the following dictionary to help with the translation (inverse of the previous one; see Inverting a Dictionary):

    Python Dictionary Inverting Program
    mon = {'01': 'January', '02': 'February', '03': 'March',
           '04': 'April',   '05': 'May',      '06': 'June', 
           '07': 'July',    '08': 'August',   '09': 'September', 
           '10': 'October', '11': 'November', '12': 'December'}
    mon_inv = {} # create empty dictionary
    for (k, v) in mon.items():
        mon_inv[v] = k # add (v, k) to mon_inv
    
    Output
    {'February': '02', 'October': '10',   'March': '03', 
     'August': '08',   'May': '05',       'December': '12', 
     'June': '06',     'September': '09', 'April': '04', 
     'January': '01',  'November': '11',  'July': '07'} 
    Use of the Inverse
    >>> mon_inv['June']
    '06'
    


What you should submit: Refer to the submissions directions and to deadlines at the top of this page. If two people are working together, be sure that both signed in as a pair and that both names appear at the top of the submission.

  • All three parts should be similar to the example in the regular expression page. You must give the program source and results of a run with the data given.

  • For full credit on 2) and 3), you might have two Python programs that will translate in each direction. You should test each program at least with the six examples shown in the write-up above. However, you can also put everything into one program, which is what I did, producing two functions, one for each of the translations. You should get all the little details of the data correct. (I spent a couple of hours getting all these details correct.)

  • But don't get too compulsive about the details. You can make reasonable assumptions without asking me if it's all right. (Only you and the NSA will know if every detail is correct anyway.)

( Revision date: 2014-07-19. Please use ISO 8601, the International Standard Date and Time Notation.)