Introduction to Programming Class Exercises (Set 1: Writing Programs)
Chapter 1
(1.2) Write a program that displays Welcome to Python five times.
(1.6) Write a program that displays the result of 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9.
(1.8) Write a program that displays the area and perimeter of a circle that has a radius of 5.5.
(1.5) Write a program that displays the result of
9.5 x 4.5 − 2.5 x 3
45.5 − 3.5
Chapter 2
(2.2) Write a program that reads in the radius and length of a cylinder and computes the area and volume
using the following formulas:
area = radius * radius * π
volume = area * length
(2.4) Write a program that converts pounds into kilograms. The program prompts the user to enter a value
in pounds, converts it to kilograms, and displays the result. One pound is 0.454 kilograms
(2.6) Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For
example, if an integer is 932, the sum of all its digits is 14. (Hint: Use the % operator to extract digits, and
use the // operator to remove the extracted digit. For instance, 932 % 10 = 2 and 932 //10 = 93.)
Chapter 3
(3.6) 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.
(3.10) Write a program to display Greek letters α β δ ε ζ η θ. The Unicode of these characters are \u03b1
\u03b2 \u03b3 \u03b4 \u03b5\u03b6 \u03b7 \u03b8.
(3.11) Write a program that prompts the user to enter a four-digit integer and displays the number in
reverse order.
Chapter 4
(4.8) Write a program that prompts the user to enter three integers and displays them in increasing order.
(4.12) Write a program that prompts the user to enter an integer and checks whether the number is
divisible by both 5 and 6, divisible by 5 or 6, or just one of them (but not both).
(4.14) Write a program that lets the user guess whether a flipped coin displays the head or the tail. The
program randomly generates an integer 0 or 1, which represents head or tail. The program prompts the
user to enter a guess and reports whether the guess is correct or incorrect.
(4.16) Write a program that displays a random uppercase letter.
1
(4.22) Write a program that prompts the user to enter a point (x, y) and checks whether the point is within
the circle centered at (0, 0) with radius 10. For example, (4, 5) is inside the circle and (9, 9) is outside the
circle, as shown in the figure below.
(4.24) Write a program that simulates picking a card from a deck of 52 cards. Your program should display
the rank (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King) and suit (Clubs, Diamonds, Hearts, Spades)
of the card.
(4.26) Write a program that prompts the user to enter a three-digit integer and determines whether it is a
palindrome number. A number is a palindrome if it reads the same from right to left and from left to right.
(4.34) Write a program that prompts the user to enter a hex character and displays its corresponding
decimal integer.
Chapter 5
(5.4) Write a program that displays the following table (note that 1 mile is 1.609 kilometers):
Miles Kilometers
1 1.609
2 3.218
... …
9 15.481
10 16.090
(5.8) Write a program that prints the following table using the sqrt function in the math module.
Number Square Root
0 0.0000
2 1.4142
... …
18 5.2426
20 5.4721
(5.12) Write a program that displays, ten numbers per line, all the numbers from 100 to 1,000 that are
divisible by 5 and 6. The numbers are separated by exactly one space.
(5.14) Use a while loop to find the smallest integer n such that n2 is greater than 12,000.
(5.20) Use nested loops that display the following patterns in four separate programs:
2
Chapter 8
(8.2) You can check whether a string is a substring of another string by using the find method in the str
class. Write your own function to implement find. Write a program that prompts the user to enter two
strings and then checks whether the first string is a substring of the second string.
(8.6) Write a function that counts the number of letters in a string using the following header:
def countLetters(s):
(8.12) Biologists use a sequence of letters A, C, T, and G to model a genome. A gene is a substring of a
genome that starts after a triplet ATG and ends before a triplet TAG, TAA, or TGA. Furthermore, the
length of a gene string is a multiple of 3 and the gene does not contain any of the triplets ATG, TAG, TAA,
and TGA. Write a program that prompts the user to enter a genome and displays all genes in the genome.
If no gene is found in the input sequence, the program displays no gene is found.
Chapter 10
(10.2) Write a program that reads a list of integers and displays them in the reverse order in which they
were read.
(10.4) Write a program that reads an unspecified number of scores and determines how many scores are
above or equal to the average and how many scores are below the average. Assume the input numbers
are separated by one space in one line.
(10.8) Write a function that returns the index of the smallest element in a list of integers. If the number of
such elements is greater than 1, return the smallest index. Use the following header:
def indexOfSmallestElement(lst):
Write a test program that prompts the user to enter a list of numbers, invokes this function to return the
index of the smallest element, and displays the index.
(10.12) Write a function that returns the greatest common divisor (GCD) of integers in a list. Use the
following function header:
def gcd(numbers):
Write a test program that prompts the user to enter five numbers, invokes the function to find the GCD of
these numbers, and displays the GCD.
(10.26) Write the following function that merges two sorted lists into a new sorted list:
def merge(list1, list2):
Implement the function in a way that takes len(list1) + len(list2) comparisons. Write a test program that
prompts the user to enter two sorted lists and displays the merged list.
Chapter 14
(14.2) Write a program that reads an unspecified number of integers and finds the ones that have the most
occurrences. For example, if you enter 2 3 40 3 5 4 –3 3 3 2 0, the number 3 occurs most often. Enter all
numbers in one line. If not one but several numbers have the most occurrences, all of them should be
reported. For example, since 9 and 3 appear twice in the list 9 30 3 9 3 2 4, both occurrences should be
reported.
(14.10) Rewrite Exercise 11.40 using a dictionary to store the pairs of states and capitals so that the
questions are randomly displayed.
3
Chapter 6
(6.2) Write a function that computes the sum of the digits in an integer. Use the following function header:
def sumDigits(n):
For example, sumDigits(234) returns 9 (Hint: Use the % operator to extract digits, and the // operator to
remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10 To remove 4 from 234, use
234 // 10. Use a loop to repeatedly extract and remove the digits until all the digits are extracted.) Write a
test program that prompts the user to enter an integer and displays the sum of all its digits.
(6.4) Write the following function to display an integer in reverse order:
def reverse(number):
For example, reverse(3456) displays 6543. Write a test program that prompts the user to enter an integer
and displays its reversal.
(6.8) Write a module that contains the following two functions:
def celsiusToFahrenheit(celsius): # Converts from Celsius to Fahrenheit
def fahrenheitToCelsius(fahrenheit): # Converts from Fahrenheit to Celsius
The formulas for the conversion are:
celsius = (5 / 9) * (fahrenheit – 32)
fahrenheit = (9 / 5) * celsius + 32
Write a test program that invokes these functions to display the following tables:
(6.12) Write a function that prints characters using the following header:
def printChars(ch1, ch2, numberPerLine):
This function prints the characters between ch1 and ch2 with the specified numbers per line. Write a test
program that prints ten characters per line from 1 to Z.
(6.18) Write a function that displays an n-by-n matrix using the following header:
def printMatrix(n):
Each element is 0 or 1, which is generated randomly. Write a test program that prompts the user to enter n
and displays an n-by-n matrix.
(6.20) Rewrite Listing 2.9, [Link], using the following function for computing the distance
between two points.
def distance(x1, y1, x2, y2):
(6.32) Exercise 4.21 uses Zeller’s congruence to calculate the day of the week. Simplify Listing 6.13,
[Link], using Zeller’s algorithm to get the start day of the month.
Chapter 15
(15.2) Rewrite the fib function in Listing 15.2 using iterations. (Hint: To compute fib(n) without recursion,
you need to obtain fib(n - 2) and fib(n - 1) first.) Let f0 and f1 denote the two previous Fibonacci numbers.
The current Fibonacci number would then be f0 + f1. The algorithm can be
described as follows:
f0 = 0 # For fibs(0)
4
f1 = 1 # For fib(1)
for i in range(2, n + 1):
currentFib = f0 + f1
f0 = f1
f1 = currentFib
# After the loop, currentFib is fib(n)
Write a test program that prompts the user to enter an index and displays its Fibonacci number.
(15.4) Write a recursive function to compute the following series:
Write a test program that displays m(i) for i 1, 2, ..., 10.
(15.6) Write a recursive function to compute the following series:
Write a test program that prompts the user to enter an integer for i and displays m(i).
(15.10) Write a recursive function that finds the number of occurrences of a specified letter in a string
using the following function header.
def count(s, a):
For example, count("Welcome", 'e') returns 2. Write a test program that prompts the user to enter a
string and a character, and displays the number of occurrences for the character in the string.
(15.12) Write a recursive function that returns the largest integer in a list. Write a test program that
prompts the user to enter a list of integers and displays the largest element.
Chapter 13
(13.2) Write a program that will count the number of characters, words, and lines in a file. Words are
separated by a whitespace character. Your program should prompt the user to enter a filename.
(13.4) Write a program that writes 100 integers created randomly into a file. Integers are separated by a
space in the file. Read the data back from the file and display the sorted data. Your program should
prompt the user to enter a filename. If the file already exists, do not override it.
(13.6) Write a program that counts the number of words in President Abraham Lincoln’s Gettysburg
Address from [Link]
(13.10) Modify the Rational class in Listing 8.4, [Link], to throw a RuntimeError exception if the
denominator is 0.
(13.12) Define an exception class named TriangleError that extends RuntimeError. The TriangleError
class contains the private data fields side1, side2, and side3 with accessor methods for the three sides of
a triangle. Modify the Triangle class in Exercise 12.1 to throw a TriangleError exception if the three given
sides cannot form a triangle.
[END]