顯而易見的答案(至少在我看來)是使用 .key(value) 但是我不斷收到錯誤“未定義的密鑰方法”。我必須回傳最高值的第一個實體的鍵。這是我放入的;
def high(x)
alphabet = Hash.new(0)
count = 0
("a".."z").each do |char|
alphabet[char] = count = 1
end
words = Hash.new(0)
x.split(" ").each do |word|
count_a = 0
word.each_char do |chars|
if alphabet.has_key?(chars)
count_a = alphabet[chars]
end
end
words[word] = count_a
end
highest = words.sort_by { |key, value| value }
(highest[0][1]..highest[-1][1]).each do |val|
if val == highest[-1][1]
return highest.key(val)
else
return highest[-1][0]
end
end
end
我知道這是雜亂的代碼(我只有幾個月的時間學習編碼)。我面臨的問題具體在以下部分;
highest = words.sort_by { |key, value| value }
(highest[0][1]..highest[-1][1]).each do |val|
if val == highest[-1][1]
return highest.key(val)
else
return highest[-1][0]
end
end
因此,在我寫 'returnhighest.key(val)' 的地方,我希望它回傳等于最高“得分”單詞的單詞,但是它只是給了我未定義的方法錯誤。任何幫助將不勝感激。
uj5u.com熱心網友回復:
我設法解決了我的問題!再一次,我知道不是很優雅的代碼,但是如果其他人有類似的問題,這可能會有所幫助。(再次感謝所有指出它是陣列而不是哈希的人)
def high(x)
alphabet = Hash.new(0)
count = 0
("a".."z").each do |char|
alphabet[char] = count = 1
end
words = Hash.new(0)
x.split(" ").each do |word|
count_a = 0
word.each_char do |chars|
if alphabet.has_key?(chars)
count_a = alphabet[chars]
end
end
words[word] = count_a
end
highest = words.sort_by { |key, value| value }.to_h
highest_score = highest.values.last
best = []
highest.each_value do |value|
if value = highest_score
best << highest.key(value)
end
end
return best[0]
end
所以這段代碼背后的總體思路是引數“x”是一個字串,字母的得分為 a = 1 b = 2 等。此代碼回傳得分最高的單詞。如果有多個單詞得分最高,它將回傳這些單詞中最先出現的單詞。
uj5u.com熱心網友回復:
使用可列舉和字串方法使魔術發生
我很高興你已經回答了你自己的問題。同時,如果您不關心在進行時跟蹤所有單詞,或者不關心每個單詞得分的運行串列,那么您可以通過讓 Ruby 做更多事情來使之變得更簡單、更快且記憶體占用更少為您完成作業,然后回傳得分最高的專案。
Ruby 有很多內置方法可以讓這樣的作業更容易,而且它的核心方法通常是高度優化的。這是一個包含七行代碼(不包括注釋)的解決方案,它利用Array 繼承的Enumerable#zip、Enumerable#sum和Enumerable#max_by方法來完成大部分繁重的作業。
Hash#merge幫助我們處理一些邊緣情況,比如復合詞中的空格或破折號。然后,在String#chars和String#downcase 的幫助下!,這幾乎就像 Ruby 為我們做的一樣!
在 Ruby 3.0.2 中:
# Map your numerical score to each letter and store in a Hash.
#
# @note We can prepend a value of zero for spaces and other special
# characters, too!
ALPHA_SCORES = {" " => 0, "-" => 0}.merge (?a..?z).zip(1..26).to_h
# Convert all words to lowercase, and return the highest-scoring word
# and its score as an Array.
def find_highest_scoring_word_in *words
words.flatten.map do |word|
word.downcase!
[word, word.chars.sum { ALPHA_SCORES[_1] }]
end.max_by(&:last)
end
這種方法提供了很大的靈活性。例如,您可以通過以下所有方式呼叫它,它將回傳正確的結果。
# pass a single Array of String objects
find_highest_scoring_word_in %w[foo bar baz]
#=> ["foo", 36]
# pass two Array of String arguments
find_highest_scoring_word_in %w[foo bar baz], %w[quux wuuble]
#=> ["wuuble", 84]
# pass in three separate String arguments
find_highest_scoring_word_in 'alpha', 'beta', 'gamma'
#=> ["alpha", 38]
# Pass in expressions that evaluate to a String. The first creates 100
# letter a's, and the second 4 z's. Since `z` is worth 26 points, and
# `a` only 1, it's "zzzz" for the win!
find_highest_scoring_word_in 'a' * 100, 'z' * 4
#=> ["zzzz", 104]
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/324617.html
下一篇:如果缺少所需資訊,請鎖定tk按鈕
