Assignment 3

Instructions:

Assignment 3 will cover Control Flow and I/O. The overall goal of this assignment is to ensure that you are comfortable with Control Flow structures like if/else statements and for/while loops as well as how to interact with files in Python. Follow the steps below to ensure that you receive a passing grade:

- Complete the following tasks within this notebook
- When finished, convert this notebook to an HTML/PDF file
- Place the following items into a zip folder:
    - assignment.HTML or assignment.PDF
    - assignment.ipynb
- Name this folder using the naming convention: FIRSTNAME_LASTNAME_ASSIGNMENT3.zip
    - For example, If your name was Jane Doe, then the zipped folder should be called JANE_DOE_ASSIGNMENT3.zip

1. Compound-Interest Problem

Let’s say a person invests money in a savings account that yields interest. Assuming that the person leaves all interest on deposit in the account, your goal is to create a function that calculates and displays the amount of money in the account at the end of each year for \(10\) years. Use the following formula for determining these amounts:

\[ a = p(1 + r)^n \]

where

  • \(p\) is the original amount invested (i.e., the principal)

  • \(r\) is the annual interest rate

  • \(n\) is the number of years

  • \(a\) is the amount on deposit at the end of the \(n^{\ th}\) year.

In your function, let \(r\), the annual interest rate, be a default argument set to \(5\%\)

Use the round() function to round the compound interest to the nearest cent.

pi = 3.14159
pi = round(pi,2) # round pi to 2 decimal points, then reassign to pi
print(pi)
3.14

Hint: You will have to use a for loop and the range() function. Think about what the arguments for the range() function should be. Should the first value in the loop be \(0\) or \(1\)?

# your function delcaration goes here

Now call your function using a principal balance of \(\$1000\), and the default interest rate

# your function call goes here

Call your function using a principal balance of \(\$5000\), and the default interest rate

# your function call goes here

Now call your function using a principal balance of \(\$1000\), and an interest rate of \(2\%\)

# your function call goes here

Now call your function using a principal balance of \(\$5000\), and an interest rate of \(3\%\)

# your function call goes here

2. Declaration of Independence Text Scraping

The file DeclarationOfIndependence.txt contains a transcript of the entire United States Declaration of Independence from 1776. Your goal is to open the file then find and print the longest word that is contained within the document.

Hint: You’ll want to use the split() function from Week 2. Also be sure to replace all \n (newline) characters with the a space using the replace() function.

# your code goes here

3. Dracula: Text analysis

Without the use of any external python packages, do the following;

  • Open the file dracula.txt

  • Read the file line by line and break the novel text into its individual paragraphs.

    • Build a python list called “paragraphs” whose elements are strings for each individual paragraph.

    • Paragraph definition:

      • For the purposes of this exercise, consider a paragraph as any chunk of text with an empty line both above and below it.
      • The paragraph must also be NON-EMPTY (i.e. paragraph != ““).
    • Word definition

      • For the purposes of this exercise, consider a “word” to be any text with a space on the left and right

  • Only consider the text of the novel, i.e. remove the lines before and after the novel starts and finishes from your analysis.

    • To do this only analyze between the following lines
      • Start_line –> “CHAPTER I”
      • end_line –> “THE END”
      • Ideally, use a string comparison to find these lines, however, if you need to you can also use their line number (which is easier).

  • At the end print the following quantities

    • Novel starts at line =
    • Novel ends at line =
    • Number of paragraphs =
    • Average number of characters per paragraph (excluding spaces) =
    • Average number of words per paragraph =

NOTE: If you get stuck and cannot figure out how to proceed, then email your TA. They will provide you with a partial solution to help you move forward.

#USER PARAMETERS
line_num=0

inside_novel=False
beginning_of_paragraph=True

paragraphs=[]

#READ FILE 
with open('dracula.txt', 'r') as file:
    
    #LOOP OVER THE LINES
    for line in file:
        line_num+=1 

        #PUT YOUR SOLUTION HERE

OPTIONAL EXTRA CREDIT * Repeat the exercise, but for sentences instead of paragraphs

  • That is, build a python list called “sentences” which contain each sentence in the novel
  • Then repeat the analysis (output the numbers of sentence, and the average number of words and characters per sentence)
  • Sentence definition:
    • For the purposes of this exercise, consider a sentence to be any chunk of text that is longer than 50 characters AND starts with the following substring “. X”, where X is any upper-case letter.
    • OR any stand alone line or text, for example, “JONATHAN HARKER’S JOURNAL”
capitals="abcdefghijklmnopqrstuvwxyz".upper()
sentences=[]
# your optional code goes here