Ejercicios de Programación 03
Ejercicios de Programación 03
KEY TERMS
backslash (\) 69 methods 75
character encoding 68 newline 69
end-of-line 69 object 74
escape sequence 69 string 67
line break 69 whitespace characters 76
CHAPTER SUMMARY
1. Python provides the mathematical functions abs, max, min, pow, and round in the
interpreter and the functions fabs, ceil, floor, exp, log, sqrt, sin, asin, cos,
acos, tan, degrees, and radians in the math module.
3. An escape sequence is a special syntax that begins with the character \ followed by a
letter or a combination of digits to represent special characters, such as \', \", \t,
and \n.
4. The characters ' ', \t , \f, \r, and \n are known as the whitespace characters.
5. All data including numbers and strings are objects in Python. You can invoke
methods to perform operations on the objects.
6. You can use the format function to format a number or a string and return the result
as a string.
TEST QUESTIONS
Do test questions for this chapter online at www.cs.armstrong.edu/liang/py/test.html.
PROGRAMMING EXERCISES
Section 3.2
3.1 (Geometry: area of a pentagon) Write a program that prompts the user to enter the
length from the center of a pentagon to a vertex and computes the area of the pen-
tagon, as shown in the following figure.
r
86 Chapter 3 Mathematical Functions, Strings, and Objects
3 23 2
The formula for computing the area of a pentagon is Area = s , where s is
2 p
the length of a side. The side can be computed using the formula s = 2r sin ,
5
where r is the length from the center of a pentagon to a vertex. Here is a sample
run:
Enter the length from the center to a vertex: The area of the 5.5
pentagon is 108.61
*3.2 (Geometry: great circle distance) The great circle distance is the distance between
two points on the surface of a sphere. Let (x1, y1) and (x2, y2) be the
geographical latitude and longitude of two points. The great circle distance
between the two points can be computed using the following formula:
Write a program that prompts the user to enter the latitude and longitude of two
points on the earth in degrees and displays its great circle distance. The average
earth radius is 6,371.01 km. Note that you need to convert the degrees into
radians using the math.radians function since the Python trigonometric
functions use radians. The latitude and longitude degrees in the formula are for
north and west. Use negative to indicate south and east degrees. Here is a sample
run:
*3.3 (Geography: estimate areas) Find the GPS locations for Atlanta, Georgia;
Orlando, Florida; Savannah, Georgia; and Charlotte, North Carolina from
www.gps-data-team.com/map/ and compute the estimated area enclosed by these
four cities. (Hint: Use the formula in Programming Exercise 3.2 to compute the
distance between two cities. Divide the polygon into two triangles and use the
for- mula in Programming Exercise 2.14 to compute the area of a triangle.)
3.4 (Geometry: area of a pentagon) The area of a pentagon can be computed using
the following formula (s is the length of a side):
5 * s2
Area =
p
4 * tan ¢ ≤
5
Write a program that prompts the user to enter the side of a pentagon and displays
the area. Here is a sample run:
Sections 3.3–3.6
*3.6 (Find the character of an ASCII code) Write a program that receives an ASCII
code (an integer between 0 and 127) and displays its character. For example, if
the user enters 97, the program displays the character a. Here is a sample run:
3.7 (Random character) Write a program that displays a random uppercase letter
using the time.time() function.
*3.8 (Financial application: monetary units) Rewrite Listing 3.4, ComputeChange.py,
to fix the possible loss of accuracy when converting a float value to an int value.
Enter the input as an integer whose last two digits represent the cents. For exam-
ple, the input 1156 represents 11 dollars and 56 cents.
*3.9 (Financial application: payroll) Write a program that reads the following infor-
mation and prints a payroll statement:
Employee’s name (e.g., Smith)
Number of hours worked in a week (e.g., 10)
Hourly pay rate (e.g., 9.75)
Federal tax withholding rate (e.g., 20%)
State tax withholding rate (e.g., 9%)
A sample run is shown below:
Enter employee's name: Smith
Enter number of hours worked in a week: 10
Enter hourly pay rate: 9.75
Enter federal tax withholding rate: 0.20
Enter state tax withholding rate: Employee 0.09
Name: Smith
88 Chapter 3 Mathematical Functions, Strings, and Objects
*3.10 (Turtle: display Unicodes) Write a program to display Greek letters abgdPzhu.
The Unicode of these characters are \u03b1 \u03b2 \u03b3 \u03b4 \u03b5
\u03b6 \u03b7 \u03b8.
3.11 (Reverse number) Write a program that prompts the user to enter a four-digit inte-
ger and displays the number in reverse order. Here is a sample run:
Sections 3.7–3.8
**3.12 (Turtle: draw a star) Write a program that prompts the user to enter the length of the
star and draw a star, as shown in Figure 3.5a. (Hint: The inner angle of each
point in the star is 36 degrees.)
*3.13 (Turtle: display a STOP sign) Write a program that displays a STOP sign, as shown
in Figure 3.5b. The hexagon is in red and the text is in white.
3.14 (Turtle: draw the Olympic symbol ) Write a program that prompts the user to
enter the radius of the rings and draws an Olympic symbol of five rings of the
same size with the colors blue, black, red, yellow, and green, as shown in
Figure 3.5c.
*3.15 (Turtle: paint a smiley face) Write a program that paints a smiley face, as shown in
Figure 3.6a.
Programming Exercises 89
(a) (b)
FIGURE 3.6 The program paints a smiley face in (a) and draws five shapes with bottom edges parallel to the x-axis in (b).
**3.16 (Turtle: draw shapes) Write a program that draws a triangle, square, pentagon,
hexagon, and octagon, as shown in Figure 3.6b. Note that the bottom edges of
these shapes are parallel to the x-axis. (Hint: For a triangle with a bottom line
parallel to the x-axis, set the turtle’s heading to 60 degrees.)
*3.17 (Turtle: triangle area) Write a program that prompts the user to enter the three
points p1, p2, and p3 for a triangle and display its area below the triangle, as
shown in Figure 3.7a. The formula for computing the area of a triangle is given
in Exercise 2.14.
FIGURE 3.7 The program displays (a) the area of the triangle and (b) the angles for the triangle. (c) The program draws a
line.
*3.18 (Turtle: triangle angles) Revise Listing 3.2, ComputeAngles.py, to write a pro- gram
that prompts the user to enter the three points p1, p2, and p3 for a triangle and
display its angles, as shown in Figure 3.7b.
**3.19 (Turtle: draw a line) Write a program that prompts the user to enter two points and
draw a line to connect the points and displays the coordinates of the points, as
shown in Figure 3.7c.