12.17 Exercises
From Foundations of Python ProgrammingGNU FDL 1.3Original pagePython3 class periods16 exercises15 graded tests
12.17 Exercises
Read first in the book: 12.12 Flow of Execution Summary, 12.13 Print vs. return, 12.14 Passing Mutable Objects, 12.15 Side Effects, 12.16 Glossary.
Exercise 1
Write a function named num_test that takes a number as input. If the number is greater than 10, the function should return "Greater than 10." If the number is less than 10, the function should return "Less than 10." If the number is equal to 10, the function should return "Equal to 10."
Open the
01folder and editmain.py. Run Tests checks it.
Exercise 2
Write a function that will return the number of digits in an integer.
Open the
02folder and editmain.py. Run Tests checks it.
Exercise 3
Write a function that reverses its string argument.
Open the
03folder and editmain.py. Run Tests checks it.
Exercise 4
Write a function that mirrors its string argument, generating a string containing the original string and the string backwards.
Open the
04folder and editmain.py. Run Tests checks it.
Exercise 5
Write a function that removes all occurrences of a given letter from a string.
Open the
05folder and editmain.py. Run Tests checks it.
Exercise 6
Although Python provides us with many list methods, it is good practice and very instructive to think about how they are implemented. Implement a Python function that works like the following:
a. count
- in
- reverse
- index
- insert
Open the
06folder and editmain.py. This one is not graded, so check it yourself.
Exercise 7
Write a function replace(s, old, new) that replaces all occurences of
old with new in a string s:
test(replace('Mississippi', 'i', 'I'), 'MIssIssIppI')
s = 'I love spom! Spom is my favorite food. Spom, spom, spom, yum!'
test(replace(s, 'om', 'am'),
'I love spam! Spam is my favorite food. Spam, spam, spam, yum!')
test(replace(s, 'o', 'a'),
'I lave spam! Spam is my favarite faad. Spam, spam, spam, yum!')
Hint: use the split and join methods.
Open the
07folder and editmain.py. Run Tests checks it.
Exercise 8
Write a Python function that will take a the list of 100 random integers between 0 and 1000 and return the maximum value. (Note: there is a builtin function named max but pretend you cannot use it.)
Open the
08folder and editmain.py. Run Tests checks it.
Exercise 9
Write a function sum_of_squares(xs) that computes the sum
of the squares of the numbers in the list xs. For example,
sum_of_squares([2, 3, 4]) should return 4+9+16 which is 29:
Open the
09folder and editmain.py. Run Tests checks it.
Exercise 10
Write a function to count how many odd numbers are in a list.
Open the
10folder and editmain.py. Run Tests checks it.
Exercise 11
Sum up all the even numbers in a list.
Open the
11folder and editmain.py. Run Tests checks it.
Exercise 12
Sum up all the negative numbers in a list.
Open the
12folder and editmain.py. Run Tests checks it.
Exercise 13
Write a function findHypot. The function will be given the length of two sides of a right-angled triangle and it should return the length of the hypotenuse. (Hint: x ** 0.5 will return the square root, or use sqrt from the math module)
Open the
13folder and editmain.py. Run Tests checks it.
Exercise 14
Write a function called is_even(n) that takes an integer as an argument and returns True if the argument is an even number and False if it is odd.
Open the
14folder and editmain.py. Run Tests checks it.
Exercise 15
Now write the function is_odd(n) that returns True when n is odd and False otherwise.
Open the
15folder and editmain.py. Run Tests checks it.
Exercise 16
Write a function is_rightangled which, given the length of three sides of a triangle, will determine whether the triangle is right-angled. Assume that the third argument to the function is always the longest side. It will return True if the triangle is right-angled, or False otherwise.
Hint: floating point arithmetic is not always exactly accurate,
so it is not safe to test floating point numbers for equality.
If a good programmer wants to know whether
x is equal or close enough to y, they would probably code it up as
if abs(x - y) < 0.001: # if x is approximately equal to y
...
Open the
16folder and editmain.py. Run Tests checks it.
For teachers
12.17 Exercises
Students read Foundations of Python Programming's “Exercises” in CS Room and work through its 16 Python coding exercises (15 graded).
- Time: 3 class periods
- Tests: 15 in 15 graded exercises, set up for you when you import the lesson
Reading first
- 12.12 Flow of Execution Summary
- 12.13 Print vs. return
- 12.14 Passing Mutable Objects
- 12.15 Side Effects
- 12.16 Glossary
Exercises
| Folder | Grading | Tests |
|---|---|---|
01 | Autograded | 1 |
02 | Autograded | 1 |
03 | Autograded | 1 |
04 | Autograded | 1 |
05 | Autograded | 1 |
06 | Not graded; students check it themselves | 0 |
07 | Autograded | 1 |
08 | Autograded | 1 |
09 | Autograded | 1 |
10 | Autograded | 1 |
11 | Autograded | 1 |
12 | Autograded | 1 |
13 | Autograded | 1 |
14 | Autograded | 1 |
15 | Autograded | 1 |
16 | Autograded | 1 |