CS 8, Spring 2017

Programming Project 3

Due: Sunday, June 4, 11:59pm
Worth: 100 points

Begin a new file named countchars.py by typing a comment at the top with your name (and lab partner's name if you work together), and the current date.

You'll notice the instructions are a bit shorter this time. You are given a problem to solve, but you must figure out how to solve it yourself. Keep in mind what you've learned about top-down programming by stepwise refinement: break the problem into meaningful parts; then solve each part separately. And have fun!

  1. The problem is to write a Python 3 program that reads a text file or web page named by the user, and prints a neat table showing the counts of all of the different characters it reads. You must also meet the following explicit specifications:
    • As we insist that your output will exactly match the results of our solution, copy the following string and dictionary constants and paste them to the beginning of countchars.py (after your header comment of course):
      # strings that must be used for output:
      prompt = "Enter filename: "
      titles = "char       count\n----       -----"
      itemfmt = "{0:5s}{1:10d}"
      totalfmt = "total{0:10d}"
      whiteSpace = {' ':'space', '\t':'tab', '\n':'nline', '\r':'crtn'}
      See further instructions about these constants below.
    • Make it so that when you run the module in IDLE, it should immediately instruct the user to enter the filename, and then it should print the table of counts in the Python shell window. Alternatively, a user can run the program directly from the command prompt without ever starting Python or IDLE, by typing the following:
      -bash-4.2$ python3 countchars.py
    • Execution must proceed as follows:
      1. Use built-in function input to get the filename from the user. Pass the string named prompt to this function.
      2. If the filename does not begin with "http" then assume it is a local file, and open it for reading with the built-in open function. Otherwise import and use the urllib.request.urlopen function to open the web page for reading.
      3. Read all of the text in the file or web page, and count each different character in it. Ignore the case of characters (e.g., count both 'A' and 'a' as 'a').
      4. Print the string named titles. Then print the table of characters and their counts in ASCII order, and print the total character count at the bottom.
    • Use the string named itemfmt to print each interior row of the table in the proper format. For example, if c is a character, and ccount is its count, then print that row of the table as follows:
      print( itemfmt.format(c, ccount) )
    • If the character being printed is one of the white space characters in the dictionary named whitespace, then print its description instead of the character itself. For example:
      print( itemfmt.format(whiteSpace[c], ccount) )
    • Use the string named totalfmt to properly print the total character count. If this count is named total, for example:
      print( totalfmt.format(total) )
  2. Fully test your program. Here are sample input files and web pages, and the associated results from our solution. Make sure that your results exactly match these results. Also realize that the graders will probably test other inputs too.

    File/page Program run
    short.txt short.txt run
    longer.txt longer.txt run
    http://cs.ucsb.edu/~mikec/cs8/syllabus.html CS 8 syllabus page run

  3. Go to CSIL (in person unless you can manage this step remotely without any assistance from us). If you developed your program on a different computer system, be sure to test it at CSIL before turning it in. Then type the following while your current directory is the one that contains countchars.py:
    turnin P3@cs8c countchars.py
    If you are working with a partner, be sure that both partners names are in a comment at the top of the source code file.

Updated 5/19/2017, by C. Michael Costanzo