Skip to content
Programming101
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

HackerRank Ruby – Enumerable – each_with_index problem solution

YASH PAL, 31 July 2024

In this HackerRank Ruby – Enumerable – each_with_index problem solution we learned about each method is central to all of the methods provided by the Enumerable class. One of such useful methods is each_with_index which allows you to iterate over items along with an index keeping count of the item.

For example,

> colors = [‘red’, ‘green’, ‘blue’]

> colors.each_with_index { |item, index| p “#{index}:#{item}” }

“0:red”

“1:green”

“2:blue”

As you can note, the counting of items starts from 0.

In this challenge, your task is to complete the skip_animals method that takes an animals array and a skip integer and returns an array of all elements except the first skip number of items as shown in the example below.

For example,

> skip_animals([‘leopard’, ‘bear’, ‘fox’, ‘wolf’], 2)

=> [“2:fox”, “3:wolf”]

It is guaranteed that number of items in the animal’s array is greater than the value of skip.

HackerRank Ruby - Enumerable - each_with_index problem solution

Problem solution.

def skip_animals(animals, skip)
  # Your code here
    ret_array = []
    animals.each_with_index { |item, index| ret_array.push("#{index}:#{item}") if index >= skip }
    ret_array
end

Second solution.

def skip_animals(animals, skip)
  arr = []
    animals.each_with_index do |animal, ind|
        if ind >= skip
            arr << "#{ind}:#{animal}"
        end
    end
    arr
end

coding problems solutions Ruby Solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes