CSC 131 - Introduction to Computer Science

### Program 1: Determining some file statistics

Have the user enter a file name.  Open that file for reading.   Assume the file contains text.  Calculate the following:

·         The number of words (without changing to lower-case or upper-case).   Make sure you remove any punctuation marks from the end of the words.

·         The number of unique words.  Do change all the words to lower-case and remove any end punctuation.  In other words, “The” and “the” are the same word.

·         For each unique lower-case word, determine how many times it occurs in the text.  Output the 20 most frequently occurring words.   Here’s one way to do this:

o   For each unique word, w, determine how many times it occurs in the original text (after converting to lower-case and removing end punctuation).  Call that number, count.

o   Add the tuple (w, count) to a list.

o   When all of the unique words and their counts have been added to the list, sort it as follows:

§  sortedList = sorted(listOfWordCounts, key=lambda x: x[1])

§  By default, sorted will sort in increasing order so if you want the top 20, they are at the end.

§  You could reverse the list if you want them at the front.

Here is my output for the file, mobydick.txt.  The inefficient code that I wrote to do the counts took 96 seconds on a very fast computer.   When I wrote it efficiently, it took 0.02 seconds.   For this homework don’t worry about efficiency, just get it to work – I’ll show the “good” and “bad” solutions in class.

Which file would you like to analyze? mobydick.txt

Number of words: 215,120

Unique words:  21,181

the        14413

of          6700

and       6391

a           4661

to          4656

in          4177

that       2931

his        2519

it           2356

i            1842

with      1762

but        1742

is          1723

he         1721

as          1719

was       1637

for        1614

all         1478

this       1403

at          1320

### Program 2: Recursively reverse a list

o    Write a recursive method to reverse a list.

o    Specifically, your method should be called reverseList.

o    If alist = [1,2,3,4], then reverseList(alist) should return [4,3,2,1].

o    If there are any loops in your program (while, for), then this problem will receive a zero.

o    Do not use any of the built-in functions like reverse, reversed, or the :: operator.   Just write the method recursively.

### Program 3: Recursively count vowels in string

o    Write a recursive method that counts the numbers of vowels (upper or lower case) in a string.

o    Specifically, your method should be called countVowels.

o    If s = “Able was I ere I saw Elba”, then countVowels(s) should return 10.

o    If there are any loops in your program (while, for), then this problem will receive a zero.