The snail challenge
In this topic, I propose 3 fairly basic tasks for quick wits. For beginners (probably for beginners, a la school, because for a real programmer it’s too commonplace). Or, perhaps, for an interview, but to verify specifically one narrow aspect: how much a person can generally make decisions on his own, and not distort and tweak for himself.Base
As a base, a rather well-known snail task for the elementary grades of the school was chosen. “The snail creeps up a 5-meter-high column. Every day she creeps up 3 meters, and every night she moves down 2 meters. In how many days it will crawl to the top of the pillar. ”
As you know, many students cut off by simply dividing 5 by (3−2) and getting 5 days, not taking into account the fact that at the end of the third day the snail has already reached the top.
Task 1
Real¹ non-negative numbers are introduced: how many creeps up a day; how much it goes down during the night; column height. The answer should be displayed - the minimum required integer day.
- If the column height is 0, return 0.
- If the daily mileage is 0, return infinity / never².
- We calculate the difference in the height of the column and the daily run (
РВСДП); if it is non-positive, return 1. - We calculate the mileage for the whole day (
ПЗЦС); if it is not positive - return infinity / never². - We return
1 + ceil(РВСДП / ПЗЦС).
¹ By valid, I meant floating point. You can take whole. The bottom line is that in the general case, the difference in the height of the column and the daily mileage may not be a multiple of the mileage for the whole day.
² These options, so be it, can be forgiven (or set on the condition that this will not happen).
The bottom line is that the decider should handle at least: (1) the classical case (the peak is reached not on the first day, but exactly at the end of one of the days); (2) multiple lengths (the peak is not reached on the first day and not exactly at the end of a day); (3) short length (the peak is reached before the end of the first day).
Task 2
Same. But it is already necessary to return not the minimum required whole number of days, but the “time” relative to the length of the day (possibly an integer). It is assumed that every day and night lasts exactly 0.5 (and the process starts exactly at the beginning of the day). And the snail moves day and night at a constant speed.
Task 3
Same. Only the process begins at an arbitrary time, and not exactly at the beginning of the day. The user enters an arbitrary real number - the "time" of the beginning of the process. The answer is an arbitrary real number - the “time” of the end of the process.
Problem 1
Vasya pulls with a force of 100 H the handle of a dynamometer , the second handle of which is tied with an inextensible rope to a fixed wall.
Question What power will the dynamometer show
Problem 2
Vasya and Fedya, each with a force of 100 H, are pulled by the opposite handles of the dynamometer.
Question What power will the dynamometer show
Problem 3
Vasya pulls the dynamometer handle with a force of 80 N, and Fedya, being weaker, pulls the opposite handle with a force of 70 N.
Question: What force will the dynamometer show?