Back to Home

The snail challenge

snail · school · interview · algorithms

The snail challenge

imageIn 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.

For pedants
The whole process begins strictly at the beginning of the daylight, when the snail can creep up (and not in the middle of the day). At the top of the column is a horizontal platform, upon reaching which the snail no longer moves down. We consider the time costs for moving from the vertical part of the column surface to the final horizontal to be zero.

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.

Classic solution
  1. If the column height is 0, return 0.
  2. If the daily mileage is 0, return infinity / never².
  3. We calculate the difference in the height of the column and the daily run ( РВСДП); if it is non-positive, return 1.
  4. We calculate the mileage for the whole day ( ПЗЦС); if it is not positive - return infinity / never².
  5. 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.

Inspired by
The tangible physics problems that an amazing teacher at the FDP once showed.

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?

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

Read Next