About "strange numbers" in the code
Sometimes in your programs you may need to use some fixed numbers arising from the meaning of the task. For example, if you are solving a problem about the clock, then most likely you will have numbers like 60 (minutes in hours) or 24 (hours in days) in the program. Then you directly specify this number in the program, as, for example, in the task "Electronic Clock - 1" from level 1A:
b = a % 60 # python b := a mod 60; // pascal
This is absolutely normal. (If you are going to write serious large programs later, then you may be required to create special variables-"constants" for these numbers, but that's not what we're talking about now, and it's not necessary in our tasks.)
But it happens that in the program you need some number that you don't know right off the bat, which you need to calculate somehow. For example, you need to know in the program by which angle (how many degrees) the minute hand shifts in one minute. It is unlikely that you will call this number right out of your head. But it is clear how to count it: it is necessary to divide the full angle (360 degrees) by 12 hours and divide what happens by 60 minutes.
What are you doing in this case? You take a calculator and use it to calculate how much 360/12/60 will be
. You get 0.5 and just leave this number (0.5) right in the program code:
b = a * 0.5 # python b := a * 0.5; // pascal
That's the wrong approach.
Any programming language works great as a calculator. Therefore, it is not necessary to use an external calculator. Just write the entire expression in the program directly:
b = a * 360 / 12 / 60 # python b := a * 360 / 12 / 60 ; // pascal
That's so much better. Why?
- Firstly, it's easier this way. You don't have to go into the calculator, count something there, and enter the result number into the program. You immediately enter the formula here, and the computer does the work for you.
- Secondly, it is more reliable this way. You may accidentally enter the wrong number into the calculator, or make a mistake when entering the result into the program. A common situation is if the result is obtained in the form of a long decimal fraction, then you can manually enter too few characters. And the computer will count for you as accurately as it can.
- Finally, it's clearer this way. If you return to your program in a week, you are unlikely to immediately remember what 0.5 is and where you got it from. And if you see the formula
360/12/60
, everything will immediately become clearer, because 360, 12 and 60 are quite understandable numbers in this task, and most likely you will immediately remember why and by what you divided. Similarly, the program will be clearer for other people who will watch it. Finally, if you actually have an error in the formula (as the simplest example, you divided by 24 instead of 12), then it is much easier to notice it than if you had only the result in the program.
Similar considerations apply to other situations. For example, you may need the A character code in the program. You can look at the symbol table and see that it is 65. Or you can write ord('A')
. Here is the second option much better than the first — let the computer think for you, the advantages are the same as described above. If you need to know how the codes of capital letters differ from small ones — also write a formula. Etc.
In general, the general rule is: