4th Homework

This homework is to be prepared in teams of two students. Find your partner on http://moodle.uni-graz.at/. Download hw_4.tar.gz and extract it. Then add your homework solutions to the files contained in the directory. Rename the directory according to the rules in the syllabus before submitting it as compressed archive. Note that the grader requires the package python3-numpy to be installed. Don't forget to add the correct subject to the email when submitting.

  1. Telephone numbers with letters
    In some countries it is a common practice to encode selected telephone numbers as text because they are easier to memorize that way. The letters of the text signal which button to press on a telephone keypad. See the image below as a reference:
    Source: Marnanel (via Wikimedia Commons)
    Implement a function as_numeric(text) that returns a string containing only the numbers that correspond to the input text. Using the function in a python3 shell should look like this:
    >>> as_numeric('0800 reimann')
    '0800 7346266'
    
    Hint: using a Python dictionary to store the translation table facilitates this task.
    Name the program file: telephone_numbers.py
  2. Working on existing programs (2 points)
    In his book "Programming in Python 3", Mark Summerfield introduces a small Python program that creates HTML files with some user defined input. Have a look at the file make_html_skeleton.py and run it a few times. Try to understand roughly how it does its job. Then change it such that there are no description or keyword tags in the output. To do this, remove the corresponding text from the HTML_TEMPLATE's head tag. Also remove the parts responsible for collecting and handling the keywords and the description.
    As websites use more and more JavaScript, add the following line to the output (after the last <meta> tag):
    <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    In addition to the above, ask if the user wants to add additional Links to JavaScript files. For each of the given files, add
    <script src="FILENAME" type="text/javascript"></script>
    to the output, where FILENAME is the name of the file the user provided. In order to do this, change the function signature of make_html_skeleton(.) to contain a new optional parameter named scripts (list of strings). Each of the strings then represents a javascript file path (eg. scripts=['js/test.js', 'js/other.js']).
    Name the program file: make_html_skeleton.py
  3. Basic statistics (2 points)
    Write a few functions that compute basic statistics from given financial data stored in CSV files. The input files have to have column headers in their first row. As you'll have to be able to deal with bigger amounts of data, it cannot be guaranteed that all of the data can fit your computer's memory. To help you out with this situation, you can use the provided function items(.) yielding one row of the data after the other when being iterated over. The rows are yielded as dictionaries using the first row as keys. The provided count(.) function gives an idea on how to use the items(.) generator function. You need to create a series of functions that compute the required values:
    calc_mean(.)
    calc_stddev(.)
    calc_sum(.)
    calc_variance(.)
    find_max(.)
    find_min(.)
    Name the program file: statistics.py
  4. Counting unique words in a file (4 points)
    In Prof. Rauch's 'Information Science' course, one of the task is to count how many times each word in an article occurs. To alleviate checking if you performed such a task correctly, write a Python program that does the work for you. You don't have to write the entire program from scratch. Instead, use the provided count_unique.py file and implement count_unique(words).
    Also implement count_unique_sorted(words) that returns a list of named tuples. The first element of each named tuple must be 'word' and the second 'count'. The list has to contain the tuples in the same order as the words occur in the input file.
    Name the program file: count_unique.py