M
M
Michael Sne Bjorn Palagin2016-04-24 04:34:05
Python
Michael Sne Bjorn Palagin, 2016-04-24 04:34:05

Task in Python, please help me understand the formula, how is it implemented?

Good day to all. Dear coders.
I am currently taking elementary courses in Python and there was such a problem:

At the Institute of Bioinformatics, a robot moves around the office. Recently, students from a group of programmers wrote a program for him, according to which the robot, when it enters a room, counts the number of programmers in it and says it out loud: "n programmers."
In order for this to sound correct, for each n you need to use the correct word ending.
Write a program that reads an integer n (non-negative) from user input, outputs this number to the console along with the correct word "programmer", so that the robot can communicate normally with people, for example: 1 programmer, 2 programmers, 5 programmers .
There can be a lot of programmers in a room. Check that your program will correctly handle all cases up to at least 1000 people.
Additional comment on the condition:
Please note that the task is not as simple as it seems at first glance. If your solution fails some test, it means that you didn't consider some of the input cases (number of programmers 0 ≤ n ≤ 1000). Be sure to check your solutions on additional values, and not just on those given in the condition of the assignment.

I sketched out the code, everything seemed to work, but did not check for the numbers 21, 31, 41, etc.
That is, when I entered 21, my code gave out instead of 21 programmers, 21 programmers.
It was the same with the number 994, for example, my code gave out 994 programmers, instead of 994 programmers.
Everything else worked. One course participant wrote me an answer, but does not want to explain how he derived the formula for the correct calculation.
Here is the code:
tp = int(input('Введите число: '))

if tp < 0:
    print('Ошибка, введите положительное число')
elif tp % 10 == 1 and tp % 100 != 11: # Формула 1
    print(tp, 'программист')
elif tp % 10 >= 2 and tp % 10 <= 4 and (tp % 100 < 10 or tp % 100 > 20): # Формула 2
    print(tp, 'программиста')
else:
    print(tp, 'программистов')

I understood the formula number 1, that is, if the remainder of the number n is 1 and the remainder is not equal to 11, then this is a programmer.
In the second formula, I only understood that if the remainder n is greater than or equal to 2 and less than or equal to 4, then this is a programmer, but I can’t understand the second part of the formula.
If it is not difficult for someone to give 5-10 minutes of time, please give a clear explanation.
Many thanks in advance for your replies.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Skusnov, 2016-04-24
@ven000mus

Numbers ending in 2, 3, 4, for example, 2, 22, 32, fall into formula 2 ("programmer").
However, if the last two digits are numbers 12-14, then they must be excluded: 12, 112, 212. .. (this is formula 3). Therefore, the remainder of the division by 100 is taken.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question