Homework 2-2

Due March 5, 2013, 2:25 pm

Reminders

For the following problems you may discuss the concepts that will help solve these problems with classmates and course staff. You may not simply copy down the answers of your classmates as that is a violation of the collaboration policy. The one exception to this rule are those problems marked as “(Independent)”. You may discuss independent problems with course staff only.

Task 1

Create a folder on your desktop called “HW2-2”. Download the starter program moby-starter.py and the text of Moby-Dick from the course website and save them in this folder. Right click on the Python program and select Edit with IDLE. When you write your functions, remember to write down what your functions does and what the arguments mean, by commenting your code. Remember that comments start with a #.

  1. The starter program contains a function called printMD1000 that takes no arguments, and is supposed to print out the first one thousand characters in Moby-Dick. It needs to know where to find the MobyDick.txt on your computer, however. Modify the string 'C:\\WinData\\Desktop...' so that its value is the correct path to MobyDick.txt on your computer.
  2. Look at the rest of the code for printMD1000 and make sure you understand why it should work. Then press F5 to run it. You should see another IDLE window come up. Then after the prompt type printMD1000() to call your function. Make sure it produces the correct answer. (You do not have to hand in anything for this part.)
  3. Now, write another function called print1000 that takes one argument. Suppose in the folder “HW2-2”, I have bunch of text files: MobyDick.txt, Hamlet.txt, Bible.txt, etc. You want the argument to this function to tell the function which text file to print. For example, calling print1000('MobyDick.txt') should print the first 1000 characters of the file Z:\WinData\Desktop\HW2-2\MobyDick.txt, and calling print1000('Hamlet.txt') should print the first 1000 characters of the file Z:\WinData\Desktop\HW2-2\Hamlet.txt, assuming the file does exist. On your computer, the path to the HW2-2 directory will probably be different than Z:\WinData\Desktop\HW2-2; use the path that works on your computer instead. Hint: remember you can use the + operator to “glue” strings together.
  4. Now you're going to test the function you just wrote. Provide one example of a call to print1000 (different from the ones we've provided) that works as expected.
  5. Write a third function, printN, that takes two arguments. The first one indicates which file to print (same as the last function) and the second is a number indicating how many characters from the beginning to print. For example, calling printN('MobyDick.txt', 500) should print out the first five hundred characters in the file located at Z:\WinData\Desktop\HW2-2\MobyDick.txt.
  6. Provide two examples of calls to printN that show that it works as expected. What are you looking for when you check that printN is correct?

When you're done, name your program YourName_MobyDick.py — for example, JadrianMiles_MobyDick.py.

Task 2

Look at the following Python program (the numbers to the left of the line are line references and not part of the program):

numOfPC = 10
numOfMac = 4
inventory = [numOfPC, numOfMac]

def addOne(num):
  result = num + 1
  return result

addOne(numOfMac)
numOfPC = addOne(numOfPC)
inventory[1] = addOne(inventory[1])
    
  1. Without actually running the program in Python, write down your prediction of the values of the variables numOfPC, numOfMac, and inventory after the program has executed lines 3, 9, 10, and 11. (Hint: when in doubt, review the way we reasoned about programs in class and follow it religiously). Write down these variables in a text file called YourName_HW2-2.txt — for example, JadrianMiles_HW2-2.txt.
  2. Now open IDLE. Under the File menu in the top left corner, click on New Window (or simply press Ctrl+N on your keyboard). This should open up a new file as we saw in class. Type in the program.
  3. By inserting some print statements, modify the program so that when you execute it, you can see what are the values of the variables numOfPC, numOfMac, and inventory after lines 3, 9, 10, and 11.
  4. Save your program and name it YourName_HW2-2_Task2.py. (You need to hand in this program.) Then press F5 to run the program. Do the outputs match your prediction from the first question? If there is a difference, try to explain it. Write this solution in your text file.

Task 3

