私はこのルビーモンクチャレンジhttp://rubymonk.com/learning/books/1-ruby-primer/problems/154-permutationsをやって、この溶液を思い付いた:ルビーモンク数シャッフル - ソリューションの比較
def number_shuffle(number)
string_rep = number.to_s
ary = string_rep.split('').permutation(number.to_s.length).to_a
result = []
ary.each do |i|
x = i.join.to_i
result << x
end
return result.sort
end
はまた、ここではRubyのモンクのソリューションです:
def number_shuffle(number)
no_of_combinations = number.to_s.size == 3 ? 6 : 24
digits = number.to_s.split(//)
combinations = []
combinations << digits.shuffle.join.to_i while combinations.uniq.size!=no_of_combinations
combinations.uniq.sort
end
私はルビーを感知モンクの解決策はルビースタイルではありますが、私はその理由をよりよく理解したいと思います。 あなたの考えを分かち合い、いくつかのヒントを教えてください。 事前に感謝します。
リンクだけでなく、質問内で問題を追加する必要があります。 –
'return'キーワードは不要です。 –
おかげさまで、将来完全な問題を投稿することを心掛けています。 – do20i