Week 1: Exercises

This is a set of optional programming tasks that you should be able to implement in Python.

I've included sample output below each task. (In some cases the sample output contains several program runs, separated by ===.)

1. Paint

Read integers representing the width, length and height of a room in meters, and print out the number of square meters of paint required to paint the walls and ceiling (but not the floor).

Width: 5
Length: 8
Height: 4
You need 144 square meters of paint.

2. The Greater Number

Read two integers X and Y and report which is greater (or that they are equal).

Enter X: 4
Enter Y: 6
Y is greater

3. Quotient

Read two integers X and Y, and print their quotient if X is exactly divisible by Y, or "indivisible" otherwise.

Enter X: 8
Enter Y: 2
8 divided by 2 is 4
===
Enter X: 10
Enter Y: 3
indivisible

4. Largest of Three

Read three integers, and print out the largest of them.

Enter X: 4
Enter Y: 17
Enter Z: 2
The largest is 17.

5. Numbers from 2 to 20

Print the even numbers from 2 to 20 on the console. (Don't use a separate print statement for each number!)

2
4
6
8
10
12
14
16
18
20

6. Factorial

Write a program that reads a number N ≥ 0 and prints the value of N!, i.e. 1 ⋅ 2 ⋅ … ⋅ N.

Enter N: 6
6! = 720

7. Exponentiation

Write a program that reads two values A (an integer) and B ≥ 0 and prints the value of AB. Do not use the built-in ** operator.

8. All Divisors

Write a program that reads a positive integer N and prints all of its divisors, as well as a count of the divisors.

Enter N: 12
1
2
3
4
6
12
There are 6 divisors

9. Making Change

Read a price in Czech crowns. Print out a combination of 20-Kč, 10-Kč, 5-Kč and 1-Kč coins that add up to the price, using the smallest possible number of coins.

Enter price: 67
20 Kc: 3
10 Kc: 0
5 Kc: 1
1 Kc: 2

10. Power of Two

Read an integer. Print "pow" if it is a power of 2, "no" otherwise. (Note: 1 is actually a power of 2, since 20 = 1).

Enter number: 64
pow
===
Enter number: 68
no
===
Enter number: 256
pow
===
Enter number: 1
pow

11. Number Guessing

Write a program that plays the following game: the user thinks of a number from 1 to 1000 and the computer guesses it.

Think of a number from 1 to 1000.
My guess: 500.  Is this (h)igh, (l)ow, or (c)orrect?  h
My guess: 250.  Is this (h)igh, (l)ow, or (c)orrect?  l
…

My guess: 467.  Is this (h)igh, (l)ow, or (c)orrect?  c

Total guesses: 8

12. Leap Years

Read a year from the console, and write out "leap" if it is a leap year, or "no leap" if it isn't.

A year is a leap year if it's divisible by 4, unless it's divisible by 100, in which case it isn't a leap year, unless it's divisible by 400, in which case it is actually a leap year.

Enter year: 1922
no leap
===
Enter year: 1900
no leap
===
Enter year: 2000
leap
===
Enter year: 2020
leap

13. Smallest Power

Write a program that reads an integer N and prints the smallest power of 2 that is ≥ N.

14. Largest Power

Write a program that reads an integer N ≥ 1 and prints the largest power of 2 that is ≤ N.

15. Number of Digits

Write a progam that reads an integer N and prints the number of decimal digits that the number contains. Do not use the integer's representation as a string in Python. Instead, use mathematical operation(s) to compute the number of digits. You can solve this exercise either using a loop, or with no loop.

16. Multiples of 3 and 5

(Project Euler, problem 1)

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

17. Even Fibonacci numbers

(Project Euler, problem 2)

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.