In class, we gave a list of things that you should not do when writing a Python program. Remember the reason that we have these “taboos” is that we can reason about programs much more easily if we restrict our way of writing them. This task is meant to give you a taste about how whimsical programs can get if you defy these rules.

Danny, a naive Python programmer, wants to write a function that, given two variables, swaps their values. Here is what he wrote:

def swapBAD(a, b):
  '''Swap the values of two variables.'''
  temp = a
  a = b
  b = temp
  return
  
x = 10
y = 42
swapBAD(x, y)
print x, y
    

What do you think are the values of x and y at the end of the program? If you are not sure about your answer, test it by running the program. Explain in a few words why the swap function succeeds or fails. You should be able to do it by just following our in-class reasoning.

Write your solutions in the same text file as Task 1.

Task 4

Carl Friedrich Gauss (1777–1855) was one of the greatest mathematicians of all time, who contributed significantly to many fields in mathematics, statistics, physics, and astronomy. There is a famous anecdote about him (whether true or not). When he was a little boy in elementary school, his math teacher one day asked the class to add up all the integers from 1 through 100. The teacher thought it would take the kids a long while and that he had just gotten himself some time for a cigarette break. However, to his annoyance, little Carl raised his hand right after he pulled out a match.

“The answer is 5050,” said Gauss. The teacher frowned suspicously and sneered, “You are wrong. Do it again.” Honestly he didn't know the answer himself, but he believed there was no way to come up with the right answer that quickly.

But Gauss looked down at his sketch paper and lifted up his head in about five seconds. “I just checked my calculation. I'm sure 5050 is the right answer.”

“Now you're just messing with me,” said the teacher, as he strode through many amazed eyes towards Gauss' desk. “Show me your work.”

“Well, if you add 1 and 100, you get 101. Adding 2 and 99 also gives 101. The same goes for 3 and 98, 4 and 97, etc. There are 50 such pairs from 1 to 100. So the answer is 101 times 50 which is 5050.”

  1. Gauss's method is pretty clever and has deep mathematical consequences. For now, let's add 1 through 100 in the conventional way. Luckily we have computers and Python on our side. Write a function that adds up all the numbers from 1 to max_val, where max_val is the argument to the function. (Hint: Use iteration! To use it, you want to create a list containing integers from 1 through max_val. There is a built-in function range(x,y) that does this. Try to call this function in the interactive environment to see what it does.) Run it with 100 and see if it produces Gauss' answer. What about 1,000,000? We've provided some starter code that you can build on in HW2-2.py.
  2. Next, write a function that adds up all the odd numbers from 1 to arg. (Hint: Iterate as you did in part 1, but add the number to your total only if it is an odd one. Use the function we've provided called isOdd to help.)

Task 5

  1. Write a function that determines if an integer a is in a list of integers myList. To do this, iterate through all elements in the list, and check if it is the same as the integer a. Once you see a match, return True. After the iteration, return False (why?). Be careful with the indentations!
  2. (Extra Credit) Now write a function that determines if all of the integers in a list myList1 are in another list of integers myList2. To do this, iterate through all elements in myList1, and check if each element is in myList2 (hmm... sounds like a familiar task — maybe you can reuse the function you wrote for part 1). Again, just fill in the corresponding function skeletons in HW2-2.py.

Handin

Email your typed up homework and programs to cs0931handin@cs.brown.edu, with the subject line YourName_HW2-2 (remember to change “YourName” to your actual name).

You should have 3 files:

  1. YourName_MobyDick.py — for example, JadrianMiles_MobyDick.py, for Task 1.
  2. YourName_HW2-2_Task2.py — for example, JadrianMiles_HW2-2_Task2.py, for Task 2.
  3. YourName_HW2-2.txt — for example, JadrianMiles_HW2-2.txt, for Task 2 and Task 3.
  4. YourName_HW2-2.py — for example, JadrianMiles_HW2-2.py, for Task 4 and Task 5.