Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSE115 Introduction to Computer Science 1

Practice Questions (Week 10)

0 Overview

Due Date: Tuesday, Apr 9 @ 8:00PM

Instructions

For this homework assignment you will define several functions.  Each function accomplishes a specific task. The functions are described below.

Before defining each function: come up with at least three test cases (other than the one provided). Before submitting to AutoLab, run your functions and check the results against your test cases.

All functions for this homework must be defined in a file named main. py.  To submit and get feedback, submit this file to AutoLab under the Practice Problems – Week 10 submission. You may submit as many times as you like, but remember that AutoLab uses your LAST submission to determine your grade (not your highest).

You may find functions in this homework a bit more complex than in previous homeworks. Try to break down problems into smaller pieces and focus on solving the smaller pieces first, it will make coming up with and testing your solutions more managable.

These exercises are graded. You will benefit most from doing them entirely on your own, but of course the course staff is available to assist on Piazza and during office hours.

Learning Outcomes

You will demonstrate an ability to read and write text files, and use dictionaries in Python.

Supporting lecture topics

expressions dictionaries

file i/o text files

1 Problems

1.  Define afunction named addLineNumbers that takes 1 parameter, a string representing a file name.

It should modify the file so that every line is preceded by "X:  ", where X is the line number. The first line in a file is line number 1.  The function should return the number of lines in the file.

Test Case: If the file named fma. txt has contents:

Stand up and walk .

Keep moving forward .

You ’ve got two good legs .

So get up and use them .

Then the call addLineNumbers("fma. txt") should return 4, and after the call the contents of fma. txt should be:

1: Stand up and walk .

2:   Keep moving forward .

3:   You ’ve got two good legs .

4: So get up and use them .

2.  Define a function named explodeLines that takes 1 parameter, a string representing a file name.

It should modify the file to contain all of it’s original contents, but with only one word per line. The words should appear in the same exact order that they appeared in the original file. Any blank lines should also be preserved.

The function should return the number of lines in the file after it gets modified. Note: You may assume that words are separated by a single space.

Test Case: If the file named bmw. txt has contents:

Life ’s tough

Get a helmet

Then the call explodeLines("bmw. txt") should return 6, and after the call the con- tents of bmw. txt should be:

Life ’s

tough

Get

a

helmet

3. Define afunction named filterByLetter that takes 2 parameters. The first is a string representing a file name, and the second is a single character of the alphabet.

It should create a new file that only contain words that start with that letter of the alphabet, ignoring capitalization. For example, if the letter is "C", the new file should contain both  "Cookies"  and  "cake"  if those were words  in the original file.   The function should preserve the number of lines and the order of words in the original file.

The new file should have the same name as the original file, but with X appended to the filename (but before the  . txt extension) where X is the uppercase letter of the alphabet that was passed in.

The function should return the number of words in the new version of the file.

Note: You may assume that words are separated by a single space.

Hint: In order to ignore capitalization you can use the string methods upper or lower when making comparisons.

Hint: To create the new file name, remember that you can use the  split string

method to break up a string around a given character.

Test Case: If the file named shel. txt has contents:

One sister for sale!

One sister for sale!

One crying and spying  young   sister for sale!

I’m  really not kidding ,

So   who ’ll  start the bidding?

Shel Silverstein

Then the call filterByLetter("shel. txt",  "s") should return 11, and create a new file named shel S. txt with contents:

sister sale!

sister sale!

spying sister sale!

So start

Shel Silverstein

4. Define a function named categorize that takes 1 parameter, a string representing a filename.

It should return a dictionary containing lists of the words in the file, organized by their first letter ignoring capitalization. The keys in the dictionary should all be uppercase. The words in each list should appear in the same order they appear in the text file.

Note: You may assume that words are separated by a single space and start with a letter of the alphabet.

Hint: In order to ignore capitalization you can use the string methods upper or lower when making comparisons.

Test Case: If the file named "proverbs. txt" has contents:

Better late than never!

Better to  be safe than sorry!

Then the call categorize("proverbs. txt") should return a dictionary with the fol- lowing contents:

{

"B": ["Better",  "Better",  "be"],

"L": ["late"],

"T": ["than",  "to", "than"],

"N":   ["never!"],

"S": ["safe",  "sorry!"]

}

5. Define a function named alphabetize that takes 1 parameter, a string representing a filename.

It should create a new file, where each line of the file starts with a single letter of the alphabet, a colon, and a space, followed by a comma separated list of all of the words in the original file that started with that letter, ignoring capitalization.  In order to do this, your function should call the categorize function you wrote in the previous problem.

The words in each line should all be in the same order they appeared in the original file. The lines should appear in alphabetical order, and lines that would have no words should be ignored.

The name of the new file should be the name of the old file with  " alphabatized" appended to it (before the  . txt extension).

The function should return the number of lines written into the new file.

Note: You may assume that words are separated by a single space and start with a letter of the alphabet.

Note: To get full credit, you must call the  categorize function from the previous problem.

Hint: In order to ignore capitalization you can use the string methods upper or lower when making comparisons.

Hint: You can use the fact that the keys of the dictionary returned by categorize will all be characters in the string "ABCDEFGHIJKLMNOPQRSTUVWXYZ" to help you write the lines in alphabetical order.

Test Case: If the file named proverbs. txt has contents:

Better late than never!

Better to  be safe than sorry!

Then the call alphabetize("proverbs. txt") should return 5 and create a file named proverbs alphabetized. txt with contents:

B: Better , Better , be

L: late

N:   never!

S:   safe , sorry!

T: than , to, than

2 Submission

Save your main. py file and submit it to Week 10 in AutoLab.

Your final submission is due Tuesday, Apr 9 @ 8:00PM.