You've successfully subscribed to The Poor Coder | Hackerrank Solutions
Great! Next, complete checkout for full access to The Poor Coder | Hackerrank Solutions
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Hackerrank Ruby Array - Selection Solution

Hackerrank Ruby Array - Selection Solution

Beeze Aal
Beeze Aal

.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}

The array class also allows to select and return a subset of an array based on some criteria defined in a block (a block is a group of code within {} that accepts a variable and returns a value).

  • Selecting elements that satisfy a given criteria
  • Rejecting elements that satisfy a given criteria > arr = [3, 4, 2, 1, 2, 3, 4, 5, 6] > arr.select {|a| a > 2} => [3, 4, 3, 4, 5, 6] > arr.reject {|a| a > 2} => [2, 1, 2] > arr => [3, 4, 2, 1, 2, 3, 4, 5, 6] > arr.drop_while {|a| a > 1} # removes elements till the block returns false for the first time => [1, 2, 3, 4, 5, 6]

As you can see, the original array remains unchanged. This is called Non-Destructive Selection.
For destructive behavior (change to the original array), Ruby provides the following methods:

 > arr = [3, 4, 2, 1, 2, 3, 4, 5, 6]  
 > arr.delete_if {|a| a < 2}
  => [3, 4, 2, 2, 3, 4, 5, 6]  
 > arr.keep_if {|a| a < 4}  
 => [3, 2, 2, 3]

Note

  • An element in a block is selected, rejected, deleted, or kept based on the True or False value generated by that block on that element.
  • For a destructive behavior for select and reject or any method that one wants to enforce a change in the original array, a ! can be used at the end of the method i.e., select! and reject!

In this challenge, you have to complete the functions below using syntax as explained above.

Solution in ruby

Approach 1.

def select_arr(arr)
    arr.select {|i| i%2!=0}
end

def reject_arr(arr)
    arr.reject{|i| i%3==0}
end

def delete_arr(arr)
    arr.delete_if {|i| i<0}
end

def keep_arr(arr)
    arr.keep_if {|i| i>=0}
end

Approach 2.

def select_arr(arr)
  # select and return all odd numbers from the Array variable `arr`
    arr.select {|x| ! x.even? }
end

def reject_arr(arr)
  # reject all elements which are divisible by 3
    arr.reject {|x| x%3 == 0}
end

def delete_arr(arr)
  # delete all negative elements
    arr.delete_if {|x| x < 0} 
end

def keep_arr(arr)
  # keep all non negative elements ( >= 0)
    arr.keep_if {|x| x >= 0}
end

Approach 3.

def select_arr(arr)
  # select and return all odd numbers from the Array variable `arr`
    arr.select {|x| x.odd? }
end

def reject_arr(arr)
  # reject all elements which are divisible by 3
    arr.reject {|x| x % 3 == 0 }
end

def delete_arr(arr)
  # delete all negative elements
    arr.delete_if {|x| x < 0 }
end

def keep_arr(arr)
  # keep all non negative elements ( >= 0)
    arr.keep_if {|x| x >= 0 }
end