Hackerrank Ruby Array - Index, Part 2 Solution
2 min read

Hackerrank Ruby Array - Index, Part 2 Solution

Hackerrank Ruby Array - Index, Part 2 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}

Here are some other ways to access array objects in Ruby.

To access the elements from the end of the list, we can use negative indices.

For the array,  arr = [9, 5, 1, 2, 3, 4, 0, -1] > arr[-1] => -1

  • The first element of the array can be accessed using > arr.first => 9
  • The last element of the array can be accessed using > arr.last => -1
  • The first n elements of the array can be accessed using arr.take(3) => [9, 5, 1]
  • Everything but the first n elements of the array can be accessed using arr.drop(3) => [2, 3, 4, 0, -1]

In this challenge, you have to use the syntax as explained above and complete the functions accordingly.

Solution in ruby

Approach 1.

def neg_pos(arr, index)
    # return the element of the array at the position `index` from the end of the list
    # Clue : arr[-index]
    arr[-index]
end

def first_element(arr)
    # return the first element of the array
    arr.first
end

def last_element(arr)
    # return the last element of the array
    arr.last
end

def first_n(arr, n)
    # return the first n elements of the array
    arr.take(n)
end

def drop_n(arr, n)
    # drop the first n elements of the array and return the rest
    arr.drop(n)
end

Approach 2.

def neg_pos(arr, index)
    # return the element of the array at the position `index` from the end of the list
    # Clue : arr[-index]
  arr[-index]
end

def first_element(arr)
    # return the first element of the array
    # arr.first
  arr.first
end

def last_element(arr)
    # return the last element of the array
    # arr.last
  arr.last
end

def first_n(arr, n)
    # return the first n elements of the array
  arr.take(n)
end

def drop_n(arr, n)
    # drop the first n elements of the array and return the rest
  arr.drop(n)
end

Approach 3.

def neg_pos(arr, index)
    # return the element of the array at the position `index` from the end of the list
    # Clue : arr[-index]
    arr[-index]
end

def first_element(arr)
    # return the first element of the array
    # arr.first
    arr.first
end

def last_element(arr)
    # return the last element of the array
    # arr.last
    arr.last
end

def first_n(arr, n)
    # return the first n elements of the array
    arr.take(n)
end

def drop_n(arr, n)
    # drop the first n elements of the array and return the rest
    arr.drop(n)
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.

×