Hackerrank Ruby - Enumerable - reduce Solution
1 min read

Hackerrank Ruby - Enumerable - reduce Solution

Hackerrank Ruby - Enumerable - reduce Solution

A common scenario that arises when using a collection of any sort, is to get perform a single type of operation with all the elements and collect the result.

For example, a sum(array) function might wish to add all the elements passed as the array and return the result.

A generalized abstraction of same functionality is provided in Ruby in the name of reduce (inject is an alias). That is, these methods iterate over a collection and accumulate the value of an operation on elements in a base value using an operator and return that base value in the end.

Let's take an example for better understanding.>>> (5..10).inject(1) {|product, n| product * n }=> 151200

In above example, we have the following elements: a base value 1, an enumerable (5..10), and a block with expressions instructing how to add the calculated value to base value (i.e., multiply the array element to product, where product is initialized with base value)

So the execution follows something like this:# loop 1n = 1product = 1return value = 1*1 # loop 2n = 2product = 1return value = 1*2 n = 3product = 2return value = 2*3 ..

As you can notice, the base value is continually updated as the expression loops through the element of container, thus returning the final value of base value as result.

Other examples,>>> (5..10).reduce(1, :*)   # :* is shorthand for multiplication=> 151200

Consider an arithmetico-geometric sequence where the  term of the sequence is denoted by . In this challenge, your task is to complete the sum method which takes an integer n and returns the sum to the n terms of the series.

Solution in ruby

Approach 1.

def sum_terms(n)
    (1..n).reduce(0) {|sum, n| sum + n*n + 1}

Approach 2.

def sum_terms(n)
  (1..n).inject(0) { |sum, t| sum + ((t * t) + 1) }

Approach 3.

def sum_terms(n)
 (1..n).reduce(0) {|sum, val| sum += (val * val + 1)}

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.