Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerRank Partial Applications problem solution in ruby

YASH PAL, 31 July 2024

In this HackerRank Partial Applications problem solution in ruby programming In Partial Application, we create a lambda that takes a parameter and returns a lambda that does something with it.

Example:

multiply_function = -> (number) do

   -> (another_number) do

       number * another_number

   end

end

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.

Task

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

Here, the combination is a variable that stores a partial application that computes combination nCr.

HackerRank Partial Applications problem solution in ruby

Problem solution.

class Integer
  def fact
    (1..self).reduce(:*) || 1
  end
end
combination = -> (n) do
    -> (r) do
        n.fact / (r.fact*(n-r).fact)
    end
end
n = gets.to_i
r = gets.to_i
nCr = combination.(n)
puts nCr.(r)

Second solution.

combination = -> (n) do
   -> (r) do
       top = (n-r+1..n).reduce(:*)
       top/(1..r).reduce(:*)
   end
end

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

coding problems solutions Ruby Solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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