lesslucid (lesslucid) wrote in ruby_lang,
lesslucid
lesslucid
ruby_lang

Ruby Newbie: What's wrong with my code?

Hallo,
I've been having a bash at the Google AI contest, writing my AI in Ruby, with mixed results. I'm just learning the language and I think I have a long way to go but I'm enjoying the process. Anyway, I wrote the following method because I wanted to be able to sort one list according to the values in another list. (Basically, when I start the game, I want to measure how far away every planet is from my starting planets and then sort the list of planets according to how far away they are). This is what I came up with, but in some mysterious way I haven't been able to work out, it keeps crashing. Any ideas why?

def doublesorter(firstlist, secondlist)
  ind = 0
  while ind < firstlist.length - 2
    if firstlist[ind] < firstlist[ind + 1]
      ind += 1
    else
      firstlist.push(firstlist[ind])
      secondlist.push(secondlist[ind])
      firstlist.delete_at(ind)
      secondlist.delete_at(ind)
      ind = 0
    end
  end
  return firstlist, secondlist
end 


Any advice or help greatly appreciated.
  • Post a new comment

    Error

    default userpic
  • 10 comments