Hackerrank Blocks Solution
2 min read

Hackerrank Blocks Solution

Hackerrank Blocks Solution

.MathJax_SVG_Display {text-align: center; margin: 1em 0em; position: relative; display: block!important; text-indent: 0; max-width: none; max-height: none; min-width: 0; min-height: 0; width: 100%} .MathJax_SVG .MJX-monospace {font-family: monospace} .MathJax_SVG .MJX-sans-serif {font-family: sans-serif} .MathJax_SVG {display: inline; font-style: normal; font-weight: normal; line-height: normal; font-size: 100%; font-size-adjust: none; text-indent: 0; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0; min-height: 0; border: 0; padding: 0; margin: 0} .MathJax_SVG * {transition: none; -webkit-transition: none; -moz-transition: none; -ms-transition: none; -o-transition: none} .mjx-svg-href {fill: blue; stroke: blue}

Higher order functions are one of the key components of functional programming.

A higher order function is a tool that takes other functions as parameters or returns them as a result.

Blocks are nameless methods that can be passed to another method as a parameter.
Passing a block to a method is a great way of data abstraction.

Blocks can either be defined with a keyword do ... end or curly braces { ... }.

Example:

a). Passing a block to a method that takes no parameter

CODE

def call_block
    puts "Start of method."
    yield
    puts "End of method."
end 
call_block do 
    puts "I am inside call_block method."
end

OUTPUT

Start of method.
I am inside call_block method.
End of method.

In this example, a block is passed to the call_block method.
To invoke this block inside the method, we used a keyword, yield.
Calling yield will execute the code within the block that is provided to the method.

b). Passing a block to a method that takes one or more parameters.

CODE

def calculate(a,b)
    yield(a, b)
end

puts calculate(15, 10) {|a, b| a - b}   

OUTPUT

5

In this example, we have defined a method calculate that takes two parameters  and .
The yield statement invokes the block with parameters  and , and executes it.


Task

You are given a partially complete code. Your task is to fill in the blanks (_______).
The factorial method computes: n! {  x  x  x  }.

Solution in ruby

Approach 1.

def factorial
    yield
end

n = gets.to_i
factorial do 
    puts (1..n).inject(:*)
end

Approach 2.

def factorial
    yield
end

n = gets.to_i
factorial do 
    puts (1..n).reduce(:*)
end

Approach 3.

def factorial
    yield
end

n = gets.to_i
factorial do 
    puts (1..n).inject(:*)   
end

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.

×