Hi Todd,

I like your math-y solution -- it probably runs circles around the
non-mathy ones.

For generating a random array of numbers, I like to use this technique:



>> Array.new(5) { rand(100) }
=> [47, 78, 88, 39, 61]
>> Array.new(5) { rand(100) }
=> [48, 38, 33, 94, 98]

So you could do this to generate an array of arrays like the one in your post:

>> require 'pp'
=> false
>> def rand_ar
>>   Array.new(10) do
?>       Array.new(3) do
?>         Array.new(3) { rand(100) - 50 }
>>       end
>>     end
>>   end
=> nil
>> pp rand_ar
[[[47, 14, 20], [1, -18, -15], [7, -46, -44]],
 [[-5, -44, -32], [-40, 9, 1], [16, 16, 10]],
 [[-15, -13, 2], [16, -16, 37], [-1, 17, -4]],
 [[-13, 5, -31], [47, -30, -27], [13, -2, -16]],
 [[23, -50, 12], [3, 34, 6], [16, 24, -34]],
 [[-6, -19, -25], [21, 5, -47], [-38, -7, -13]],
 [[13, 48, 23], [12, -33, -7], [48, -14, -47]],
 [[-12, -28, -31], [3, 37, -16], [-50, 29, -44]],
 [[13, -21, 29], [36, 30, 45], [8, 9, 36]],
 [[-16, 8, -47], [43, -49, 42], [45, 4, 5]]]
=> nil
>> pp rand_ar
[[[45, 5, 34], [19, -14, 11], [-17, 32, -43]],
 [[-2, 31, 40], [-16, 40, -19], [-10, -18, 37]],
 [[21, 38, -13], [47, 23, -10], [0, -2, -12]],
 [[7, 49, 8], [-23, -38, -25], [-45, -31, -3]],
 [[47, -20, -34], [39, -21, -35], [17, 17, -6]],
 [[0, 24, -2], [-38, 14, -26], [4, 2, -9]],
 [[47, 7, -34], [28, -24, 18], [-9, -5, -6]],
 [[-34, -15, 1], [-26, 17, -2], [-8, 46, 30]],
 [[-20, 23, -28], [44, -12, -18], [8, -40, -47]],
 [[31, -6, 32], [5, 27, 31], [-14, -14, -41]]]

Dan

On Mon, Apr 21, 2008 at 6:08 PM, Todd Benson <caduceass / gmail.com> wrote:
> I'm doing a little cheating here, and may have severely wimped out.
>  Not only did I fail to meet the actual requirements of the quiz, but
>  also ignored the unit test (well, not completely).  I was going to
>  --and may still-- use dot products some other day to show off my
>  meager knowledge of math and allow myself to enter valhalla.  I'm not
>  here for prosperity, so, anyways, my one-liner to satisfy the quiz was
>  atrocious.  To replace it, I'll use this for now...
>
>  require 'mathn'
>  puts 0.5 * Matrix[[28.0, 23.0, 1.0], [-46.0, 31.0, 1.0], [-39.0, 5.0,
>  1.0]].det.abs
>
>  => 934.0
>
>  That's probably the easiest way.
>
>  Doing a redneck modify to the test data structure...
>
>  require 'mathn'
>  arr = [
>    [[-42, 4, 1], [-26, -34, 1], [  2,   8, 1]],
>    [[ 45, -44, 1], [  1,  43, 1], [ 42,  48, 1]],
>    [[-24,  29, 1], [ 42,  -1, 1], [ 10,  43, 1]],
>    [[ 48, -19, 1], [-19,  37, 1], [-15,  36, 1]],
>    [[-10, -40, 1], [-35, -19, 1], [  1,  33, 1]],
>    [[ 28,  23, 1], [-46,  31, 1], [-39,   5, 1]],
>    [[-32,  17, 1], [-50,  -8, 1], [-39,  27, 1]],
>    [[ 40, -19, 1], [ 39, -34, 1], [-37, -15, 1]],
>    [[ 47, -34, 1], [ 26, -37, 1], [ 50,  -7, 1]],
>    [[-49,  46, 1], [ 29,  46, 1], [  5, -34, 1]],
>   ]
>  arr.each do |a|
>   puts 0.5 * Matrix[*a].det.abs
>  end
>
>  <output/>
>  868.0
>  1893.5
>  972.0
>  78.5
>  1028.0
>  934.0
>  177.5
>  579.5
>  279.0
>  3120.0
>
>  It's not "application worthy", but I wasn't shooting for that.  I'm
>  also not sure if you might have to use Floats in the array above or
>  not, because it passes the test given Integers.
>
>  I wrote some code that attempts to build a random array, but I'm
>  embarrassed to show it; and also the Vector into Matrix code I used
>  looks ugly.
>
>  I wrote that snippet of code before going to wikipedia.  I had this
>  feeling originally that I should attempt something like Heron's idea,
>  but didn't have the time.
>
>  I suppose another way to approach it could be to use a matrix
>  transformation to get your 'base' (turn the triangle, or the
>  coordinate system; however you prefer to see it) and use a simple
>  1/2(b*h).
>
>  Non-euclidean would be an interesting extra credit.
>
>  Todd
>
>