# Activity 2-3

March 3, 2015

## Task 1: More Python Practice

Do this activity with a partner. You don't have to turn anything in.

1. With a pen or pencil, For each line of code below, write what you expect to see output if you were to enter the line in a Python shell. Some lines wouldn't output anything (like assignments), so don't worry about those.
```myString = 'hi there!'
len(myString)
myList = myString.split()
len(myList[1])

x = 10
y = 15
str(x) + str(y)
[x]+[y]

range(1,11)
range(3,10)[0]

negNums = range(-7,1)
negNums[3:5]

(x == 10) and (str(y) == 'fifteen')
(x < 4) or (y*0.1 < 4)

if len(myList[1]) > 10:
print(myList[1],'has length > 10')
else:
print(myList[1],'has length <= 10')

def tip(bill):
return bill * 0.15

tip(100.00)
```
2. Enter the statements above in a Python shell and verify your answers. The line numbers on the left-hand side are for reference only.

## Task 2: Understand and Run Last Class's Programs

Download and save `ACT2-3.py`. Open it in `IDLE`.

1. Look at `readMobyDick()`. Make sure you understand the following line:
`fileString = fileString[0:1000]`
2. Look at `CountWordsInMobyDick()`. Make sure you understand the following lines:
```for word in myList:
count = count + 1
```
3. Press F5 and run `countWordsInMobyDick()`. What's your answer?
4. Comment out two lines in `readMobyDick()` to count all the words in MobyDick (hint: one of the lines is a print statement, which technically has no bearing on how the rest of the function executes). Press F5 again and run `countWordsInMobyDick()` again. What's your answer?

## Task 3: Compute the Average Word Length and Longest Word in "Moby Dick"

1. Fill in the `avgWordLengthInMobyDick()` function.
1. Grab the list of words in the book using the `readMobyDick` function you wrote.
2. Create a variable that will keep track of the sum of the lengths of all words. What should its initial value be?
3. Use a for-loop to iterate over all words, summing up the length of all words using the `len` function.
4. With the total sum of all word-lengths (i.e., the total number of non-whitespace characters), get the average by dividing this sum by the number of words and return the average.
2. Fill in the `getLongestWordInMobyDick()` function.
1. Grab the list of words in the book using the `readMobyDick` function you wrote.
2. Create a variable that will hold the a string equal to the longest word found so far. What should its initial value be?
3. Use a for-loop to iterate over all words, checking whether each word is longer than what we have seen so far.
4. If a given string is longer than what we have seen so far, assign it to the variable that is tracking the longest word.
5. What should this function return?