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

  1. The neg_pos function takes in an array and an index as input. It returns the element of the array at the position 'index' from the end of the list. This is achieved by using negative indexing, where the last element of the array has an index of -1, the second to last element has an index of -2, and so on.
  2. The first_element function takes in an array as input. It returns the first element of the array by using the .first method.
  3. The last_element function takes in an array as input. It returns the last element of the array by using the .last method.
  4. The first_n function takes in an array and an integer 'n' as input. It returns the first 'n' elements of the array by using the .take(n) method.
  5. The drop_n function takes in an array and an integer 'n' as input. It drops the first 'n' elements of the array and returns the rest of the elements by using the .drop(n) method.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe