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 ===.)
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.
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
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
Read three integers, and print out the largest of them.
Enter X: 4 Enter Y: 17 Enter Z: 2 The largest is 17.
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
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
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.
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
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
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
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
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
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
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
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.
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
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
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
Write a program that reads an integer N and prints the smallest power of 2 that is ≥ N.
Write a program that reads an integer N ≥ 1 and prints the largest power of 2 that is ≤ N.
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.
(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.
(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.