What I expect my code to do: print out
{ "pivot0"=>5, "left0"=>[5,0,1,2,3,4], "right0"=>[6,7,8,9] }
What my code does: print out { "pivot0"=>5}
I've used print to try to debug. Both hash["left0"] and hash["right0"] return [0,1,2,3,4,6,7,8,9]. Yet neither keys are showing up at all inside hash. And even if they did show up, they don't have the right numbers in them. What am I not understanding? As you can tell by the title, I don't even know what is causing the problem. Trying to write similar code with any one of those factors removed (if, hash, block, or push) seems to give me expected results, so I'm thoroughly confused.
source = [5,0,1,2,3,4,6,7,8,9]
hash = Hash.new([])
l=0
hash['pivot' + l.to_s] = source[0]
source.each_with_index do |e, i|
if i > 0
if e <= hash['pivot' + l.to_s]
puts "hit left on #{e}"
hash['left'+l.to_s] << e
else
puts "hit right on #{e}"
hash['right'+l.to_s] << e
end
end
end
print hash
Aucun commentaire:
Enregistrer un commentaire