CS 3723
Programming Languages  
Fall 2014
  Homework 0. Copy Program  
Week 0: Before classes

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

[Refer to the webpage: Python for information about the Python language.]

For this homework you are to type in the first Python program given below and run it. This homework has several objectives:

  • Start working on the Python language. In particular, the Python program you are to copy uses three specific programming methods:

    • Reading from a text file.
    • Using regular expressions to extract fields from a string.
    • Using Python's "list" data type. (A Python list has all the features of an array and of a linked list, plus many more.)

  • Learn to write and run programs in Linux (on the Elk machines) in case you're not already familiar with this environment. Note: You may program in Python wherever you might have access to the language: Python is usually already present on Apple and Linux machines, and it is available for download on Windows.

  • Practice using email to submit homework in a way required for the course.
Note: As a "copy" program, you might think the idea would be to get one class member to type it in and email it to everyone else. Instead, each of you should type it in by yourself from scratch. (There is significant benefit from typing it yourself. If you make typing errors, so much the better.)


Running a Python Program: Assume your program is in the file "h0.py". Execute the command:
    % python h0.py
where "%" stands for the Linux prompt. Better is to use the command:
    % python h0.py -tt
With the "-t" option, Python issues a warning if there are any tabs, and "-tt" makes any tabs an error.

        Alternatively, you can add the following as the first line of the file h0.py:
          #!/usr/bin/python -tt
          

        And then type:

          % chmod +x h0.py # make h0.py executable
          % ./h0.py # execute, using first line to find python
          

        I mostly won't use this method (more trouble than it's worth for writing sample programs), but it is very useful for systems programming and larger production work.


The Program for you to type in and run: Type the program shown in the box below. Store it using the name  h0.py . (The name must end in  .py ) Copy the data file  students.txt  into the same directory as the Python program. Then execute as shown above.

Here is the program that you are to type in and run. It is given as an image:

Larger copy of the above image: image
Machine readable copy of input file: students.txt

Notes:

  • In order to produce just this output, the list "ave" is not needed, since the program could compute a running sum. I wanted to illustrate lists.
  • Python mostly has no declarations of variables, and a given variable can mostly be used for any type you like, or even for several types in the same program.
  • When you are inside parens (as in lines 8-9 and 28-29 above), you may indent on the second line in any way you like. Otherwise, the indenting should be exactly as shown above, with exactly 4 spaces for each level of indenting, and with no tab characters. (I was able to set my editor so that the Tab key always produces 4 spaces and no tabs.)
  • My favorite mistake is to leave off the ":" character just before a new level of indenting, or to use ";".
  • The "strip( )" in line 19 strips off any whitespace characters from the start or end of the character string "line". In this case there was only a newline at the end. The "strip( )" method makes a new copy of the string for use in line 19, but the variable "line" retains the newline. You would need to write "line = line.strip( )" to get rid of the newline from the variable "line".
  • Python regular expressions are mostly the same as in any other scripting language, but there are significant notational differences. Python has nothing like the "$" variables that can be used in Perl to refer to the different matching groups. Instead you must use the "group( )" method as shown on lines 13-15. Thus  "m.group(3)"  takes on the role that  "$3"  has in Perl. The result is a string, and lines 13-15 use the function  "float( )"  to convert this string to floating point (a double).


Parts of Python Illustrated by this Program:

  • Reading from a text file: "students.txt" is a file of records in Linux/Unix where each record is terminated with a newline ("\n"). (In Windows, records are often terminated with a carriage return and a linefeed.)

    Line 5 is an iterator that provides each record of the file in sequence. The record retains its newline at the end (in Unix). Lines 12-17 form a while loop that explicitly reads each record and explicitly stores the record in the variable "line". (The name "line" is an arbitrary choice.)

    Below is a program that fakes having a file, "opening" it, and "reading" from it. This can be convenient if you interact with a terminal window that has Python but doesn't support input. Notice that the only thing changed below is the new definition of the variable f (in red).


  • Formatted input using a regular expression: Here we don't show the input, but show how the fields of one string are extracted.

    The regular expression (RE) below is what comes between the quote marks in  r"xxx" . In this case it is "xxx". Each RE describes a collection of character strings. (The above describes only a triple of "x"s.) In the example here, we want to describe the contents of lines in the file. Short-hand notation gives us "\s" for white space, "\d" for a digit, and "\w" for a character in a word (upper-lower case letter). Adding a "+" to the right means "one or more occurrences of". Anything inside parentheses are matched and available for use. (A parenthesis itself is represented by "\(" or "\)".) The "compile" method makes the RE ready for use, and the "search" method searches for any matches available. There's a lot more to this, and we'll go over it later.


  • Making use of a Python list: In Python, the common and versatile data structure "list" is a combination of all the features of a linked list and of an array, along with much more besides.


How to Submit: You should email this homework to a special account of mine that is only for submission of homework. Of course δοτ below is a "dot" and ατ is an "at-sign".
    <nealδοτwagnerδοτextraατgmailδοτcom>


What to Submit: As is always the case, you should submit:
  • the program source (.py file),
  • the input data (.txt file), and
  • the result of running the program, using Python in this case.

I (or the TA) will grade this first program. Although in this case the grading is mostly a formality, the email response gives you a confirmation, so you will know that the process seems to be working. Remember, that the email address with the word "extra" in it is for submission of all homework in this CS 3723 course and for nothing else. (Only submissions. Everything else should be sent to my regular gmail account.)

It is particularly important that each student separately email his or her own copy program that they typed in themselves.


Don't Forget the Subject Header: Your header line for this submission should be: "H0, <your last name>, <your first name>", where you put in your actual last name and first name. Thus a student named Bruce Wayne should use
    H0, Wayne, Bruce
for the subject line.

Don't be creative, but be uniform, with just a single capital "H" followed by the homework number, followed by the last name and first name, each item separated by ", " from the next.

Repeat the subject line at the start of the contents. There is no need to bother writing "CS 3723" or "Programming Languages" anywhere in the submission.


Suppose you need to use Python from a browser: The best case is to use the
    ideone Python 2 and 3 simulator.
The link is set for Java by default. You have to change a box from Java to Python or to Python 3.

You can copy the whole students.txt file into their spot for stdin. You have to read from the file sys.stdin. One way is to use the line

    for  line  in  sys.stdin:
as shown above. (In place of line 10 in the original program. You also need to delete or comment out line 4, the line that opens the file.)

( Revision date: 2014-08-17. Please use ISO 8601, the International Standard.)