1つの課題は、逆に、下記のリンクリストの:data
値を印刷することである:私の方法は、上記のコードを渡されたとき{:data=>3, :next=>{:data=>2, :next=>{:data=>1, :next=>nil}}}
だから、それは返す必要がありますprint_linked_list_in_reverse関数が機能しないのはなぜですか?私がやっているRubyの過程で
1
2
3
ここで私の試みは、上記のコードでは動作しません。私はなぜ理解できない、と誰かが私が間違ってやっているかを説明することができれば、私はそれを感謝したい:
def print_list_in_reverse(hash)
if hash[:next].nil? #i.e. is this the final list element?
print "#{hash[:data]}\n"
return true
else
#as I understand it, the next line should run the method on `hash[:next]` as well as checking if it returns true.
print "#{hash[:data]}\n" if print_list_in_reverse(hash[:next])
end
end
ここで、それはあなたが私のミスを見つけることができます場合には解決策は、です。
def print_list_in_reverse(list)
return unless list
print_list_in_reverse list[:next]
puts list[:data]
end
ありがとうございます。
ありがとう@pjs、最初の2行だけで十分でしたが、追加情報は非常に有益であり、非常に感謝しています。 – adc17
ようこそ。私はGuido van Rossumが言っていることにかかわらず、何かをする方法は複数あり、代替案を探索するのは楽しいから、残りの部分を追加しました。 – pjs