Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Ruby – Enumerable – reduce problem solution

YASH PAL, 31 July 202429 January 2026

In this HackerRank Ruby – Enumerable – reduce problem 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 1

n = 1

product = 1

return value = 1*1

# loop 2

n = 2

product = 1

return value = 1*2

n = 3

product = 2

return 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 nth term of the sequence is denoted by tn = n*n + 1, n >= 0. 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.

HackerRank Ruby - Enumerable - reduce problem solution

HackerRank Ruby – Enumerable – reduce problem solution.

def sum_terms(n)
  # your code here
    nums = []
    (1).upto(n) do |i|
        nums << ((i*i)+1)
    end
    nums.reduce(0, :+)
end

Ruby – Enumerable – reduce problem solution.

def array_up_to(i)
    (1..i).to_a
end
def sum_terms(n)
    if n==0
        return 0 
    end
    t = array_up_to(n)
    t=t.map{|x| x**2+1}
    
    return t.inject{|sum,x| sum + x }
    
    
  # your code here
end

coding problems solutions Hackerrank Problems Solutions Ruby Solutions HackerRankruby

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes