Hackerrank Partial Applications Solution
1 min read

Hackerrank Partial Applications Solution

Hackerrank Partial Applications Solution

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, combination is a variable that stores a partial application which computes combination .

Solution in ruby

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


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

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.

×