-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAverage Array
More file actions
35 lines (23 loc) · 966 Bytes
/
Copy pathAverage Array
File metadata and controls
35 lines (23 loc) · 966 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
ASC Week 1 Challenge 5 (Medium #2)
Create a function that takes a 2D array as an input, and outputs another array that contains the average values for the numbers in the nested arrays at the corresponding indexes.
Note: the function should also work with negative numbers and floats.
Examples
[ [1, 2, 3, 4], [5, 6, 7, 8] ] ==> [3, 4, 5, 6]
1st array: [1, 2, 3, 4]
2nd array: [5, 6, 7, 8]
| | | |
v v v v
average: [3, 4, 5, 6]
And another one:
[ [2, 3, 9, 10, 7], [12, 6, 89, 45, 3], [9, 12, 56, 10, 34], [67, 23, 1, 88, 34] ] ==> [22.5, 11, 38.75, 38.25, 19.5]
1st array: [ 2, 3, 9, 10, 7]
2nd array: [ 12, 6, 89, 45, 3]
3rd array: [ 9, 12, 56, 10, 34]
4th array: [ 67, 23, 1, 88, 34]
| | | | |
v v v v v
average: [22.5, 11, 38.75, 38.25, 19.5]
*/
def avg_array(arrs):
return [sum(e) / len(e) for e in zip(*arrs)]