# Introduction to Computer Programming: Lab 1

## Fruitful Functions and For Loops

### Instructions

• Work on this lab with your partner (or on your own if you have not been assigned one).

• Start by working with the supplied starter file.

• Unless otherwise indicated, use only the Python operations and commands we have described thus far in class.

• Show your work for a given problem to the instructor or one of the lab assistants before moving on to the next problem, but do not get too bogged down on any one problem.

• In the common case, you will not complete the exercises during our scheduled lab. It is then up to you to complete the lab on your own time.

1. Complete the function `change(nickels, dimes, quarters)` so that it returns the total dollar (not cent) equivalent of specified number of nickels, dimes and quarters. Examples:

``````>>> change(0, 0, 0)
0.0
>>> change(1, 1, 1)
0.4
>>> change(20, 10, 4)
3.0``````
2. Complete `sum_odds(m)` so that it returns the sum of the first `m` odd numbers, i.e., 1 + 3 + ... + (2`m`-1). Use a repeating `for` loop. Examples:

``````>>> sum_odds(0)   # nothing to sum, so result is 0
0
>>> sum_odds(3)   # 1 + 3 + 5
9
>>> sum_odds(50)  # 1 + 3 + ... + 99
2500``````
3. Complete `emphasize(s)` so that it returns a new string that is `s` surrounded by `*` on each side. Examples:

``````>>> emphasize('')
'**'
>>> emphasize('kidding')
'*kidding*'``````
4. Complete `bold(s, n)` so that it returns a new string that is s surrounded by `n` ''s on each side. Use a repeating `for` loop and call* `emphasize`. Examples:

``````>>> bold('boring', 0)
'boring'
>>> bold('NOT', 5)
'*****NOT*****'``````

5. Function composition; built-in functions. In IDLE's interaction window, write one line expressions. You may use string concatenation, the standard arithmetic operations, and these standard Python functions:`int, len, chr, ord, abs, min, max`. Example: if the instruction said "Generate the concatenation of the strings `'hello'` and `'bye'`" you should type:

``>>> 'hello' + 'bye'``
1. Generate the ASCII character that corresponds to the number that is the length of the phrase "Machines take me by surprise with great frequency".

2. Generate the ASCII character that corresponds to 32 more than the ASCII value of the letter `Q`.

3. Compute the maximum of 59 and the absolute value of the minimum of 43 and -71.

4. Generate the ASCII character that corresponds to 5 times the value of the integer interpretation of the string formed by concatenating the ASCII character corresponding to the value of 7 squared with the ASCII character corresponding to three more than the ASCII value corresponding to the string `'0'`.

6. Complete `diff_squares(a, b)` so that given two numbers a and b it returns the difference of the square of the larger number minus the square of the smaller number. You may use `max`, `min`, `abs` as you see fit; you should also call `square`. Examples:

``````>>> diff_squares(5, 3)
16
>>> diff_squares(3, 4)
7
>>> diff_squares(5, 13)
144``````
7. Complete `az(n)` so that it returns a string consisting of 2`n` characters alternating by going up in the alphabet from `A` and down in the alphabet from `Z`. You may assumes `n` < 26. Examples:

``````>>> az(0)
''
>>> az(1)
'AZ'
>>> az(4)
'AZBYCXDW'``````
8. Complete `mid(a, b, c)` so that it returns the middle number (that is the number that is neither the maximum nor the minimum of the a, b, c). Hint: use `max` and `min`. Examples:

``````>>> mid(1, 2, 3)
2
>>> mid(9, -7, 5)
5
>>> mid(400, 401, 0)
400``````

9. Complete `sum_powers(n, x)` so that it returns the sum of the first `n` powers of `x` (counting from 0). That is it should compute: 1 + `x` + `x`^2 + `x`^(`n`-1). You are welcome to use the exponent (`**`) operator. Recall that `x`^0 is 1. You should use the more general `for`/`range` loop. Examples:

``````  >>> sum_powers(0, 99)  # there is nothing to add
0
>>> sum_powers(4, 2)   # 1 + 2 + 4 + 8
15
>>> sum_powers(3, 3)   # 1 + 3 + 9
13
>>> sum_powers(5, 7)
2801``````
10. Complete the nonfruitful function `looper` so that it asks the user for a number `n` and then for the first `n` numbers (starting at 1), prints that number and thrice that number plus one. Use the more general `for`/`range` loop. Example:

``````>>> looper()
Enter a number: 4
1 --> 4
2 --> 7
3 --> 10
4 --> 13``````

11. Use Python's turtles to write a function that draws a regular "n-gon" - a polygon with n sides of equal length and equal angles. It should take three parameters: a turtle, the number of sides, and the length of the sides. You can test it using the supplied `test_ngon` function. For example, `test_ngon(8, 20)` should generate an octagon like:

Some relevant geometry: a regular n-gon has n sides of equal length; its combined exterior angles add up to 360 degrees. Its interior angles are complements of their corresponding exterior angles (i.e., 180 - the exterior angle). However, when the turtle turns to visually form that angle, the angle actually specified is relative to the direction the turtle had been heading in, so, to draw the interior angle, the turtle has to turn at an angle of the exterior value.

12. Write a function that uses a general `for` loop and your `draw_regular_ngon` function to draw, from left to right a triangle, square, regular pentagon and regular hexagon, each of increasing size. Example output: