CSC 131 - Introduction to Computer Science

### Program 1: Word Statistics

Write a Python program that does the following:

1.      It asks the user for the name of a file to read from.

2.      It reads all the words in the file and stores them (probably in a list, though you could use a dictionary too.)

3.      It prints out the total number of words in the document.

4.      It prints out the total number of unique words in the document.

5.      It computes and prints the average length of each word.

6.      If we assume a period marks the end of a sentence, compute and print the number of sentences in the document.  Remember that a period will be “attached” to the end of a word – it will not be a separate word.

7.      If you want a sample file, here is mobydick.txt.

Here’s what I found for Moby Dick.

Enter the file: mobydick.txt

Total words 215139

Unique words: 33781

Average word length: 4.721277871515625

Number of sentences: 7119

### Program 2: Write a class Student

Given the following UML diagram, generate a corresponding Python class.

If your class is correct, the following code (TestStudent.py) should have the output given below.  Here are some things to note:

·         Your constructor, the __init__ method, is expecting to be passed two arguments: the first is the student’s name; the second is the student’s grade point average.

·         The method that converts a Student object to a string should return a string which consists of the name, a tab (‘\t’), the label “GPA:”, and then the grade point average.

·         The method setGPA is expecting the programmer to pass one argument, the new grade point average.

·         The method getGPA returns the student’s grade point average.

from Student import *

student1 = Student("Mary Jones", 3.15)

student2 = Student("Bill Smith", 3.01)

student3 = Student("Mars Landon", 2.85)

print(student1)

print(student2)

print(student3)

print(student1.name, "has a GPA of", student1.getGPA())

student2.setGPA(3.9)

print(student2)

love

### Program 3: Converting a binary string to a decimal number

Write a Python program that asks the user for a binary number.  The user will enter that number.  You will end up with a string like “1010101011”.    Convert that string to a decimal number.   Remember the rightmost digit is worth 1, the 2nd rightmost is worth 2, the 3rd rightmost is worth 4, the 4th rightmost is worth 8, and so on.    (For the string above, the answer is 683).

This does not have to be done recursively.

### Program 4: Recursive palindrome

Write a recursive method, palindrome, that takes one parameter, a string.  The method should return True if the parameter is a palindrome (same forward as backward); False, otherwise.   This must be done recursively.  No loops!!   And, no calls to “reverse”, “reversed” or the :: operator

To receive full credit, this method must ignore lower/upper case, white space and punctuation marks in determining whether a string is a palindrome.   So, not only are ‘racecar’, ‘bob’, ‘anna’ and ‘a’palindromes, but so is ‘RaceCar’, ‘A man, a plan, a canal, Panama!’, and ‘No pinot noir on Orion to nip on.’

Hint: Get this working without worrying about lower/upper case, white space and punctuation marks.   Then, after you have that working, then try to figure out how to handle those cases.