HackerRank Ruby Partial Applications Solution

Hello coders, In this post, you will learn how to solve HackerRank Ruby Partial Applications Solution. This problem is a part of the Ruby Tutorial series. One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions.

HackerRank Ruby Partial Applications Solution
HackerRank Ruby Partial Applications Solution

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

HackerRank Ruby Partial Applications Solution

Let’s get started with Ruby Partial Applications Solution

Problem Statement

In Partial Application, we create a lambda that takes a parameter and returns a lambda that does something with it.


multiply_function = -> (number) do
-> (another_number) do
number * another_number

doubler = multiply_function.(2)
tripler = multiply_function.(3)

puts doubler.(4)
puts tripler.(4)

In the above example, the lambda will take number as a parameter, and return a lambda. When you call this lambda with another_number, it will return the product of the two.


You are given a partially complete code. Your task is to fill in the blanks (_______).

Here, combination is a variable that stores a partial application which computes

combination nCr.

HackerRank Ruby Partial Applications Solution

combination = -> (n) do
        -> (r) do
        # https://en.wikipedia.org/wiki/Combination
        (n-r+1..n).inject(:*) / (1..r).inject(:*)

n = gets.to_i
r = gets.to_i
nCr = combination.(n)
puts nCr.(r)    

Note: This problem (Ruby Array – Initialization is generated by HackerRank but the solution is provided by Chase2Learn. This tutorial is only for Educational and Learning purpose.

Sharing Is Caring