CSc 250: Assignment 8

This assignment is all python. You should only use python features that we have discussed up to this point. If you have questions about whether or not a particular feature can/should be used, ask the instructors!

When grading your assignments, each problem/script/function will be thoroughly tested with many types of input, so make sure you test your scripts/functions well!

The python in this assignment will be graded using python 3.6, and tests will be run on a Mac machine. In general python works well cross-platform, but if you want to be absolutely sure your program works as expected, test on a mac! Any python version 3.4 or newer should suffice though.

All of your scripts should be well-formatted and easy for the graders to read. Each script should have a header comment at the top that has the following format:

# Author: Student Name
# Description:
#    A short description of what this program / script / set of functions does!

Each function in your script(s) should have a descriptive comment above it. If any part of your scripts are particularly complex, you should put documentation comments above those lines of code.

For all problems

Put all of the functions below in a file named You can test these functions using the techniques described in lab 4.

Problem 1 (25 points)

Write a function named convert_keys. This function takes one argument named numbers. numbers is a dictionary in which all of the keys are integer numbers. This function will return a dictionary that has the same key/value pairs, but the keys will be converted to text given the process described below. Each digit of the key will be converted to it’s corresponding word. For example:

… and so on.

Below are a few examples of calling this function in an interactive python shell:

>>> nums = { 23:'Samuel', 18:'Todd', 34:'Katy' }
>>> asg8.convert_keys(nums)
{'TwoThree': 'Samuel', 'OneEight': 'Todd', 'ThreeFour': 'Katy'}
>>> employee_ids = { 34753:'John', 99311:'Jan', 11331:'Kathy' }
>>> asg8.convert_keys(employee_ids)
{'ThreeFourSevenFiveThree': 'John', 'NineNineThreeOneOne': 'Jan', 'OneOneThreeThreeOne': 'Kathy'}
>>> employee_ids = { 12345:'James', 67890:'Alex', 13579:'Andy' }
>>> asg8.convert_keys(employee_ids)
{'OneTwoThreeFourFive': 'James', 'SixSevenEightNineZero': 'Alex', 'OneThreeFiveSevenNine': 'Andy'}

Problem 2 (25 points)

Write a function named search_file. This function will take two arguments. The first will be file_name and will specify the name of the file. The second will be called search_term and will be a string. This function will return a number, indicating the number of lines in the file that search_term appears at least one time in.

For example, say there is a file named story.txt in the current working directory with the following contents:

My heart leaps up when I behold
A rainbow in the sky:
So was it when my life began,
So is it now I am a man,
So be it when I shall grow old
Or let me die!
The Child is father of the Man:
And I could wish my days to be
Bound each to each by natural piety.

Below are a few example calls to search_file:

>>> asg8.search_file('story.txt', 'rainbow')

Clearly, “rainbow” only appears on 1 line in this file, so 1 is returned.

>>> asg8.search_file('story.txt', 'each')

Notice that the word “each” appears in the file twice, but both appearances are on the same line (line 9), so 1 is still returned.

>>> asg8.search_file('story.txt', 'the')

the actually appears three times in this file (once on line 2, and twice on line 7), but 2 is returned because it appears in 2 lines total.

>>> asg8.search_file('story.txt', 'it')

Clearly, it appears on 3 unique lines so 3 is returned.

>>> asg8.search_file('story.txt', 'o')

The letter “o” appears on 8 lines (all but line 6). As this example suggests, this search should be case-sensitive, because it ignores the upper-case “O” at the beginning of line 6.

>>> asg8.search_file('story.txt', 'fellowship')

The word “fellowship” never appears, so 0 is returned.

Problem 3 (25 points)

Write a function named count_lines. This function takes one argument named file_name which is the name of the file to read. This function will count and report the number of times each line of the file appears in the file. Note that you should strip() each line of the file after reading it in to get rid of extra whitespace at the beginning and end of each line.

Below are a few examples:

If the file lines.txt looks like:

$ cat lines.txt


>>> asg8.count_lines('lines.txt')
"orange" appeared 3 time(s).
"apple" appeared 3 time(s).
"grape" appeared 1 time(s).

If the file lines.txt looks like:

$ cat lines.txt
Star Wars
Indiana Jones
Star Wars
 Star Wars
   Star Wars
A Tale of Two Cities


>>> asg8.count_lines('lines.txt')
"Star Wars" appeared 4 time(s).
"Indiana Jones" appeared 1 time(s).
"LOTR" appeared 1 time(s).
"A Tale of Two Cities" appeared 1 time(s).

If the file lines.txt looks like:

$ cat lines.txt
 A B C D
 A B C D


>>> asg8.count_lines('lines.txt')
"A B C D" appeared 5 time(s).
"X Y Z" appeared 1 time(s).

Problem 4 (25 points)

Write a function named reverse_file which will take a single argument named file_name. This function will open the file with the name stored in file_name, reverse the order of the lines, and then write the file to disk with the same name.

Below are a few example runs:

In both cases, after calling reverse_file on a file, the ordering of the lines is reversed. Remember, the list data structure has a reverse() function!

Testing for correctness

This assignment requires printing out precise text messages and notifications. As already mentioned, it is important to be precise when doing these printouts.

We will only take minimal points off for very minor differences (for example, minor character spacing issues). However, we will take points off for any non-trivial differences, so try to be as precise as you can!

Submission and grading

This problem will be graded out of 100 points.

This was assigned on Sunday, March 19, 2017. It is due Friday, March 24, 2017, at 6:00pm (right before spring break).

Turn-in instructions:

Following these turn-in instructions closely is very important, because our grading scripts will depend on some of the details. You may lose points if these instructions are not followed precisely!