一個多維串頻陣列我要問用戶的字符串,程序應該命令字符串,並將其轉換成一個數組這樣排序在Ruby中
[["Letter", frequence]]
[["a", 4],["b", 3],["c",3]]
在情況下等於頻率,順序按字母順序排列。
我有這樣的代碼
puts "Write what you want"
text = gets.chomp
text.downcase!
words = text.split("")
print words
frequencies = Hash.new(0)
words.each do |word|
frequencies[word] += 1
end
frequencies = frequencies.sort_by {|x,y| y}.reverse!
frequencies = frequencies.sort_by {|x| x[1]}
frequencies.each do |word, freq|
puts word + " " + freq.to_s
end
你的問題是什麼? – sawa