04.PB Python For Loop Exercise
04.PB Python For Loop Exercise
Problems for exercise and homework for the course "Programming Basics" course @ SoftUni Global
Submit your solutions to the SoftUni Judge system: https://judge.softuni.org/Contests/4584
1. Numbers Ending in 7
Write a program that prints the numbers in the range 1 to 1000 that end with 7
Input Output
7
17
(no input) 27
…
997
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 1 of 9
12
1
4 Yes 1 + 2 + 3 = 12
6 Sum = 6
1
2
3
3 No |10 - (1 + 1)| = 8
1 Diff = 8
1
10
3 No |5 - (5 + 1)| = 1
5 Diff = 1
5
1
3 No
1 Diff = 1
1
1
2. Read the count of numbers to be entered on the console - n, and make a for loop from 0 to n, reading a
number num at each iteration:
3. Check if the read number is greater than "max_num". If it is greater, equate the value of "max_num" to its
value. Then add the value of the read number to "sum_numbers":
4. After the loop, check if "max_num" is equal to the sum of all numbers from which "max_num" was
subtracted:
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 2 of 9
Testing in the Judge System
Test your solution in the online Judge system https://judge.softuni.org/Contests/Compete/Index/4584#1
Input Data
The first line of the input is the integer n (1 ≤ n ≤ 1000) - the count of numbers. On the next n lines, there is one
integer in the interval [1...1000] - the numbers on which the histogram is to be calculated.
Output Data
Print the histogram on the console in 5 lines, each containing a number between 0% and 100%, formatted to two
digits after the decimal point, e.g. 25.00%, 66.67%, 57.14%.
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 3 of 9
599 333 920
799 555 12
111 7
9 150
250
680
2
600
200
4. Clever Lily
Lily is now N years old. For every birthday she receives a present.
For the odd birthdays (1, 3, 5...n) she receives toys.
For the even birthdays (2, 4, 6...n) she receives money.
For the second birthday, she receives 10.00 USD, and the amount increases by 10.00 USD for each following even
birthday (2 -> 10, 4 -> 20, 6 -> 30...etc.). Over the years, Lily has been secretly saving this money. Lily's brother, in
the years that she receives money, takes 1.00 USD of it. Lily sold the toys, she received over the years, for P USD
each and added the amount to the saved money. With the money, she wanted to buy a washing machine for X
USD. Write a program to calculate how much money she has collected and whether she has enough to buy a
washing machine.
Input Data
The program reads 3 numbers entered by the user on separate lines:
Lily's age – an integer in the range [1...77]
Price of the washing machine – a floating-point number in the range [1.00...10 000.00]
Unit price of a toy – an integer in the range [0...40]
Output Data
Print on the console on a single line:
If Lily's money is enough:
o "Yes! {N}" - where N is the remaining money after the purchase
If the money is not enough:
o "No! {М}" - where M is the needed amount
The numbers N and M must be formatted to 2 digits after the decimal point.
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 4 of 9
On her fifth birthday, she receives a toy
On her sixth birthday, she receives money (30 USD)
On her seventh birthday, she receives a toy
On her eighth birthday, she receives money (40 USD)
On her ninth birthday, she receives a toy
On her tenth birthday, she receives money (50 USD)
Savings: 10 + 20 + 30 + 40 + 50 = 150 USD
She sold 5 toys * 6 USD = 30 USD
Her brother took 5 times * 1 USD = 5 USD
Total saved money: (150 + 30) – 5 = 175 USD
175 >= 170 (the price of the washing machine)
=> she managed to buy a washing machine
Left: 175 - 170 = 5 USD
21 No! Savings: 550 USD
1570.98 997.98
She sold 11 toys * 3 USD = 33 USD
3
Her brother took 10 times * 1 USD = 10 USD
Total saved money: (550 + 33) – 10 = 573 USD
573 < 1570.98 – she didn't manage to buy a washing machine
Needed money: 1570.98 – 573 = 997.98 USD
5. Salary
A company boss notices that more and more employees are spending time on sites that distract them.
To prevent this, he introduces surprise checks on his employees' opened browser tabs.
According to the opened tab site, the following fines are applied:
"Facebook" -> 150 USD
"Instagram" -> 100 USD
"Reddit" -> 50 USD
Two lines are read from the console:
The number of tabs opened in the browser n – an integer in the range [1...10]
Salary – an integer in the range [500...1500]
Then n - number of times the website name is read – string.
Input Data
If during the check the salary becomes less than or equal to 0 USD, the console displays "You have lost
your salary." and the program ends.
Otherwise, after the check, the console displays the remainder of the salary (to be displayed as an integer).
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 5 of 9
Stackoverflow.com For the second tab -> Stackoverflow.com -> no fine
Instagram For the third tab -> Instagram -> fine 100 USD (salary: 600 - 100 = 500)
Facebook For the fourth tab -> Facebook -> fine 150 USD (salary: 500 - 150 = 350)
Reddit For the fifth tab -> Reddit -> fine 50 USD (salary: 350 - 50 = 300)
Facebook For the sixth tab -> Facebook -> fine 150 USD (salary: 300 - 150 = 150)
Facebook For the seventh tab -> Facebook -> fine 150 USD (salary: 150 - 150 = 0)
Salary equals 0 => prints the corresponding output and the program
ends.
Input Output Input Output
3 500 3 350
500 500
Github.com Facebook
Stackoverflow.com Stackoverflow.com
softuni.bg softuni.bg
6. Oscars
You have been invited by the academy to write software that calculates the points for an actor/actress. The
academy will give you initial points for the actor. Each assessor will then give their score. The points an actor gets
are formed by: the length of the assessor's name multiplied by the points they give divided by two.
If the score at any point exceeds 1250.5 the program must break and print that the given actor has received a
nomination.
Input Data
• Actor's name - string
• Academy points – a floating-point number in the range [2.0... 450.5]
• Number of assessors n – an integer in the range [1… 20]
On the next n number of lines:
o Assessor's name - string
o Assessor's score – a floating-point number in the range [1.0... 50.0]
Output Data
Print on the console on a single line:
If the points are above 1250.5:
"Congratulations, {actor's name} got a nominee for leading role with {points}!"
If the points are not enough:
"Sorry, {actor's name} you need {needed points} more!"
Format the result to the first digit after the decimal point!
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 6 of 9
Johnny Depp => 205 + ((11 * 45) / 2) = 452.5
45 Second is Will Smith
Will Smith => 452.5 + ((10 * 29) / 2) = 597.5
29 Third is Jet Lee
Jet Lee => 597.5 + ((7 *10) / 2) = 632.5
10 Fourth is Matthew McConaughey
Matthew => 632.5 + ((19 * 39) / 2) = 1003.0
McConaughey
1003.0 < 1250.5
39
=> The points are not enough.
Needed points: 1250.5 - 1003.0 = 247.5
Sandra Bullock Congratulations,
340 Sandra Bullock got a
5 nominee for leading
role with 1268.5!
Robert De Niro
50
Julia Roberts
40.5
Daniel Day-Lewis
39.4
Nicolas Cage
29.9
Sophia Loren
33
7. Trekking Mania
Climbers from all over the world gather in groups and mark the next peaks to climb. Depending on the size of the
group, climbers will climb different peaks.
Group up to 5 people - climb Makalu
Group of 6 to 12 people - climb Mont Blanc
Group of 13 to 25 people - climb Kilimanjaro
Group of 26 to 40 people - climb K2
Group of 41 or more people - climb Everest
Write a function that calculates the percentage of climbers climbing each peak.
Input Data
A series of numbers are read from the console, each on a separate line:
On the first line – the number of groups of climbers – an integer in the range [1...1000]
For each group on a separate line – the number of people in the group - an integer in the range [1...1000]
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 7 of 9
Output Data
Print 5 lines on the console, each containing a percentage between 0.00% and 100.00% formatted to two digits
after the decimal point:
First line - the percentage of people climbing Makalu
Second line - the percentage of people climbing Mont Blanc
Third line - the percentage of people climbing Kilimanjaro
Fourth line - the percentage of people climbing K2
Fifth line - the percentage of people climbing Everest
8. Tennis Ranklist
Rafael Nadal is a tennis player whose next goal is to climb the world rankings in men's tennis.
During the year, Nadal participates in a certain number of tournaments, and for each tournament, he receives
points that depend on the position he finished in the tournament. There are three options for finishing a
tournament:
W - if he is the winner, he receives 2000 points
F - if he is a finalist, he receives 1200 points
SF - if he is a semi-finalist, he receives 720 points
Write a program that calculates how many points Rafael Nadal will have after playing all the tournaments,
knowing how many points he started the season with. Also, calculate how many points he earns on average from
all tournaments played and what percentage of tournaments he has won.
Input Data
Two lines are first read from the console:
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 8 of 9
Number of tournaments he has participated in – an integer in the range [1…20]
Initial number of points in the ranking – an integer in the range [1...4000]
A separate line is read for each tournament:
Tournament stage reached – string – "W", "F" or "SF"
Output Data
Print three lines in the following format:
"Final points: {number of points after the tournaments are played}"
"Average points: {average points earned per tournament}"
"{percentage of won tournaments}%"
Average points should be rounded down to the nearest integer number and percentages to be formatted to the
second digit after the decimal point.
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Follow us: Page 9 of 9