7 Replies - 2115 Views - Last Post: 28 January 2014 - 08:42 AM Rate Topic: -----

#1 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1368
  • View blog
  • Posts: 3,450
  • Joined: 28-November 09

[Challenge][Basic] Avg Sum Array Collection

Post icon  Posted 27 January 2014 - 05:59 PM

Another one of my recent things I had to think for more than a few seconds about at work, summing an array collection.

Given an array collection:
[
  [1,2,3,4],
  [2,3,4,5],
  [3,4,5,6]
]



Return an array with the average of the columns:
[2,3,4,5]



While basic, you may find an interesting feature of Ruby.

Bonus

That same solution has to work for:
[[1,2,3,4]]



I'll post my solution some time later tomorrow, unless a lot of replies come in tonight.

Problem Sets

10 x 10
[
 [99, 47, 80, 22, 83, 45, 4, 4, 59, 65],
 [13, 24, 50, 86, 6, 72, 83, 46, 94, 96],
 [75, 73, 0, 56, 7, 50, 56, 49, 7, 56],
 [78, 28, 6, 44, 63, 0, 88, 88, 67, 45],
 [63, 70, 2, 7, 7, 71, 91, 66, 4, 78],
 [96, 38, 46, 90, 40, 50, 94, 53, 45, 31],
 [24, 37, 53, 19, 94, 11, 82, 12, 96, 84],
 [94, 7, 10, 71, 44, 15, 42, 72, 54, 95],
 [3, 13, 32, 30, 53, 58, 63, 76, 50, 11],
 [75, 8, 97, 68, 27, 4, 67, 98, 64, 1]
]



Solution:
[62, 34, 37, 49, 42, 37, 67, 56, 54, 56]


This post has been edited by Lemur: 27 January 2014 - 09:25 PM


Is This A Good Question/Topic? 1
  • +

Replies To: [Challenge][Basic] Avg Sum Array Collection

#2 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,867
  • Joined: 12-December 12

Re: [Challenge][Basic] Avg Sum Array Collection

Posted 27 January 2014 - 06:54 PM

What's a flattened average? Perhaps it has an alternative name as I couldn't discover it on a brief search.
Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: [Challenge][Basic] Avg Sum Array Collection

Posted 27 January 2014 - 07:13 PM

Here's a cute way to do it. Probably not canonical, but it works.

some_array_collection.reduce(:+).reduce(:+)


Probably this repetitive code will make someone cry...

(and yes, I know this isn't a general solution... working on that)

View Postandrewsw, on 27 January 2014 - 08:54 PM, said:

What's a flattened average? Perhaps it has an alternative name as I couldn't discover it on a brief search.


Flatten the nested array, then take the average. "Flatten" is a typical FP function, it generally means to take a nested collection and return the inorder traversal.
(((this is) (a) deeply) (nested ((((collection))))) isn't it?)
=>flattens to=>
(this is a deeply nested collection, isn't it?)  




EDIT:
Okay, here's my better attempt:

some_nested_array.flatten.reduce(:)/>/>


Should have looked in the docs.

This post has been edited by jon.kiparsky: 27 January 2014 - 07:13 PM

Was This Post Helpful? 0
  • +
  • -

#4 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1368
  • View blog
  • Posts: 3,450
  • Joined: 28-November 09

Re: [Challenge][Basic] Avg Sum Array Collection

Posted 27 January 2014 - 07:17 PM

....it's a Monday. Rephrased to sum of columns.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: [Challenge][Basic] Avg Sum Array Collection

Posted 27 January 2014 - 07:22 PM

Note to self: read problem before solving. :)/>

This post has been edited by jon.kiparsky: 27 January 2014 - 07:22 PM

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3480
  • View blog
  • Posts: 11,867
  • Joined: 12-December 12

Re: [Challenge][Basic] Avg Sum Array Collection

Posted 27 January 2014 - 08:15 PM

Doh, on a quick read I was looking across the row at 1,2,3 then wondering why you were back-tracking to cover 2,3,4(?). It didn't help that these were the same numbers going down :whistling: .. you could have gone all the way up-to 12 :)
Was This Post Helpful? 0
  • +
  • -

#7 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1368
  • View blog
  • Posts: 3,450
  • Joined: 28-November 09

Re: [Challenge][Basic] Avg Sum Array Collection

Posted 27 January 2014 - 09:19 PM

To make it more fun, here are your generators:
def gen_row(n)
  n.times.reduce([]) { |ary, _| ary << rand(100).to_i }  
end  

def gen_table(n)
  n.times.reduce([]) { |ary, _| ary << gen_row(n) }
end

def rand_table
  gen_table(rand(100).to_i)
end



Make something that will get the average of each column for rand_table, which will be a 1x1 to 100x100 table with random numbers from 1 to 100.

Spoiler

This post has been edited by Lemur: 27 January 2014 - 09:27 PM

Was This Post Helpful? 0
  • +
  • -

#8 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: [Challenge][Basic] Avg Sum Array Collection

Posted 28 January 2014 - 08:42 AM

I can do it in one line of scheme or common lisp :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1