The three rules of Ruby Q.:
-
Please do not post any solutions or spoiler discussion for this quiz
until
48 hours have passed from the time on this message. -
Support Ruby Q. by submitting ideas as often as you can:
- Enjoy!
Suggestion: A [QUIZ] in the subject of emails about the problem helps
everyone
on Ruby T. follow the discussion. Please reply to the original quiz
message,
if you can.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
by Matthew M.
Microwave ovens have had a significant impact on how we cook today. One
report
from 1997 indicated that 90% of US households owned one. Assuming the
promise of
faster cooking times, that’s a lot of time saved.
But I imagine there are microwave users out there who know the trick to
saving
even more time. Knowing that many microwave ovens recognize 90 seconds
as the
same as 1 minute 30 seconds, finger-travel distance is saved. (Yes, it’s
rather
insignificant, but don’t tell them… us… whatever.)
Your task is to write a function in Ruby that determines the optimal
pattern of
buttons to hit based on this example button pad (where * is “Cook”):
±–±–±–+
| 1 | 2 | 3 |
±–±–±–+
| 4 | 5 | 6 |
±–±–±–+
| 7 | 8 | 9 |
±–±–±–+
| 0 | * |
±–±–+
Your function should accept an integral time value representing desired
seconds
and should output an integer that indicates the buttons to press on the
microwave’s input pad. The metric for determining what input is more
efficient
is distance (not number of buttons hit). Distance to the Cook button
must be
included in your efficiency calculation. For simplicity in distance
calculations, you may consider the shape of each button to be square.
Examples:
99 seconds is 1:39, but 99 is less movement than 139
microwave(99) => 99
71 seconds is only two keys, but entering 111 is far less movement.
microwave(71) => 111
120 seconds is 2 minutes, and 200 is slightly less movement than 120
microwave(120) => 200
123 seconds is 2:03, but 203 is a lot more distance
microwave(123) => 123
Once you’ve done the basic version, try modifying your code enough to
handle
these:
-
We often don’t care to be exact. 99 seconds, for example, is
basically the
same as 95 seconds, but more efficient to enter. Modify your function to
accept
a tolerance in seconds, and return answers that are within that
tolerance of the
desired time. Try ±5 and ±10 seconds. -
Try changing the efficiency metric, to something like number of
buttons
pressed, or Manhattan distance. -
Try changing the button dimensions… For example, what happens if
each
button is twice as wide as it is high?