我是紅寶石新手。我正在嘗試創建一個字母計數器。我的預期輸出應該是 [2,"I"] 但我一直得到 [3,"D]。任何有助于理解我出錯的地方都會非常有幫助,謝謝。
class LetterCounter
def initialize(text)
@text = text
end
def calculate_most_common()
counter = Hash.new(1)
most_common = nil
most_common_count = 1
@text.chars.each do |char|
next unless is_letter?(char)
counter[char] = (counter[char] || 1) 1
if counter[char] > most_common_count
most_common = char
most_common_count = counter[char]
end
end
return [most_common_count, most_common]
end
private
def is_letter?(letter)
return letter =~ /[a-z]/i
end
end
counter = LetterCounter.new("Digital Punk")
p counter.calculate_most_common
# Intended output:
# [2, "i"]
uj5u.com熱心網友回復:
嘗試:
class LetterCounter
def initialize(text)
@text = text
end
def calculate_most_common()
arr=@text.scan(/[a-z]/i)
arr.each_with_object(Hash.new(0)) { |n,h| h[n] = 1 }.max_by(&:last)
end
end
counter = LetterCounter.new("Digital Punk")
p counter.calculate_most_common
印刷:
["i", 2]
如果您想修復您的問題,請嘗試:
class LetterCounter
def initialize(text)
@text = text
end
def calculate_most_common()
counter = Hash.new(0)
most_common = nil
most_common_count = 0
@text.chars.each do |char|
next unless is_letter?(char)
counter[char] = 1
if counter[char]>most_common_count
most_common=char
most_common_count=counter[char]
end
end
return [most_common_count, most_common]
end
private
def is_letter?(letter)
return letter =~ /[a-z]/i
end
end
counter = LetterCounter.new("Digital Punk")
p counter.calculate_most_common
uj5u.com熱心網友回復:
class LetterCounter
def initialize(text)
@text = text
end
def calculate_most_common()
counter = Hash.new(0) # wrong initialization in your code
most_common = nil
most_common_count = 0
@text.chars.each do |char|
next unless is_letter?(char)
counter[char] = (counter[char] || 1) 1
puts "#{char} #{counter[char]}"
if counter[char] > most_common_count
puts "most common: #{most_common_count} #{char} #{counter[char]}"
most_common = char
most_common_count = counter[char] # error in your code
end
end
return [most_common_count, most_common]
end
private
def is_letter?(letter)
return letter =~ /[a-z]/i
end
end
counter = LetterCounter.new("Digital Punk")
p counter.calculate_most_common
uj5u.com熱心網友回復:
初始計數器為 1。這些應該為 0,因為無論如何您都在回圈內部遞增。
在第一次迭代中 - 哈希 counter[char] 被初始化為 1( From Hash.new(1) ),然后 counter[char] 在第一個回圈中增加到 1 并且 most_common_count 也增加了 1。這導致 D有值 3。
因為 most_common_count 已經是 3 - 回圈將不再進入 if 條件 - 因為其他字符只會達到 2( 1 來自 Hash.new 和 1 來自 counter[char] 1 )
if 條件是 > most_common_count 而不是 >= - 因此即使 i 達到 3 - 條件也不會執行。
因此輸出 [3, 'D']
試試這個:
類 LetterCounter def 初始化(文本)@text = 文本結束
def calculate_most_common() counter = Hash.new(0) most_common = nil most_common_count = 0 @text.chars.each do |char| next unless is_letter?(char) puts char " ==> " counter[char].to_s # To know the value on each iteration counter[char] = 1 if counter[char] > most_common_count most_common = char most_common_count = counter[char] puts [most_common_count, most_common] # To know when the if condition is executed end end return [most_common_count, most_common] end private def is_letter?(letter) return letter =~ /[a-z]/i end end counter = LetterCounter.new("Digital Punk") p counter.calculate_most_common
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/510567.html
標籤:红宝石循环哎呀调试
下一篇:無法附加到行程。一般例外
