Python Algorithms for Basic Programming Exercises
Python Algorithms for Basic Programming Exercises
Series No. 1
Exercise No. 1:
Write an algorithm and its translation in Python that allows entering two integers a and b (formed by two
to display a four-digit integer r by interleaving a between the two digits of b.
Example: a=54 and b=36 so r=3546
Algorithm exercise1
Start
TDO
Display ("Enter a two-digit integer:")
Read (n1)
Subject Type
Display ("Enter another two-digit integer:")
Read (n2) n1 Enter
a n1 // 10 n2 Enter
b n1 % 10 a Enter
n a * 1000 + n2 * 10 + b b Enter
Display ("the final result = ", n)
End
n1 = int(input("Enter a two-digit integer:"))
n2 = int(input("Enter another two-digit integer:"))
a = n1 // 10
b = n1 % 10
n = a * 1000 + n2 * 10 + b
print('four-digit integer = ', n)
Exercise No. 2:
For each of the propositions below, check the correct answer:
The correct syntax is:
Exercise No. 3:
Write a program that allows you to enter a number and then determine whether it belongs to an interval.
given, knowing that the endpoints of the interval are fixed by the user.
Solution :
a = int(input("enter [a :"))
b = int(input("enter b]:"))
val = int(input("enter a value:"))
if a <= val <= b:
print(val, "belongs to the interval [", a, ", ", b, "]")
else:
print(val, "does not belong to the interval [", a, ", ", b, "]")
Exercise No. 4:
Write a program that asks the user for two numbers and then informs them if their product is
negative or positive. However, be careful not to calculate the product of the two numbers.
Solution :
a = int(input("enter a : "))
b = int(input("enter b: "))
if a > 0 and b > 0 or a < 0 and b < 0:
the product is positive
else:
the product is negative
Exercise No. 5:
Write a program that calculates the amount of overtime for an employee.
knowing the unit price of an hour, according to the following scale:
The first 39 hours without extra charge,
From the 40th to the 44th hour are increased by 50%,
From the 45th to the 49th hour are increased by 75%.
From the 50th hour or more, they are increased by 100%.
Solution :
nbheures = int(input("saisir le nombre d heures"))
price = float(input("enter the unit price of one hour"))
montant = 0
if nbheures <= 39:
montant = 0
elif hours < 45:
amount = (number of hours - 39) * (price * 1.5)
elif hours < 50:
montant = (5*prix*1.5)+(nbheures-44)*(prix*1.75)
else:
montant = (5*prix*1.5)+(5*prix*1.75)+(nbheures-49)*(prix*2)
the amount of overtime is:
Exercise No. 6:
Write a program that will read the hour and minutes from the keyboard, and it will display what time it will be.
a minute later.
For example, if the user types 21 then 32, the program must respond: "In one minute, it will be 21"
hour(s) 33".
Note: It is assumed that the user enters a valid time. Therefore, there is no need to check it.
Solution :
hour = int(input("enter the hour: "))
minutes = int(input("enter the minutes: "))
if hour < 23 :
if minutes == 59:
hour += 1
minutes=0
else :
minutes+=1
else :
if minutes==59 :
heure=0
minutes=0
else:
minutes+=1
after one minute the time is
Exercise No. 7:
Write a program that displays the corresponding mention based on a grade?
Solution :
note=float(input("enter a grade: "))
if note < 10:
not admitted
elif note < 12 :
passable
elif note<14 :
pretty good
elif note<16 :
good
elif note <= 20:
very well
else :
enter a valid grade
Exercise No. 8:
Write an algorithm that asks the user for a child's age. Then, it informs them of their
category :
Chicken from 6 to 7 years old
"Pupil" from 8 to 9 years old
"Minime" from 10 to 11 years old
'Cadet' after 12 years
Solution :
Solution 1
Algorithm child_category :
Start
Display ("Enter the age: ");
Read(age);
IF age < 6 THEN
Display ("Enter an age greater than or equal to 6")
IF age <=7 THEN
Display ("Chick")
IF age <= 9 THEN
Display ('Pupil')
ELSE IF age <= 11 THEN
Display ("Minimum")
SINON
Display ("Cadet")
FIN IF
End
Solution 2
Algorithm category_child :
Start
Display("Enter the age: ");
Read(age);
IF age < 6 THEN
Display ("Enter an age greater than or equal to 6")
IF age >= 6 AND age <= 7 THEN
Display ("Chick")
IF age >=8 AND age <=9 THEN
Show (Pupil)
IF age >=10 AND age <=11 THEN
Display ("Minimal")
ALTERNATIVELY
Display ("Cadet")
FIN YES
End
Exercise No. 9:
Write an algorithm that, given a number between 1 and 7, displays the corresponding day?
Solution :
Algorithm week_day :
Start
Display ("Enter a day:");
Read(nb) ;
IF day=1 THEN
Display ("Monday")
ELSE IF day=2 THEN
Display ("Tuesday")
IF day=3 THEN
Display ("Wednesday")
IF day=4 THEN
Display ("Thursday")
IF day=5 THEN
Display ("Friday")
IF day=6 THEN
Show ("Saturday")
SINON SI jour=7 ALORS
Display ("Sunday")
IF NOT
Display ("invalid day")
FINE IF
End
Solution 2:
Algorithm week_days:
Start
Display ('Enter a value: ');
Read (day);
ACCORDING to the day
1: Display ("Monday")
2 : Display ("Tuesday")
3 : Display ("Wednesday")
4: Display ("Thursday")
5 : Show ("Friday")
6 : Display ("Saturday")
7: Show ("Dianche")
ELSE : Display ("Invalid day")
End ACCORDING TO
End
if M1 > M2:
the best grade is:
else:
the best grade is : M2