2

Trying to make a method skip_animals that takes an animals array and a skip integer and returns an array of all elements except first skip number of items.

input: skip_animals(['leopard', 'bear', 'fox', 'wolf'], 2)

expected output: ["2:fox", "3:wolf"]

   def skip_animals(animals, skip)
        arr = Array.new
        animals.each_with_index{|animal, index| arr.push("#{animal}:#{index}") }
        puts arr.drop(skip)
    end

This instead puts each output on a separate line and doesn't add them to the array arr. I thought the arr.push would add them correctly. What do I have to do to get the elements added to the array?

I want to use these methods, not map or something more advanced. I need to tinker with this each_with_index line, not overhaul it.

(This is a challenge on Hackerrank, so it uses STDIN and STDOUT)

EDIT

Here is my updated code with p instead of puts. It's giving me a weird output of two different arrays, not sure why.

def skip_animals(animals, skip)
    arr = Array.new
    animals.each_with_index{|animal, index| arr.push("#{index}:#{animal}") }
    p arr.drop(skip)
end

This gives me two lines of output:

["3:panda", "4:tiger", "5:deer"]
["0:leopard", "1:bear", "2:fox", "3:wolf", "4:dog", "5:cat"]

I'm assuming the top is the correct array, but I don't get why the second is printing also, or why it has a different set of animals.

2
  • Why can't you use map? I hope you are not going to say this is homework and you are trying to let someone else do the homework for you. Commented Jun 26, 2015 at 5:06
  • @sawa lol its an online problem on a learn to code site, not homework. So the goal is to learn why this isn't working. Commented Jun 26, 2015 at 5:08

2 Answers 2

1

Use p instead of puts.

irb(main):001:0> puts ['1', '2']
1
2
=> nil
irb(main):002:0> p ['1', '2']
["1", "2"]

According to the documentation, puts:

Writes the given objects to ios as with IO#print. Writes a record separator (typically a newline) after any that do not already end with a newline sequence. If called with an array argument, writes each element on a new line. If called without arguments, outputs a single record separator.


BTW, I would code like this (using Enumerable#map + returning result instead of printing inside the function):

def skip_animals(animals, skip)
  animals.drop(skip).each_with_index.map { |animal, index|
    ("#{index + skip}:#{animal}")
  }
end

p skip_animals(['leopard', 'bear', 'fox', 'wolf'], 2)
Sign up to request clarification or add additional context in comments.

4 Comments

This did get an array printing, but something in the code is still wrong. Question edited above, any ideas?
@VirgeAssault, "#{animal}:#{index}", index and animal should be swapped. Aren't they?, Check the last code in the answer.
yes, but the error is bc I'm getting two lines of output for some reason. Can you check the edited code w/ output and see if you know why
@VirgeAssault, I cannot reproduce the symptom you have. Is there any other call except skip_animals(['leopard', 'bear', 'fox', 'wolf'], 2)?
0

just remove puts remove form this line puts arr.drop(skip)

def skip_animals(animals, skip)
    arr = Array.new
    animals.each_with_index{|animal, index| arr.push("#{animal}:#{index}") }
    arr.drop(skip)
end

1 Comment

Without the puts nothing is put out. I think I need to use p instead, but not sure about the STDIN stuff from HackerRank? Any ideas?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.