📜 ⬆️ ⬇️

The task of the snail

image In this topic, I offer 3 fairly basic tasks for ingenuity. For novice programmers (probably for very beginners, a la school, because for a real programmer is too trite). Or, perhaps, for an interview, but to check specifically one narrow aspect: how much a person can make decisions at all independently, and not to bother and correct them.

Base


As a base, a fairly well-known snail problem for junior school was chosen. “A snail crawls up a 5-meter high pole. Every day she crawls up to 3 meters, and every night she slides down 2 meters. For how many days it crawls to the top of the pillar. "

As you know, many schoolchildren are cut off by the fact that they simply divide 5 by (3–2) and get 5 days, not taking into account that at the end of the third day the snail has already reached the top.

For pedants
The whole process begins strictly at the beginning of the light part of the day, when the snail can crawl up (and not in the middle of the day). On top of the pillar is a horizontal platform, reaching which the snail no longer slides down. The time spent on moving from the vertical part of the surface of the column to the final horizontal is considered to be zero.

Task 1


Real non-negative numbers are entered: how many creeps up in a day; how much slides down for the night; post height. The answer should be displayed - the minimum necessary integer number of days.
')
Classic solution
  1. If the height of the column is 0, return 0.
  2. If the daily mileage is 0, we return infinity / never².
  3. Calculate the difference between the height of the pillar and the daily run ( ); if it is non-positive, we return 1.
  4. Calculate the mileage for the whole day ( ); if it is nonpositive, then we return infinity / never².
  5. Return 1 + ceil( / ) .

Âą By valid I meant floating point. You can take the whole. The bottom line is that, in general, the difference between the height of the pillar and the daily run may not be a multiple of the run for the whole day.
² These options, so be it, can be forgiven (or set in the condition that this will not happen).

The bottom line is that the decisive handles at least: (1) the classic case (the vertex is not reached on the first day, but exactly at the end of one of the days); (2) non-multiple length (the top is reached not on the first day and not exactly at the end of a certain day); (3) short length (the top is reached before the end of the first day).

Task 2


Same. Only it is necessary to return not the minimum necessary integer number of days, but “time” relative to the length of the day (possibly a non-integer number). It is assumed that every day and night lasts exactly 0.5 (and the process begins exactly at the beginning of the day). And the snail moves at a constant speed both during the day and at night.

Task 3


Same. Only the process starts 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.

Inspired by
Clear task in physics, which once showed a stunning teacher at the FDP.

Task 1
Vasya with a force of 100 H pulls the dynamometer handle, the second handle of which is bound to a fixed wall with an inextensible rope.
Question: What force will the dynamometer show?

Task 2
Vasya and Fedya, each with a force of 100 H, are pulled by the opposite handles of the dynamometer.
Question: What force will the dynamometer show?

Task 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?

Hidden text
It seems to be trite, but many, even students, are already cut off at the second task. Especially if in that order (the first is success, the second is fail, the third is o_O). And an understanding of these tasks allows for a deeper understanding of the basic principles that a student could click through in all N years of school.

Source: https://habr.com/ru/post/311680/


All Articles