# [Challenge][Basic] Avg Sum Array Collection

Page 1 of 1

## 7 Replies - 2290 Views - Last Post: 28 January 2014 - 08:42 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=339061&amp;s=b335e6a556b9e26b6120a4309d81b30c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Lemur

• Pragmatism over Dogma

Reputation: 1438
• Posts: 3,609
• Joined: 28-November 09

# [Challenge][Basic] Avg Sum Array Collection

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

• I'm not here to twist your niblets

Reputation: 4708
• Posts: 17,489
• 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.

### #3 jon.kiparsky

• Pancakes!

Reputation: 8770
• Posts: 15,098
• 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)

andrewsw, 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

### #4 Lemur

• Pragmatism over Dogma

Reputation: 1438
• Posts: 3,609
• 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.

### #5 jon.kiparsky

• Pancakes!

Reputation: 8770
• Posts: 15,098
• 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

### #6 andrewsw

• I'm not here to twist your niblets

Reputation: 4708
• Posts: 17,489
• 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 .. you could have gone all the way up-to 12

### #7 Lemur

• Pragmatism over Dogma

Reputation: 1438
• Posts: 3,609
• 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

### #8 mostyfriedman

• The Algorithmi

Reputation: 728
• 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