Here are the exercises for the Python Basics module. These exercises are designed to test your understanding of the concepts covered in the module. You can use the exercises to practice and improve your Python skills.
Covered Topics:
- Simple Input/Output in a loop
- Break and Continue
- Nested Loops
- Conditional Statements
- Loops
Problem: Write a program timer that asks the user for the number of seconds n to start the countdown from.
- An integer representing the number of seconds to start the countdown from.
- A countdown from n to 1, followed by the message "Start!".
No. | Inputs | Outputs |
---|---|---|
1 | 5 | 5 4 3 2 1 Start! |
2 | 3 | 3 2 1 Start! |
3 | 1 | 1 Start! |
The problem tests the ability to use loops and conditional statements to implement a countdown timer.
Problem: Write a program that asks the user for two numbers n
and m
and prints the number n
m
times in a row.
- Two integers representing the numbers
n
andm
.
- The number
n
printedm
times in a row.
No. | Inputs | Outputs |
---|---|---|
1 | 10 5 |
10 10 10 10 10 |
2 | 3 3 |
3 3 3 |
3 | 7 1 |
7 |
The problem tests the ability to use loops and conditional statements to print a number multiple times.
Problem: Write a program to output all integers from 20
to n
inclusive (where n > 20
), where n
is an integer entered by the user.
- An integer representing the upper limit of the range.
- A sequence of integers from
20
ton
inclusive.
No. | Inputs | Outputs |
---|---|---|
1 | 25 | 20 21 22 23 24 25 |
2 | 30 | 20 21 22 23 24 25 26 27 28 29 30 |
3 | 37 | 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
4 | 20 | 20 |
The problem tests the ability to use loops and conditional statements to print a sequence of numbers.
Problem: Write a program to print integers from 1
to n
with the number of #
characters equal to the value of the number.
- An integer representing the upper limit of the range.
- A sequence of integers from
1
ton
inclusive, with the number of#
characters equal to the value of the number.
No. | Inputs | Outputs |
---|---|---|
1 | 5 | 1 # 2 ## 3 ### 4 #### 5 ##### |
2 | 3 | 1 # 2 ## 3 ### |
3 | 7 | 1 # 2 ## 3 ### 4 #### 5 ##### 6 ###### 7 ####### |
The problem tests the ability to use conditional statements to classify an input into one of several categories.
Problem: Write a program to calculate the sum of all integers from 1
to n
inclusive.
- An integer representing the upper limit of the range.
- An integer representing the sum of all integers from
1
ton
inclusive.
No. | Inputs | Outputs |
---|---|---|
1 | 100 | 5050 |
2 | 16 | 136 |
3 | 1 | 1 |
The problem tests the ability to solve a quadratic equation and handle different cases of discriminant values.
Problem: Write a program to print a pattern of *
characters as shown in the output for a given value of n
.
- An integer representing the number of rows in the pattern.
- A pattern of
*
characters as shown in the examples.
No. | Inputs | Outputs |
---|---|---|
1 | 5 | * ** *** **** ***** |
2 | 3 | * ** *** |
3 | 7 | * ** *** **** ***** ****** ******* |
The problem tests the ability to use loops and conditional statements to print a pattern of characters.
Problem: Write a program to print a pattern of #
characters as shown in the output for a given value of n
.
- An integer representing the number of rows in the pattern.
- A pattern of
#
characters as shown in the examples.
No. | Inputs | Outputs |
---|---|---|
1 | 5 | ## #Β # #Β Β # #Β Β Β # #Β Β Β Β # |
2 | 3 | ## #Β # #Β Β # |
3 | 7 | ## #Β # #Β Β # #Β Β Β # #Β Β Β Β # #Β Β Β Β Β # #Β Β Β Β Β Β # |
The problem tests the ability to use loops and conditional statements to print a pattern of characters.
Problem: Write a program to print all even numbers from 1
to n
in a single row separated by a space. Use the continue
statement to skip odd numbers.
- An integer representing the upper limit of the range.
- A sequence of even numbers from
1
ton
inclusive, separated by a space.
No. | Inputs | Outputs |
---|---|---|
1 | 10 | 2 4 6 8 10 |
2 | 7 | 2 4 6 |
3 | 20 | 2 4 6 8 10 12 14 16 18 20 |
The problem tests the ability to work with digits of a number.
Problem: Write a program to print all integers from a
to b
inclusive that are multiples of c
. The numbers a
, b
, and c
are integers entered by the user.
- Three integers representing the lower limit
a
, upper limitb
, and the multiplec
.
- A sequence of integers from
a
tob
inclusive that are multiples ofc
.
No. | Inputs | Outputs |
---|---|---|
1 | 1 10 2 |
2 4 6 8 10 |
2 | 3 15 3 |
3 6 9 12 15 |
3 | 5 20 5 |
5 10 15 20 |
The problem tests the ability to work with digits of a number and compare their values.
Problem: Write a program to print a table of equivalence between mass in pounds and mass in kilograms for the values of n
pounds (1 pound = 453 grams) in the form of a table.
- An integer representing the number of pounds.
- A table of equivalence between mass in pounds and mass in kilograms for the values of
n
pounds.
No. | Inputs | Outputs |
---|---|---|
1 | 5 | 1 0.45 2 0.91 3 1.36 4 1.81 5 2.27 |
2 | 3 | 1 0.45 2 0.91 3 1.36 |
3 | 7 | 1 0.45 2 0.91 3 1.36 4 1.81 5 2.27 6 2.72 7 3.18 |
The problem tests the ability to use loops and conditional statements to print a table of values.
Problem: Write a program to calculate the sum of the expression 1/2 + 2/3 + 3/4 + β¦β + n/(n + 1)
for a given value of n
.
- An integer representing the upper limit of the range.
- A floating-point number representing the sum of the expression
1/2 + 2/3 + 3/4 + β¦β + n/(n + 1)
.
No. | Inputs | Outputs |
---|---|---|
1 | 5 | 3.55 |
2 | 10 | 7.98 |
3 | 3 | 1.92 |
The problem tests the ability to use loops and conditional statements to calculate the sum of a series.
Problem: Write a program to calculate the sum of all three-digit numbers that are divisible by n
, where n
is an integer entered by the user.
- An integer representing the number
n
.
- An integer representing the sum of all three-digit numbers that are divisible by
n
.
No. | Inputs | Outputs |
---|---|---|
1 | 125 | 3500 |
2 | 440 | 1320 |
3 | 600 | 600 |
4 | 1000 | 0 |
The problem tests the ability to use loops and conditional statements to calculate the sum of a sequence of numbers.
Problem: Write a program to prompt the user for a password. If the user enters the wrong password, the program should display an error message and prompt the user to try again. If the user enters the correct password, the program should display a success message.
- An integer representing the password.
- A sequence of integers representing the password entered by the user.
- A message indicating whether the password is correct or incorrect.
No. | Inputs | Outputs |
---|---|---|
1 | 12345 | 111 Error 45 Error 12345 Done |
2 | 123 | 111 Error 45 Error 12345 Error 123 Done |
3 | 111 | 111 Done |
The problem tests the ability to use loops and conditional statements to validate user input.
Problem: Write a program to determine how many of n
numbers are equal to zero and output this count.
- An integer
n
representing the number of integers to be entered. n
integers representing the numbers.
- An integer representing the count of numbers that are equal to zero.
No. | Inputs | Outputs |
---|---|---|
1 | 4 0 23 11 0 |
2 |
2 | 3 0 0 0 |
3 |
3 | 5 1 2 3 4 5 |
0 |
The problem tests the ability to use loops and conditional statements to count the number of occurrences of a specific value.
Problem: Write a program to draw a staircase as shown in the output for a given value of n
.
- An integer representing the number of steps in the staircase.
- A staircase as shown in the examples.
No. | Inputs | Outputs Reference |
---|---|---|
1 | 4 | Pattern 1 |
2 | 3 | Pattern 2 |
3 | 7 | Pattern 3 |
#
##
###
####
#
##
###
#
##
###
####
#####
######
The problem tests the ability to use loops and conditional statements to print a pattern of characters.
Problem: Write a program to draw a pattern as shown in the output for a given value of n
and m
.
- Two integers representing the number of rows
n
and the number of columnsm
.
- A pattern as shown in the examples.
No. | Inputs | Outputs |
---|---|---|
1 | 6 3 |
Pattern 1 |
2 | 3 3 |
Pattern 2 |
3 | 3 7 |
Pattern 3 |
0 0 0
1 1 1
2 2 2
3 3 3
4 4 4
5 5 5
0 0 0
1 1 1
2 2 2
0 0 0 0 0 0 0
1 1 1 1 1 1 1
2 2 2 2 2 2 2
Problem: Write a program to determine the total number of erors collected over n
days.
- An integer
n
representing the number of days. n
integers representing the number of errors collected each day.
- An integer representing the total number of errors collected over
n
days.
No. | Inputs | Outputs |
---|---|---|
1 | 6 45 101 67 43 21 0 |
277 |
2 | 3 0 0 0 |
0 |
3 | 5 1 2 3 4 5 |
15 |
The problem tests the ability to use loops and conditional statements to calculate the sum of a sequence of numbers.
Problem: Write a program to print all two-digit numbers, the sum of the squares of the digits of which is divisible by n
. The number n
is an integer entered by the user.
- An integer representing the number
n
.
- A sequence of two-digit numbers, the sum of the squares of the digits of which is divisible by
n
.
No. | Inputs | Outputs |
---|---|---|
1 | 5 | 12, 13, 17, 18, 21, 24, 26, 29, 31, 34, 36, 39, 42, 43, 47, 48, 50, 55, 62, 63, 67, 68, 71, 74, 76, 79, 81, 84, 86, 89, 92, 93, 97, 98 |
2 | 7 | 70, 77 |
3 | 10 | 13, 17, 24, 26, 29, 31, 34, 36, 39, 42, 43, 47, 48, 51, 53, 54, 57, 58, 62, 63, 67, 68, 71, 74, 76, 79, 82, 83, 87, 88, 91, 93, 94, 97, 98 |
4 | 100 | 68, 86 |
5 | 162 | 99 |
The problem tests the ability to use loops and conditional statements to print a sequence of numbers.
Problem: Write a program to print all odd numbers from 1
to b
, where b
is an integer entered by the user. You cannot use the branching construct.
- An integer representing the upper limit of the range.
- A sequence of odd numbers from
1
tob
inclusive.
No. | Inputs | Outputs |
---|---|---|
1 | 15 | 1 3 5 7 9 11 13 15 |
2 | 8 | 1 3 5 7 |
3 | 5 | 1 3 5 |
The problem tests the ability to use loops and conditional statements to print a sequence of numbers.
Problem: Write a program to calculate the sum of the factorials of all integers from 1
to n
inclusive.
- An integer representing the upper limit of the range.
- An integer representing the sum of the factorials of all integers from
1
ton
inclusive.
No. | Inputs | Outputs |
---|---|---|
1 | 3 | 9 |
2 | 4 | 33 |
3 | 5 | 153 |
The problem tests the ability to use loops and conditional statements to calculate the sum of a series.
Problem: Write a program to print all integers formed from the input number by discarding the last digit from each previous number.
- An integer representing the input number.
- A sequence of integers formed from the input number by discarding the last digit from each previous number.
No. | Inputs | Outputs |
---|---|---|
1 | 138945 | 13894 1389 138 13 1 |
2 | 123456 | 12345 1234 123 12 1 |
3 | 987654 | 98765 9876 987 98 9 |
Problem: Write a program to calculate the average of all integers in a sequence that ends with the number 0
. The number 0
is not included in the sequence, but is used as a sign of its end.
- A sequence of integers that ends with the number
0
.
- A floating-point number representing the average of all integers in the sequence.
No. | Inputs | Outputs |
---|---|---|
1 | 3 4 5 0 |
4.0 |
2 | 1 2 3 4 5 0 |
3.0 |
3 | 10 20 30 40 50 0 |
30.0 |
The problem tests the ability to use loops and conditional statements to calculate the average of a sequence of numbers.
Problem: Write a program to determine the number of even elements in a sequence that ends with the number 0
. The number 0
is not included in the sequence, but is used as a sign of its end.
- A sequence of integers that ends with the number
0
.
- An integer representing the number of even elements in the sequence.
No. | Inputs | Outputs |
---|---|---|
1 | 3 6 9 8 0 |
2 |
2 | 1 2 3 4 5 0 |
2 |
3 | 10 20 30 40 50 0 |
5 |
The problem tests the ability to use loops and conditional statements to count the number of occurrences of a specific value.
Problem: Write a program to determine the number of days it takes for a car to cover a distance greater than t
km, where the car covers d
km on the first day and increases its distance by 10% each day.
- Two integers representing the distance
d
and the target distancet
.
- A floating-point number representing the total distance covered and an integer representing the number of days it takes to cover a distance greater than
t
km.
No. | Inputs | Outputs |
---|---|---|
1 | 10 100 |
114.36 km, 8 days |
2 | 20 50 |
66.20 km, 3 days |
3 | 5 25 |
30.53 km, 5 days |
4 | 1 10 |
11.44 km, 8 days |
5 | 50 100 |
155.13 km, 3 days |
The problem tests the ability to use loops and conditional statements to calculate the sum of a series.
Problem: Write a program to determine the number of three-digit numbers, the sum of the digits of which is equal to a certain integer value n
, which is entered by the user.
- An integer representing the number
n
.
- An integer representing the number of three-digit numbers, the sum of the digits of which is equal to
n
.
No. | Inputs | Outputs |
---|---|---|
1 | 27 | 1 |
2 | 2 | 3 |
3 | 20 | 36 |
The problem tests the ability to use loops and conditional statements to print a sequence of numbers.
Problem: Write a program to calculate the sum of the first n
terms of the series Pi = 4/1 - 4/3 + 4/5 - 4/7 + ...
.
- An integer representing the number of terms in the series.
- A floating-point number representing the sum of the first
n
terms of the series.
No. | Inputs | Outputs |
---|---|---|
1 | 10 | 3.0418396189294032 |
2 | 5 | 3.3396825396825403 |
3 | 15 | 3.017071817071818 |
4 | 120 | 3.1332594798865546 |
5 | 500 | 3.139592655589783 |
The problem tests the ability to use loops and conditional statements to calculate the sum of a series.
Problem: Given integers a and b. Find their product without using the multiplication operation.
- Two integers
a
andb
.
- An integer representing the product of
a
andb
.
No. | Inputs | Outputs |
---|---|---|
1 | 7 8 |
56 |
2 | 5 6 |
30 |
3 | 3 4 |
12 |
4 | 10 10 |
100 |
The problem tests the ability to use loops and conditional statements to calculate the product of two numbers.
Problem: Write a program that reads numbers (one per line) until the sum of the entered numbers equals 0, and immediately after that, outputs the sum of the squares of all the entered numbers. It is guaranteed that at some point the sum of the entered numbers will be 0, and reading should not continue after that.
- A series of integers.
- An integer representing the sum of the squares of all the entered numbers.
No. | Inputs | Outputs |
---|---|---|
1 | 1 2 3 4 -4 -3 -2 -1 |
60 |
2 | 1 2 3 4 -10 |
130 |
3 | 1 2 3 4 -1 -2 -3 -4 |
60 |
The problem tests the ability to use loops and conditional statements to calculate the sum of the squares of a sequence of numbers.
Problem: A single-celled amoeba divides into 2
cells every 3
hours. Determine how many cells there will be after t
hours if there was initially one amoeba.
- An integer representing the number of hours
t
.
- An integer representing the number of cells.
No. | Inputs | Outputs |
---|---|---|
1 | 6 | 4 |
2 | 9 | 8 |
3 | 24 | 256 |
Problem: Write a program to determine the lowest temperature over a period of time and whether the temperature has dropped below -18 degrees.
- An integer representing the number of days.
n
integers representing the temperatures for each day.
- An integer representing the lowest temperature and a string representing whether the temperature has dropped below -15 degrees.
No. | Inputs | Outputs |
---|---|---|
1 | 3 -20 2 -18 |
-20 Yes |
2 | 5 -10 -15 -20 -25 -30 |
-30 Yes |
3 | 7 -5 -10 -15 -20 -25 -30 -35 |
-35 Yes |
The problem tests the ability to use loops and conditional statements to calculate the lowest temperature and determine whether it has dropped below a certain value.
Problem: Write a program to determine the difference between the maximum and minimum speeds of the cars and the number of cars whose speed did not exceed 30 km/h. The program receives the number of recorded cars n
(1 β€ n β€ 30) as input, followed by their speeds. The speed values cannot be less than 1 or greater than 300. The program should first output the difference between the maximum and minimum speeds of the cars, then the number of cars whose speed did not exceed 30 km/h.
- An integer representing the number of recorded cars
n
(1 β€ n β€ 30). n
integers representing the speeds of the cars.
- An integer representing the difference between the maximum and minimum speeds of the cars.
- An integer representing the number of cars whose speed did not exceed 30 km/h.
No. | Inputs | Outputs |
---|---|---|
1 | 3 15 25 140 |
125 2 |
2 | 5 10 20 30 40 50 |
40 3 |
3 | 7 5 10 15 20 25 30 35 |
30 6 |
The problem tests the ability to use loops and conditional statements to calculate the difference between the maximum and minimum speeds of the cars and the number of cars whose speed did not exceed a certain value.
Problem: Write a program to output a two-dimensional square table of n x n
numbers, where n
is an integer entered by the user. The main diagonal of the table (from the top left value to the bottom right value) should contain 0
, above the main diagonal should be 1
, and below it should be -1
. To display the table values nicely, use the tab character \t
.
- An integer representing the number
n
.
- A two-dimensional square table of
n x n
numbers.
No. | Inputs | Outputs |
---|---|---|
1 | 5 | 0 1 1 1 1 -1 0 1 1 1 -1 -1 0 1 1 -1 -1 -1 0 1 -1 -1 -1 -1 0 |
2 | 3 | 0 1 1 -1 0 1 -1 -1 0 |
3 | 7 | 0 1 1 1 1 1 1 -1 0 1 1 1 1 1 -1 -1 0 1 1 1 1 -1 -1 -1 0 1 1 1 -1 -1 -1 -1 0 1 1 -1 -1 -1 -1 -1 0 1 -1 -1 -1 -1 -1 -1 0 |
The problem tests the ability to use loops and conditional statements to print a two-dimensional square table of numbers.
Problem: Write a program to print a pattern of number characters as shown in the output for a given value of n
.
- An integer representing the number of rows in the pattern.
- A pattern of number characters as shown in the examples.
No. | Inputs | Outputs |
---|---|---|
1 | 5 | 1 12 123 1234 12345 |
2 | 3 | 1 12 123 |
3 | 7 | 1 12 123 1234 12345 123456 1234567 |
Problem: Write a program to print all n
-digit odd natural numbers in descending order.
- An integer representing the number
n
.
- A sequence of
n
-digit odd natural numbers in descending order.
No. | Inputs | Outputs |
---|---|---|
1 | 1 | 9 7 5 3 1 |
2 | 2 | 99 97 95 93 91 89 87 85 83 81 79 77 75 73 71 69 67 65 63 61 59 57 55 53 51 49 47 45 43 41 39 37 35 33 31 29 27 25 23 21 19 17 15 13 11 |
The problem tests the ability to use loops and conditional statements to print a sequence of numbers.
Problem: Write a program to determine the greatest common divisor (GCD) of two natural numbers a
and b
. Use the Euclidean algorithm.
- Two natural numbers
a
andb
.
- An integer representing the greatest common divisor of the two numbers.
No. | Inputs | Outputs |
---|---|---|
1 | 8 2 |
2 |
2 | 12 3 |
3 |
3 | 15 5 |
5 |
The problem tests the ability to use loops and conditional statements to calculate the greatest common divisor of two numbers.
Problem: Write a program to determine the results of the integer division of a
by b
and the remainder of the division of a
by b
, without using the standard integer division and remainder operations.
- Two integers
a
andb
(a > b
).
- Two integers representing the results of the integer division of
a
byb
and the remainder of the division ofa
byb
.
No. | Inputs | Outputs |
---|---|---|
1 | 16 5 |
3 1 |
2 | 20 3 |
6 2 |
3 | 25 4 |
6 1 |
The problem tests the ability to use loops and conditional statements to calculate the results of the integer division and the remainder of the division of two numbers.
Problem: Write a program to determine whether the difference between the maximum and minimum digits of a natural number n
is even.
- A natural number
n
.
- A boolean value representing whether the difference between the maximum and minimum digits of
n
is even.
No. | Inputs | Outputs |
---|---|---|
1 | 2134389 | True |
2 | 1234 | False |
3 | 123456789 | True |
The problem tests the ability to use loops and conditional statements to determine whether a certain condition is met.
Problem: Write a program to calculate the sum of the digits of an integer n
. The program should take into account that a negative integer may be entered.
- An integer
n
.
- An integer representing the sum of the digits of
n
.
No. | Inputs | Outputs |
---|---|---|
1 | -123 | 6 |
2 | 1 | 1 |
3 | 412098 | 24 |
The problem tests the ability to use loops and conditional statements to calculate the sum of the digits of an integer.
Problem: Write a program to obtain the Fibonacci sequence from 0 to n
, where n
is an integer. The Fibonacci sequence is a series of numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, .... Each subsequent number is found by adding the two numbers before it.
- An integer
n
.
- A sequence of numbers representing the Fibonacci sequence from 0 to
n
.
No. | Inputs | Outputs |
---|---|---|
1 | 50 | 1 1 2 3 5 8 13 21 34 |
2 | 100 | 1 1 2 3 5 8 13 21 34 55 89 |
3 | 200 | 1 1 2 3 5 8 13 21 34 55 89 144 |
The problem tests the ability to use loops and conditional statements to print a sequence of numbers.
Problem: Write a program to obtain the Fibonacci sequence number. The Fibonacci sequence is a series of numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, .... Each subsequent number is found by adding the two numbers before it.
- An integer
n
.
- An integer representing the
n
-th number in the Fibonacci sequence.
No. | Inputs | Outputs |
---|---|---|
1 | 9 | 34 |
2 | 3 | 2 |
3 | 5 | 5 |
The problem tests the ability to use loops and conditional statements to print a sequence of numbers.
Problem: Write a program to determine how many elements in a sequence are greater than the next element. The sequence consists of integers and ends with the number 0. The program should take into account that the sequence contains at least two numbers.
- A sequence of integers that ends with the number 0.
- An integer representing the number of elements in the sequence that are greater than the next element.
No. | Inputs | Outputs |
---|---|---|
1 | 2 9 1 4 3 0 |
2 |
2 | 3 1 2 3 4 0 |
0 |
3 | 5 1 2 3 4 5 0 |
1 |
The problem tests the ability to use loops and conditional statements to count the number of occurrences of a specific value.
Problem: Write a program to determine the value of the second largest element in a sequence. The sequence consists of different natural numbers and ends with the number 0. The program should take into account that the sequence contains at least two numbers.
- A sequence of integers that ends with the number 0.
- An integer representing the value of the second largest element in the sequence.
No. | Inputs | Outputs |
---|---|---|
1 | 1 4 3 2 0 |
3 |
2 | 3 1 2 3 4 0 |
3 |
3 | 5 1 2 3 4 5 0 |
4 |
The problem tests the ability to use loops and conditional statements to calculate the value of the second largest element in a sequence.
Problem: Write a program to determine the index of the largest element in a sequence. The sequence consists of natural numbers and ends with the number 0
. The program should take into account that the sequence contains at least two numbers.
- A sequence of integers that ends with the number
0
.
- An integer representing the index of the largest element in the sequence.
No. | Inputs | Outputs |
---|---|---|
1 | 4 2 6 9 5 0 |
3 |
2 | 3 1 2 3 4 0 |
4 |
3 | 5 1 2 3 4 5 0 |
4 |
The problem tests the ability to use loops and conditional statements to calculate the index of the largest element in a sequence.
Problem: Write a program to determine how many times the sign changes in a sequence. The sequence consists of non-zero integers and ends with the number 0
. The program should take into account that the sequence contains at least two numbers.
- A sequence of integers that ends with the number
0
.
- An integer representing the number of times the sign changes in the sequence.
No. | Inputs | Outputs |
---|---|---|
1 | -5 -3 10 6 -4 7 -1 0 |
4 |
2 | 3 1 2 3 4 0 |
0 |
3 | 5 1 2 3 4 5 0 |
0 |
The problem tests the ability to use loops and conditional statements to count the number of occurrences of a specific value.
Problem: Write a program to determine the position of a given digit d
in a natural number n
, counting from the end of the number. If there is no such digit, the answer should be the number 0
; if there are several such digits in the number, the position of the rightmost one should be determined.
- A natural number
n
. - A digit
d
.
- An integer representing the position of the rightmost occurrence of the digit
d
in the numbern
.
No. | Inputs | Outputs |
---|---|---|
1 | 1233572 3 |
4 |
2 | 123456789 5 |
5 |
3 | 123456789 0 |
0 |
The problem tests the ability to use loops and conditional statements to calculate the position of a digit in a number.
Problem: Write a program to determine the number of palindromes that do not exceed n
, where n
is an integer entered by the user.
- An integer
n
(1 β€ n β€ 100000).
- A sequence of numbers representing the palindromes that do not exceed
n
.
No. | Inputs | Outputs |
---|---|---|
1 | 50 | 1 2 3 4 5 6 7 8 9 11 22 33 44 |
2 | 100 | 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 |
3 | 200 | 1 2 3 4 5 6 7 8 9 11 22 33 44 55 66 77 88 99 101 111 121 131 141 151 161 171 181 191 |
Problem: Write a program to determine the number of palindromes that do not exceed n
, where n
is an integer entered by the user.
- An integer
n
(1 β€ n β€ 100000).
- A number representing the number of palindromes that do not exceed
n
.
No. | Inputs | Outputs |
---|---|---|
1 | 50 | 13 |
2 | 100 | 18 |
3 | 200 | 28 |
The problem tests the ability to use loops and conditional statements to calculate the number of palindromes that do not exceed a certain value.
Problem: Two four-digit numbers a
and b
are entered. Print all four-digit numbers in the interval from a
to b
that are palindromes (read the same from left to right and from right to left).
- Two four-digit numbers
a
andb
.
- A sequence of four-digit numbers in the interval from
a
tob
that are palindromes.
No. | Inputs | Outputs |
---|---|---|
1 | 1400 2200 |
1441 1551 1661 1771 1881 1991 2002 2112 |
2 | 1000 2000 |
1001 1111 1221 1331 1441 1551 1661 1771 1881 1991 |
3 | 2000 3000 |
2002 2112 2222 2332 2442 2552 2662 2772 2882 2992 |
The problem tests the ability to use loops and conditional statements to calculate the number of palindromes that do not exceed a certain value.
Problem: Write a program to determine the number of palindromes that do not exceed n
, where n
is an integer entered by the user.
- An integer
n
(1 β€ n β€ 100000).
- A number representing the number of palindromes that do not exceed
n
.
No. | Inputs | Outputs |
---|---|---|
1 | 10 | 9 |
2 | 50 | 13 |
3 | 100 | 18 |
4 | 200 | 27 |
The problem tests the ability to use loops and conditional statements to calculate the number of palindromes that do not exceed a certain value.
Problem: Two four-digit numbers a
and b
are entered. Print all four-digit numbers in the interval from a
to b
that contain only three identical digits.
- Two four-digit numbers
a
andb
.
- A sequence of four-digit numbers in the interval from
a
tob
that contain only three identical digits.
No. | Inputs | Outputs |
---|---|---|
1 | 1400 1600 |
1411 1444 1511 1555 |
2 | 1000 2000 |
1111 1222 1333 1444 1555 1666 1777 1888 1999 |
3 | 2000 3000 |
2002 2111 2222 2333 2444 2555 2666 2777 2888 2999 |
Problem: Two numbers a
and b
are entered. Print all odd numbers from the interval from a
to b
(b β€ a). Write a program without using a branching instruction.
- Two integers
a
andb
(a β₯ b).
- A sequence of odd numbers from the interval from
a
tob
.
No. | Inputs | Outputs |
---|---|---|
1 | 10 1 |
9 7 5 3 1 |
2 | 20 1 |
19 17 15 13 11 9 7 5 3 1 |
3 | 30 1 |
29 27 25 23 21 19 17 15 13 11 9 7 5 3 1 |
The problem tests the ability to use loops to print a sequence of numbers.
Problem: Two numbers a
and b
are entered. Print all even numbers from the interval from a
to b
(a β€ b). Write a program without using a branching instruction.
- Two integers
a
andb
(a β€ b).
- A sequence of even numbers from the interval from
a
tob
.
No. | Inputs | Outputs |
---|---|---|
1 | 1 20 |
2 4 6 8 10 12 14 16 18 20 |
2 | 1 30 |
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 |
3 | 1 40 |
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 |
The problem tests the ability to use loops to print a sequence of numbers.
The exercises provided range from understanding conditional statements to implementing complex branching logic. Here's a summary of the exercises ordered by difficulty, starting with the easiest and moving to more challenging problems.
Total: 14
Total: 19
Total: 18
Each exercise is designed to challenge different aspects of problem-solving, from simple arithmetic to complex logical reasoning. The estimated completion time for the exercises ranges from 5 minutes for the simplest tasks to 25 minutes for the most complex ones. This gives an average expected time of approximately 10-15 minutes per exercise, depending on the student's prior knowledge and experience.
GitHub Classroom's autograder provides immediate feedback on your exercises. Each exercise is worth points based on its complexity, with a total of 90
points available across all exercises.
When you commit and push your solution, GitHub Actions will run tests on your code. If all tests for an exercise pass, you will be awarded the full points for that exercise. If some tests fail, partial points may be awarded.
The workflow will run all tests, even if some fail. The total score will be shown in the workflow output as points scored/total points available
. For example, if you see 10/90
, it means you've successfully completed the first problem worth 10
points.
Please note that the workflow will show as failed if any test fails, even if you've successfully solved some problems. Look for the points tally in the output to see your score.
Remember, practice makes perfect! If you don't pass all tests the first time, review your code, make improvements, and try again. You can push new changes as many times as you need.
To run your code and tests locally, you can use the following commands:
- Run the code:
python exercises/exercise_1.py
- Run the tests:
To run the tests for an exercise, you'll use the pytest framework. First, make sure you have pytest installed:
pip install pytest
Then, you can run the tests for a specific exercise using the following command:
python -m unittest tests/test_exercise_1.py
And for more beautiful output, you can use:
pytest --color=yes -vv tests/test_exercise_1.py
or to run only failed test from a specific file:
pytest --color=yes --failed-first -x tests/test_exercise_1.py
Replace exercise_1
with the exercise you're working on (e.g., exercise_2
, exercise_3
, etc.) and test_exercise_1
with the corresponding test file.