Homework 2-4

Due October 15, 2015, 9:00 am


If a problem is marked as “(Independent)”, you may only discuss the problem with course staff. Otherwise, you are free to discuss the concepts that will help you solve the problems with classmates as well as course staff. However, you are never allowed to simply copy answers.

Task 1

In class, we talked about 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. It might help to refer back to the slides that introduced how input variables and Python's "tables" of variable names and values work when defining new functions.

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
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 a Google doc called FirstLast_HW2-4_Task1.

Task 2

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 with a for loop! 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. Notice how the first and last values of the list it returns are related to the input arguments you give to range.) 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-4.py. Rename it to FirstLast_HW2-4.py before you hand this in.
  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 3

  1. In FirstLast_HW2-4.py, finish writing the function called isAnElementOf 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-4.py.


Make a Google folder called FirstLast_HW2-4. It should contain the following files:

  1. FirstLast_HW2-4.py
  2. FirstLast_HW2-4_Task1 (this is a google doc)

Share the Google folder with cs0931handinfall2015@gmail.com .

Note: Before you turn in your Python files, make sure they run without any errors (save your Python file, then select Run > Run Module or hit F5 on your keyboard)! If nothing appears in the Shell, don't worry, as long as no red error messages appear. If they don't run, i.e. if red stuff starts appearing in the shell, points will be taken off!