Micrrowave Numbers (#118)

The three rules of Ruby Q.:

  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 Q. by submitting ideas as often as you can:

http://www.rubyquiz.com/

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

  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?

On Mar 30, 7:55 am, Ruby Q. [email protected] wrote:

    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.

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?

- Warren B.

On Mar 30, 10:08 am, “Karl von Laudermann” [email protected]
wrote:

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

Clearly, I meant “the last two digits aren’t 60 or greater”.

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 G. II

On Mar 30, 2007, at 9:11 AM, Brown, Warren wrote:

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?

Hmm, these are good points. Let’s just focus on the two number
scenarios for the purpose of this quiz then.

James Edward G. II

On Fri, 30 Mar 2007 23:17:55 +0900
James Edward G. II [email protected] wrote:

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?

Hmm, these are good points. Let’s just focus on the two number
scenarios for the purpose of this quiz then.

It’s still possible to use larger numbers, just a bit of math is needed.

For example, 4:09 can also be 3:69, with the latter being a much
shorter distance. On the flip side, compare 12:33 with 11:93, where
the former is shorter.

On Mar 30, 2007, at 9:42 AM, Jamie M. wrote:

123 seconds is 2:03, but 203 is a lot more distance

Hmm, these are good points. Let’s just focus on the two number
scenarios for the purpose of this quiz then.

It’s still possible to use larger numbers, just a bit of math is
needed.

For example, 4:09 can also be 3:69, with the latter being a much
shorter distance. On the flip side, compare 12:33 with 11:93, where
the former is shorter.

Good point.

James Edward G. II

On 3/30/07, Brown, Warren [email protected] wrote:

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

On 3/30/07, Brown, Warren [email protected] wrote:

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

On Fri, 30 Mar 2007 23:11:45 +0900, Brown, Warren wrote:

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?

- Warren B.

Here’s a thought: for times from 0-99 seconds, determine the fastest way
of typing those times.

For times above 99 seconds, do as follows:
min,sec=thetime.divmod(60)
min-=1
sec+=60
and compute the distances that way.

My microwave will win in every case. It has a dial timer.

  • Hans F., 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 :slight_smile:

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 :slight_smile:

From: “Josef ‘Jupp’ Schugt” [email protected]

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. :slight_smile:

So I almost invariably choose “add a minute; add a minute” over
“2 0 0 ”.

LOL

Regards,

Bill

On Fri, 30 Mar 2007 20:55:46 +0900, Ruby Q. wrote:

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.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
=-=-=-=-=
saved. (Yes, it’s rather insignificant, but don’t tell them… us…
±–±--±–+
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) =>

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

  2. 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:08): 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:18): 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:28): 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:38): 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:48): 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:58): 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:08): 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:18): 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:28): 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:38): 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:08): 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:18): 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:28): 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:38): 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:48): 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:58): 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:08): 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:18): 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:28): 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:38): 98*
99 (1:39): 99*

Ken B. [email protected] writes:

74 (1:14): 114*
75 (1:15): 115*
76 (1:16): 116*

I think 74* is faster, because it’s one keypress less?

On 3/30/07, Josef ‘Jupp’ Schugt [email protected] wrote:

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.

Well our MW has a digital timer with a dial input. The dial works
like the dreaded BMW iDrive control, you can turn it AND push it.

To set the timer you hit the timer or cook time button, then twist the
dial to set the minutes which show up on the digital display. You
then push the dial in which shifts it to setting the seconds, a second
push on the dial completes the process.

And no, you can’t enter seconds greater than 59 with this setup.


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

On Sat, 31 Mar 2007 19:38:03 +0900, Christian N. wrote:

Ken B. [email protected] writes:

74 (1:14): 114*
75 (1:15): 115*
76 (1:16): 116*

I think 74* is faster, because it’s one keypress less?

The quiz only discussed distance moved, not keys pressed (in the main
solution). This appears to be a tie for distance, so my program just
picked one. I suppose it’s not too hard to add a tiebreaker metric
though.

–Ken

On 3/31/07, Rick DeNatale [email protected] wrote:

To set the timer you hit the timer or cook time button, then twist the
My blog on Ruby
http://talklikeaduck.denhaven2.com/

Wait I second guys my MW has a Ruby interpreter :wink: [1.6 though, Matz
did you abandon the Microwave edition?]

Robert

On Fri, 30 Mar 2007 20:55:46 +0900, Ruby Q. wrote:

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.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
=-=-=-=-=
saved. (Yes, it’s rather insignificant, but don’t tell them… us…
±–±--±–+
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) =>

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

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

require ‘matrix’
require ‘enumerator’
class Matrix

returns the row and column of the value requested, or nil if not

found

def find value
row_vectors.each_with_index do |row,rownum|
row=row.to_a
row.each_with_index do |col,colnum|
return rownum,colnum if col==value
end
end
end
end

#these functions are the distance metrics

def euclidian_distance array1, array2
Math.sqrt(array1.zip(array2).inject(0){|a,(v1,v2)| a+(v2-v1)**2})
end
def manhattan_distance array1, array2
array1.zip(array2).inject(0){|a,(v1,v2)| a+(v2-v1).abs}
end
def num_buttons array1, array2
1
end
def rand_metric array1, array2
rand
end

#make it easy to try out different distance metrics by changing these
#aliases
alias distance_metric euclidian_distance
alias tiebreaker_metric num_buttons

now we compute acutal Primary for all pairs

if we wanted, we could write a function that computes this every

time rather than memoizing it in a hash

Positions=Matrix[[‘1’,‘2’,‘3’],[‘4’,‘5’,‘6’],[‘7’,‘8’,‘9’],[’-’,‘0’,’*’]]
Primary={}
Tiebreaker={}

(‘0’…‘9’).each do |from|
(‘0’…‘9’).each do |to|
Primary[[from,to]]=distance_metric(
Positions.find(from),
Positions.find(to))
Tiebreaker[[from,to]]=tiebreaker_metric(
Positions.find(from),
Positions.find(to))
end
Primary[[from,’’]]=distance_metric(
Positions.find(from),
Positions.find(’
’))
Tiebreaker[[from,’’]]=tiebreaker_metric(
Positions.find(from),
Positions.find(’
’))
end

computes the distance and the string used for a specific (possibly

improper) number of minutes and seconds to be entered into the

microwave

def make_array min,sec
("%d%02d*" % [min,sec]).gsub(/^0+([^*])/,’\1’).split(//)
end

def compute_dist array,distances
array.enum_cons(2).inject(0){|a,v| a+distances[v]}
end

given the number of seconds to run the microwave for, this function

returns the shortest path of buttons that one can press to make the

microwave run for that period of time

if both possibilites have the same total distance, then the function

just picks one in some undefined way

def compute_best_distance sec
min_improper,sec_improper=(min_proper,sec_proper=sec.divmod(60))
if min_improper>0 and sec_improper<40
min_improper-=1
sec_improper+=60
else
#the improper time will be the same as the proper time, which
#isn’t a problem
end
proper=make_array(min_proper,sec_proper)
improper=make_array(min_improper,sec_improper)
[[
compute_dist(proper,Primary),
compute_dist(proper,Tiebreaker),
proper
],[
compute_dist(improper,Primary),
compute_dist(improper,Tiebreaker),
improper
]].sort[0][-1].join
end

#print a the values for runs up to 5 minutes long
(0…300).each do |x|
printf “%d (%s): %s\n”, x, “%d:%02d” % x.divmod(60),
compute_best_distance(x)
end