COMP1D04

From Scratch to Apps: Foundations of Computational Thinking and Literacy for Problem Solving

Grace Ngai

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*.

This homework is designed to give you practice in tracing through programming code.

Copy the stencil code for this assignment to your computer

Throughout the stencil code, there are several commented numbers (#1, #2, etc) scattered around. These are *break points*. At each break point, we want you to write a *comment* (the lines which begin with a `#`

) with the following:

- List
**every user-defined**variable that**currently**exists at that line. - Each variable's type (int, float, string, etc. For lists, specify the type of data in the list, i.e. list(string), list(int), list(string, int), etc.)
- Each variable's value (like 1, 3.14, ["a", "b", "c"], etc.)

If this is still a little confusing, read lines 14-29 in the stencil for a better explanation with examples. Once you've finished, double check your answers by adding print() statements at each break point to see what the variables actually are. If you got any answers wrong, **do not correct what you wrote**. Instead, write a `CORRECTION`

line after the mistake with the correct answer (See line 19 in the stencil.)

Rename your file `Name_StudentID_HW2-2_TraceCode.py`

. You will hand it in as part of your assignment.

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 answerBut 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.”

- 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 programming statements that will add up all the numbers from 1 to
`max_val`

, where`max_val`

is a variable with the number to add up to.**Hint**: Use iteration with a for loop! To use it, you want to create a list containing integers from 1 through`max_val`

.- Python has a built-in function
`range(x,y)`

that generates a list with a given beginning and end values. Try to call this function with various values of`x`

and`y`

in the IDLE interpreter 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?

`HW2-2.py`

. Rename it to`Name_StudentID_HW2-2.py`

before you hand this in. - (I noticed that there is a problem with this question and so I am deleting it. If you have already completed it, I'm sorry. But I assume that it probably didn't take you too long to complete.)
~~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.)

We will now have some fun with lists. In starter code, there are two variables: `word`

, which is a string, and `myList`

, which is a list.

- Write programming statements to check whether
`word`

is an element of`myList`

. To do this:- Use the variable
`found`

(it's already in the starter code.) - Iterate through all elements in
`myList`

, and check if it is the same as`word`

. - If the words match, set
`found`

to`True`

.

`word`

was found in`myList`

. We have provided an in-built Python function that does the same check for you, so you know whether your answer was right. - Use the variable
- We will do something a little more difficult now. Instead of just checking whether
`word`

is in`myList`

, we will return the`index`

of`word`

in`myList`

(and -1 if`word`

is not in`myList`

). Again, the comments in the starter code will guide you.

You need to hand in both `Name_StudentID_HW2-2_TraceCode.py`

and `Name_StudentID_HW2-2.py`

. Share the files with `PolyUCOMP1D04@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!**