Velocity Reviews > Ruby > [QUIZ] Micrrowave Numbers (#118)

# [QUIZ] Micrrowave Numbers (#118)

Ruby Quiz
Guest
Posts: n/a

 03-30-2007
The three rules of Ruby Quiz:

1. Please do not post any solutions or spoiler discussion for this quiz until
48 hours have passed from the time on this message.

2. Support Ruby Quiz by submitting ideas as often as you can:

http://www.rubyquiz.com/

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
on Ruby Talk follow the discussion. Please reply to the original quiz message,
if you can.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

by Matthew Moss

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:

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

2. Try changing the efficiency metric, to something like number of buttons
pressed, or Manhattan distance.

3. Try changing the button dimensions... For example, what happens if each
button is twice as wide as it is high?

Karl von Laudermann
Guest
Posts: n/a

 03-30-2007
On Mar 30, 7:55 am, Ruby Quiz <(E-Mail Removed)> wrote:

> # 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

Unless I'm missing something (and I usually am), the problem with the
last two examples is that the microwave will interpret "120" and "123"
as 1:20 and 1:23 when typed in, not as a raw number of seconds. How
the microwave interprets the number depends solely on whether the last
two digits exceed 59. If they do, it's a number of seconds, otherwise
it's minutes and seconds.

Maybe there should be an additional requirement of the quiz: the
program will never suggest the number of seconds representation, even
when that's more efficient than the minutes and seconds
representation, if the the number of seconds representation will be
misinterpreted (i.e. the last two digits are 60 or greater).

Actually, I'm not even convinced that a microwave would interpret e.g.
"170" as 170 seconds, or 2:50. Perhaps 170 would be interpreted as one
minute and 70 seconds? In other words, after counting down for 70
seconds, the display would read "1:00", and then proceed to 59 instead
of 99, not remembering the 70 that it started with.

Karl von Laudermann
Guest
Posts: n/a

 03-30-2007
On Mar 30, 10:08 am, "Karl von Laudermann" <(E-Mail Removed)>
wrote:

> misinterpreted (i.e. the last two digits are 60 or greater).

Clearly, I meant "the last two digits *aren't* 60 or greater".

James Edward Gray II
Guest
Posts: n/a

 03-30-2007
On Mar 30, 2007, at 9:10 AM, Karl von Laudermann wrote:

> Actually, I'm not even convinced that a microwave would interpret e.g.
> "170" as 170 seconds, or 2:50. Perhaps 170 would be interpreted as one
> minute and 70 seconds? In other words, after counting down for 70
> seconds, the display would read "1:00", and then proceed to 59 instead
> of 99, not remembering the 70 that it started with.

Yes, I just tested mine and this is how it behaved (1 minute and 70
seconds).

James Edward Gray II

Matthew Moss
Guest
Posts: n/a

 03-30-2007
On 3/30/07, Brown, Warren <(E-Mail Removed)> wrote:
> James,
>
> > # 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

>
> How is the microwave supposed to know that "120" is 120 seconds but
> "200" is two minutes? My experience is that microwaves will interpret
> two-digit numbers as seconds and three-digit numbers as minutes and
> seconds. Therefore, "99" is 99 seconds, but "120" is one minute, twenty
> seconds. Am I missing something?

I think the confusion is that I specified input and output to both be
integers, but they don't really mean the same thing.

Input is always seconds... two minutes is "120", five minutes is
"600". Input is NOT the keys you would press.

Output is always the keys to press... It would have been clearer,
perhaps, if I requested the output as an array such as [1, 2, 0,
:cook], or even a string like "120*". When I proposed the problem, I
realized that the "*" (cook) button is always part of the output, so I
could have the function return an integer and the "*" would be
implicit. But this, I think, confused things.

So 120 (or "120*") as output means to push, in order, the buttons 1,
2, 0 and *. Which, for a microwave means 1:20, or 80 seconds.

Does this clear things up?

Matthew Moss
Guest
Posts: n/a

 03-30-2007
On 3/30/07, Brown, Warren <(E-Mail Removed)> wrote:
> James,
>
> > # 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

>
> How is the microwave supposed to know that "120" is 120 seconds but
> "200" is two minutes? My experience is that microwaves will interpret
> two-digit numbers as seconds and three-digit numbers as minutes and
> seconds. Therefore, "99" is 99 seconds, but "120" is one minute, twenty
> seconds. Am I missing something?

The input is always "desired seconds", so 200 as input means 3 mins,
20 secs, and 120 as input means 2 mins.

The output is always "buttons to press", so 200 as output always means
2:00, and 120 _as output_ means 1:20.

And the convention for this quiz (not well specified, sorry) is that
if the buttons I press on the microwave are 170, the microwave will
treat that as 1:70... it will first countdown 70 seconds to 1:00,
then 0:59, 0:58... etc.

Hans Fugal
Guest
Posts: n/a

 03-30-2007
My microwave will win in every case. It has a dial timer.

Ruby Quiz wrote:
> The three rules of Ruby Quiz:
>
> 1. Please do not post any solutions or spoiler discussion for this quiz until
> 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz by submitting ideas as often as you can:
>
> http://www.rubyquiz.com/
>
> 3. Enjoy!
>
> Suggestion: A [QUIZ] in the subject of emails about the problem helps everyone
> on Ruby Talk follow the discussion. Please reply to the original quiz message,
> if you can.
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
>
> by Matthew Moss
>
> 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:
>
> 1. 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.
>
> 2. Try changing the efficiency metric, to something like number of buttons
> pressed, or Manhattan distance.
>
> 3. Try changing the button dimensions... For example, what happens if each
> button is twice as wide as it is high?
>

Ken Bloom
Guest
Posts: n/a

 03-30-2007
On Fri, 30 Mar 2007 20:55:46 +0900, Ruby Quiz wrote:

> The three rules of Ruby Quiz:
>
> 1. Please do not post any solutions or spoiler discussion for this quiz
> until 48 hours have passed from the time on this message.
>
> 2. Support Ruby Quiz by submitting ideas as often as you can:
>
> http://www.rubyquiz.com/
>
> 3. Enjoy!
>
> Suggestion: A [QUIZ] in the subject of emails about the problem helps
> everyone on Ruby Talk follow the discussion. Please reply to the
> original quiz message, if you can.
>
> -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

=-=-=-=-=
>
> by Matthew Moss
>
> 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:
>
> 1. 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.
>
> 2. Try changing the efficiency metric, to something like number of
> buttons pressed, or Manhattan distance.
>
> 3. Try changing the button dimensions... For example, what happens if
> each button is twice as wide as it is high?

Someone want to verify my answers?

For euclidian distance:
0 (0:00): 0*
1 (0:01): 1*
2 (0:02): 2*
3 (0:03): 3*
4 (0:04): 4*
5 (0:05): 5*
6 (0:06): 6*
7 (0:07): 7*
8 (0:0: 8*
9 (0:09): 9*
10 (0:10): 10*
11 (0:11): 11*
12 (0:12): 12*
13 (0:13): 13*
14 (0:14): 14*
15 (0:15): 15*
16 (0:16): 16*
17 (0:17): 17*
18 (0:1: 18*
19 (0:19): 19*
20 (0:20): 20*
21 (0:21): 21*
22 (0:22): 22*
23 (0:23): 23*
24 (0:24): 24*
25 (0:25): 25*
26 (0:26): 26*
27 (0:27): 27*
28 (0:2: 28*
29 (0:29): 29*
30 (0:30): 30*
31 (0:31): 31*
32 (0:32): 32*
33 (0:33): 33*
34 (0:34): 34*
35 (0:35): 35*
36 (0:36): 36*
37 (0:37): 37*
38 (0:3: 38*
39 (0:39): 39*
40 (0:40): 40*
41 (0:41): 41*
42 (0:42): 42*
43 (0:43): 43*
44 (0:44): 44*
45 (0:45): 45*
46 (0:46): 46*
47 (0:47): 47*
48 (0:4: 48*
49 (0:49): 49*
50 (0:50): 50*
51 (0:51): 51*
52 (0:52): 52*
53 (0:53): 53*
54 (0:54): 54*
55 (0:55): 55*
56 (0:56): 56*
57 (0:57): 57*
58 (0:5: 58*
59 (0:59): 59*
60 (1:00): 60*
61 (1:01): 61*
62 (1:02): 62*
63 (1:03): 63*
64 (1:04): 64*
65 (1:05): 65*
66 (1:06): 66*
67 (1:07): 67*
68 (1:0: 68*
69 (1:09): 69*
70 (1:10): 70*
71 (1:11): 111*
72 (1:12): 112*
73 (1:13): 113*
74 (1:14): 114*
75 (1:15): 115*
76 (1:16): 116*
77 (1:17): 77*
78 (1:1: 78*
79 (1:19): 79*
80 (1:20): 80*
81 (1:21): 121*
82 (1:22): 122*
83 (1:23): 123*
84 (1:24): 84*
85 (1:25): 85*
86 (1:26): 86*
87 (1:27): 87*
88 (1:2: 88*
89 (1:29): 89*
90 (1:30): 90*
91 (1:31): 91*
92 (1:32): 92*
93 (1:33): 133*
94 (1:34): 94*
95 (1:35): 95*
96 (1:36): 96*
97 (1:37): 97*
98 (1:3: 98*
99 (1:39): 99*

For Manhattan Distance:
0 (0:00): 0*
1 (0:01): 1*
2 (0:02): 2*
3 (0:03): 3*
4 (0:04): 4*
5 (0:05): 5*
6 (0:06): 6*
7 (0:07): 7*
8 (0:0: 8*
9 (0:09): 9*
10 (0:10): 10*
11 (0:11): 11*
12 (0:12): 12*
13 (0:13): 13*
14 (0:14): 14*
15 (0:15): 15*
16 (0:16): 16*
17 (0:17): 17*
18 (0:1: 18*
19 (0:19): 19*
20 (0:20): 20*
21 (0:21): 21*
22 (0:22): 22*
23 (0:23): 23*
24 (0:24): 24*
25 (0:25): 25*
26 (0:26): 26*
27 (0:27): 27*
28 (0:2: 28*
29 (0:29): 29*
30 (0:30): 30*
31 (0:31): 31*
32 (0:32): 32*
33 (0:33): 33*
34 (0:34): 34*
35 (0:35): 35*
36 (0:36): 36*
37 (0:37): 37*
38 (0:3: 38*
39 (0:39): 39*
40 (0:40): 40*
41 (0:41): 41*
42 (0:42): 42*
43 (0:43): 43*
44 (0:44): 44*
45 (0:45): 45*
46 (0:46): 46*
47 (0:47): 47*
48 (0:4: 48*
49 (0:49): 49*
50 (0:50): 50*
51 (0:51): 51*
52 (0:52): 52*
53 (0:53): 53*
54 (0:54): 54*
55 (0:55): 55*
56 (0:56): 56*
57 (0:57): 57*
58 (0:5: 58*
59 (0:59): 59*
60 (1:00): 60*
61 (1:01): 61*
62 (1:02): 62*
63 (1:03): 63*
64 (1:04): 64*
65 (1:05): 65*
66 (1:06): 66*
67 (1:07): 67*
68 (1:0: 68*
69 (1:09): 69*
70 (1:10): 70*
71 (1:11): 111*
72 (1:12): 112*
73 (1:13): 113*
74 (1:14): 114*
75 (1:15): 115*
76 (1:16): 116*
77 (1:17): 77*
78 (1:1: 78*
79 (1:19): 79*
80 (1:20): 80*
81 (1:21): 121*
82 (1:22): 122*
83 (1:23): 123*
84 (1:24): 84*
85 (1:25): 85*
86 (1:26): 86*
87 (1:27): 87*
88 (1:2: 88*
89 (1:29): 89*
90 (1:30): 90*
91 (1:31): 131*
92 (1:32): 132*
93 (1:33): 133*
94 (1:34): 94*
95 (1:35): 95*
96 (1:36): 96*
97 (1:37): 97*
98 (1:3: 98*
99 (1:39): 99*

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

Josef 'Jupp' Schugt
Guest
Posts: n/a

 03-31-2007
--------------enig97B57A0F9012E2DABA691387
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

* Hans Fugal, 30.03.2007 17:50:
> My microwave will win in every case. It has a dial timer.

If it wins or not is not decided. Mine has a dial timer as well. As a
shootout we would have to compare degrees per minute

Besides: I wonder why microwave ovens don't simply have an up and a
dow button (like those present on most mobile phones) to
increment/decrement the time setting(s).

A dial timer is quite imprecise (especially for short time intervals)
for mechanical reasons while a numerical pad is unnecessarily
complicated meaning that it has many parts that may fail. They won't
fail? Hmm, perhaps you compare it to your PC keyboard, but: In most
households the s/p probability density peaks near the microwave, and
not near the keyboard.

s/p: softdrink/pizza

Josef 'Jupp' Schugt, eagerly awaiting tomorrow's RFC
--=20
Blog available at http://www.mynetcologne.de/~nc-schugtjo/blog/
PGP key with id 6CC6574F available at http://wwwkeys.de.pgp.net/

--------------enig97B57A0F9012E2DABA691387
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFGDbdgrhv7B2zGV08RAv5cAKCFCUVbYpUjd0Kv2/JFzsudf5EsvwCfSVM7
Lh9YfMLcKW2mYY1AKVsXbEk=
=gFjD
-----END PGP SIGNATURE-----

--------------enig97B57A0F9012E2DABA691387--

Bill Kelly
Guest
Posts: n/a

 03-31-2007
From: "Josef 'Jupp' Schugt" <(E-Mail Removed)>
>
> Besides: I wonder why microwave ovens don't simply have an up and a
> dow button (like those present on most mobile phones) to
> increment/decrement the time setting(s).

Ours does. It has an "add a minute" button, but also +/- 10 seconds
buttons. (They can be used before or after pressing the "Start"
button.)

Semi-interestingly, the "add a minute" button includes an implicit
"Start" button press.

So I almost invariably choose "add a minute; add a minute" over
"2 0 0 <start>".

LOL

Regards,

Bill