## Computer Science Exercises

Here are some exercises for you to try on your own:

1. Import module math, and use its functions to complete the following exercises:

a) Write a single expression that rounds the value of -4.3 and then takes the absolute value of that result.

b) Write an expression that takes the ceiling of sine of 34.5.

2. In the following exercises, you will work with Python's calendar module:

a) Visit the Python documentation website at http://docs.python. org/modindex.html, and look at the documentation on the calendar module.

b) Import the calendar module.

c) Read the description of the function isLeap. Use isLeap to determine the next leap year.

d) Find and use a function in module calendar to determine how many leap years there will be between the years 2000 and 2050, inclusive.

e) Find and use a function in module calendar to determine which day of the week July 29, 2016 will be.

3. Using string methods, write expressions that do the following:

a) Capitalize 'boolean'.

c) Find the second occurrence of "2" in 'C02 H20'.

d) Determine whether 'Boolean' begins with a lowercase.

e) Convert "MoNDaY" to lowercase letters and then capitalize the result.

f) Remove the leading whitespace from " Monday".

4. The example used to explain import * was as follows:

Download modules/from2.cmd

>>> from math import * >>> '%6f' % sqrt(8) '2.828427'

Explain why there are quotes around the value 2.828427.

5. Why do you think the media module mentioned in Section 4.3, Objects and Methods, on page 60 isn't part of the standard Python library? How do you think Python's developers decide what should be in the standard library and what shouldn't? If you need something that isn't in the standard library, where and how can you find it?

6. Write a program that allows the user to choose a file and then shows the picture twice.

7. Write a program that allows the user to choose a file, sets the red value of each pixel in the picture to 0, and shows the picture.

8. Write a program that allows the user to pick a file, halves the green value of each pixel in the picture, and shows the picture.

9. Write a program that allows the user to pick a file and makes it grayscale; it should calculate the average of red, green, and blue values of each pixel and then set the red, green, and blue values to that average.

10. Write a program that allows the user to pick a file, doubles the red value of each pixel in the picture, and shows the picture. What happens when a value larger than 255 is calculated?

11. Media outlets such as newspapers and TV stations sometimes "enhance" photographs by recoloring them or digitally combine pictures of two people to make them appear together. Do you think they should be allowed to use only unmodified images? given that almost all pictures and TV footage are now digital and have to be processed somehow for display, what would that rule actually mean in practice?

12. Suppose we want to test a function that calculates the distance between two XY points:

Download modules/distance.py

import math def distance(x0, y0, x1, y1):

'''Calculate the distance between (x0, y0) and (x1, y1).'''

a) Unlike the rounding-off version of to_celsius, this returns a floating-point number. Explain why this makes testing more difficult.

b) A friend of yours suggests testing the function like this:

Download modules/test_distance.py

import nose from distance import distance def close(left, right):

'''Test if two floating-point values are close enough.'''

def test_distance():

'''Test whether the distance function works correctly.'''

assert close(distance(1.0, 0.0, 1.0, 0.0), 0.0), 'Identical points fail.' assert close(distance(0.0, 0.0, 1.0, 0.0), 1.0), 'Unit distance fails.'

nose.runmodule()

Explain what your friend is trying to do. As gently as you can, point out two flaws in his approach.

### Chapter 5

Up to this point, each variable we have created has referred to a single number or string. In this chapter, we will work with collections of data and use a Python type named list. Lists contain 0 or more objects, and they allow us to store data such as 90 experiment measurements or 10,000 student IDs. We'll also see how to access files and represent their contents using lists.

Was this article helpful?

0 0