Hackerrank Procs Solution
2 min read

Hackerrank Procs Solution

Hackerrank Procs 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}

Passing blocks is one way to pass functions as arguments to other functions.

Blocks are one of the very few exceptions to the "everything is an object" rule in Ruby. Blocks are not objects, and they can't be saved to variables.

Proc objects are blocks of code that can be bound to a set of local variables. You can think of a proc object as a "saved" block.

Procs are a great way of keeping your code DRY. DRY stands for Do not Repeat Yourself.

Example:

CODE

def foo(a, b, my_proc)
    my_proc.call(a, b)
end

add = proc {|x, y| x + y}

puts foo(15, 10, add)

OUTPUT

25

In this example, we have created the proc add, which adds two numbers.
The proc add is passed as a parameter to the method foo.
In the method foo, my_proc.call(a, b) executes the proc.


Task

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

The square_of_sum method computes the sum of the elements in an input array and returns the square of the summed elements.

For example:

Input array: [1, 2, 3]
Output: 36

Solution in ruby

Approach 1.

def square_of_sum my_array
    (my_array.inject :+)**2
end
my_array = gets.split().map(&:to_i)

puts square_of_sum(my_array)

Approach 2.

def square_of_sum (my_array, proc_square, proc_sum)
    sum = proc_sum.call(my_array)
    proc_square.call(sum)
end
proc_square_number = proc {|x| x**2 }
proc_sum_array = proc {|y| y.inject(:+)} 
my_array = gets.split().map(&:to_i)

puts square_of_sum(my_array, proc_square_number, proc_sum_array)

Approach 3.

def square_of_sum (my_array, proc_square, proc_sum)
    sum = proc_sum.call(my_array)
    proc_square.call(sum)
end

proc_square_number = proc {|x| x*x}
proc_sum_array     = proc {|a| a.reduce(:+)}
my_array = gets.split().map(&:to_i)

puts square_of_sum(my_array, proc_square_number, proc_sum_array)

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.

×