Week 2: Exercises

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

1. 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

2. 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

3. 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.

4. 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

5. 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

6. Square

Read a number N, then print an N x N square of asterisks as in the example below.

Enter N: 5
*****
*   *
*   *
*   *
*****

7. Triangle

Read a number N, then print an N x N triangle of asterisks as in the example below.

Enter N: 6
     *
    **
   ***
  ****
 *****
******

8. The Ell

Read a number N, then use asterisks to print a letter L with height and width N, and a thickness of 3.

Size: 7
***
***
***
***
*******
*******
*******

9. Average

Read a set of floating-point numbers from the console until the user presses ctrl+D (Unix, macOS) or ctrl-Z (Windows). Print their average.

2.5
3.5
5.5
6.5
^D
4.5

10. Positive and Negative

Read integers from the console until the user presses ctrl+D or ctrl+Z. Write the sum of the positive integers and the sum of the negative integers.

Enter numbers:
4
2
-6
-1
5
-2
^D
Positive sum: 11
Negative sum: -9

11. Equal To The First

Read integers from the console until the user presses ctrl+D or ctrl+Z. Write a number indicating how many of the integers were equal to the first one (not including the first one itself).

Enter numbers:
4
2
2
8
4
4
3
^D
2 numbers were equal to 4
===
Enter numbers:
5
3
6
^D
0 numbers were equal to 5

12. 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

13. 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. (This is the same game we wrote in the lecture, but with the roles reversed.)

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

14. Second Largest

Write a program that reads a series of non-negative integers from standard input until the user presses Ctrl+D or Ctrl+Z. The program should then print the second largest of the integers that were read.

15. Rolling N Dice

Read a number N from the console. Simulate rolling N 6-sided dice. Write the value of each die roll, followed by the sum of all of them.

How many dice? 4
6 2 6 1
total: 15
===
How many dice? 2
6 2
total: 8

16. Flipping a Coin

Simulate coin flips, printing an 'H' each time the coin falls on heads and 'T' each time it falls on tails. Once you reach the third H, stop and write how many coins were flipped.

HTTTHTH
7 flips
===
TTTHTTTTTHH
11 flips

17. 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

18. 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.

19. 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.

20. 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.