[REBOL] Re: More on expressions
From: carl:cybercraft at: 5-Feb-2003 21:51
On 03-Feb-03, Joel Neely wrote:
> Many thanks to all who've participated in this thread. Anyone who
> wants to keep playing, skip reading this email until you've had
> your fun!
Okay, I'm late to this, although I didn't skip the following, but just
skimmed it to the end to see Joel's solution. ;-) And, for anyone
still playing I'll leave a few lines in saying nothing so you won't
see Joel's solution, or mine. So...
> For those who'd like a hint about another approach,
> keep
> reading
> this
> email
> below.
> .
> .
> .
> .
> .
> .
> .
> .
> .
> .
[snip]
> and then we recall that we only wanted the median, which means we
> only wanted the ultimate value for B, giving us our final answer:
> (a min b) max ((a max b) min c)
Which was wrong as it happens, but here's Joel's corrected (in a later
post) version, written as a function:
median3: func [a b c] [max (min a b) (min (max a b) c)]
As I said, I just skimmed Joel's post that explained how he arrived at
his solution, but I did notice the use of min and max. So now for a
brief explaination of how I arrived at my soloution.
Firstly, I worked out what I considered the easiest way to get the
maximum and minimum values from A, B and C...
>> a: 1 b: 2 c: 3
== 3
>> max max a b c
== 3
>> min min a b c
== 1
This gave me the idea for my first solution...
>> difference reduce [a b c] reduce [max max a b c min min a b c]
== [2]
which returned the correct result, but in a block, and difference may
be against the spirit of the game anyway, since sort and so on was
out. (Difference is the word I thought of first when I read Joel's
original post containing his challenge.)
After more doodling, I hit on the thought that this...
min a b min b c min a c
would produce three values, none of which would be the maximum value
of A, B and C, but which would include the median value, which of
course would be the maximum value of these three new values. So, put
a max and a max in front of those and my solution is...
>> max max min a b min b c min a c
== 2
A triffle longer than Joel's, but very REBOL I think. (And I do so
hate parens;)
--
Carl Read