This is a set of optional programming tasks that you should be able to implement in Python.
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 progran that reads a number 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 number N, then print an N x N square of asterisks as in the example below.
Enter N: 5 ***** * * * * * * *****
Read a number N, then print an N x N triangle of asterisks as in the example below.
Enter N: 6 * ** *** **** ***** ******
Read a number N, then use asterisks to print a letter L with height and width N, and a thickness of 3.
Size: 7 *** *** *** *** ******* ******* *******
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. (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
Write a program that reads a series of 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
Read two numbers on the same line representing a 24-hour time in hours and minutes. Print the time that is 20 minutes later, wrapping past midnight if necessary.
Time: 2 30 2:50 === Time: 5 50 6:10 === Time: 23 55 0:15
Read two numbers on the same line representing a 24-hour time in hours and minutes. Read another number representing an offset N in minutes (a positive number). Print the time that is N minutes later, wrapping past midnight if necessary.
Time: 2 30 Offset: 20 2:50 === Time: 5 50 Offset: 100 7:30 === Time: 23 55 Offset: 1000 16:35
Write a program that reads the values of (x + y) and (x – y), where x and y are unknown real values. The program should solve for x and y and print their values, with one digit after the decimal point.
Enter x + y: 8.9 Enter x – y: 1.7 x = 5.3 y = 3.6
Write a program that reads the values of (x + y) and (xy), where x and y are unknown real values. The program should solve for x and y and print their values, with one digit after the decimal point. If no solution is possible, write "No solution".
Enter x + y: 8 Enter xy: 15 x = 3 y = 5 === Enter x + y: 1 Enter xy: 2 No solution
(Project Euler, problem 3)
What is the largest prime factor of the number 600851475143 ?