# Help Kiyo きよ solve her problems LCM Fun!

From codewars.com by Edxael (that’s me)

### Description:

### Help Kiyo きよ solve her problems LCM Fun!

Kiyo has been given a series of problems and she needs your help to solve them!

You will be given a two-dimensional array such as the one below.

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 |
a = [ [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9], [1, 2, 3, 4, 5, 6, 7, 8, 9] ] Remove all even integers from each sub-array. Sum the remaining odd integers of each sub-array. Sum of odds ( a[0] = 1 + 3 + 5 + 7 + 9 ) = 25 Find the Least common multiple of the arrays. (25, 25, 25, 25, 25, 25, 25, 25, 25) ^ ^ | | a[0]-----------------------------a[8] example : lcm( 25, 25, 25, 25, 25, 25, 25, 25, 25 ) = 25 example : lcm( 37, 29, 19, 38, 31, 28, 15, 24, 9 ) = 1592632440 |

Integers are between 0 and 9. Sub-array size is always 9. The number of sub-arrays varies between 9 and 18.

Watch out for non-integers mixed in the arrays. If all arrays are empty return 0.

My solution:

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 |
#Help Kiyo きよ solve her problems LCM Fun! def kiyo_lcm(a) totals = [] a.size.times do |x| a[x].size.times do |y| if a[x][y].class == String then a[x][y] = 0 end if a[x][y].even? == true then a[x][y] = 0 end end totals << a[x].inject {|acc, n| acc + n} end totals.compact.reduce(:lcm) == nil ? 0 : totals.compact.reduce(:lcm) end a = [[1, 1, 5, 4, 4, 2, 5, 1, 5], [4, 2, 3, 5, 1, 4, 4, 2, 5], [4, 2, 1, 4, 5, 5, 4, 3, 3], [2, 1, 5, 1, 1, 1, 1, 2, 4], [5, 1, 3, 1, 3, 2, 4, 2, 1], [3, 1, 1, 2, 4, 2, 5, 3, 5], [5, 2, 5, 3, 3, 4, 3, 4, 1], [3, 4, 4, 5, 5, 5, 5, 4, 5], [3, 3, 5, 5, 3, 3, 3, 5, 2]] p kiyo_lcm(a) # ---------------------------------------- # Test Run # # 21420 # ---------------------------------------- |

Note: The following code is not written by me, it was created by another user of codewars.com it belongs to the user: Kazk

1 2 3 |
def kiyo_lcm(a) a.reduce(1) {|a,b| a.lcm(b.reduce(0) {|s,x| (x.is_a? Integer) && x.odd? ? s + x : s})} end |

Note: The following code is not written by me, it was created by another user of codewars.com it belongs to the user: njohnson7

1 2 3 4 |
def kiyo_lcm(a) odds = a.map { |sub| sub.select { |c| c.is_a?(Integer) && c.odd? } } odds.map { |sub| sub.reduce(0, :+) }.reduce(:lcm) end |

.