理解Ruby需要帮助:num_重复返回多次出现的字母数
我需要帮助理解下面代码的一小部分。返回字符串中多次出现的字母数,同时假定字符串中的字符仅以小写形式出现理解Ruby需要帮助:num_重复返回多次出现的字母数,ruby,Ruby,我需要帮助理解下面代码的一小部分。返回字符串中多次出现的字母数,同时假定字符串中的字符仅以小写形式出现 def num_repeats(string) counts = [] str_idx = 0 while str_idx < string.length letter = string[str_idx] counts_idx = 0 while counts_idx < counts.length if counts[count
def num_repeats(string)
counts = []
str_idx = 0
while str_idx < string.length
letter = string[str_idx]
counts_idx = 0
while counts_idx < counts.length
if counts[counts_idx][0] == letter
counts[counts_idx][1] += 1
break
end
counts_idx += 1
end
if counts_idx == counts.length
#add letters if not found first time.
counts.push([letter, 1])
end
str_idx += 1
end
num_repeats = 0
counts_idx = 0
while counts_idx < counts.length
if counts[counts_idx][1] > 1
num_repeats += 1
end
counts_idx += 1
end
return num_repeats
end
我不明白计数
我知道它们不是来自javaq.Q的2d数组 我会让其他人回答你的问题,但我不能不告诉你,你所展示的是可怕的Ruby代码。它看起来像是由一个无法摆脱程序思维定势的人写的 Ruby允许您非常轻松地计算字符串中多次出现的字母数 代码
def count_of_repeated_letters(str)
str.gsub(/[[:punct:] ]/, '').
downcase.
each_char.
with_object(Hash.new(0)) { |c,h| h[c] += 1 }.
count { |_,count| count > 1 }
end
str = "Now is the time; for all good Rubiests; to ring in the New Year"
count_of_repeated_letters(str)
#=> 12
示例
def count_of_repeated_letters(str)
str.gsub(/[[:punct:] ]/, '').
downcase.
each_char.
with_object(Hash.new(0)) { |c,h| h[c] += 1 }.
count { |_,count| count > 1 }
end
str = "Now is the time; for all good Rubiests; to ring in the New Year"
count_of_repeated_letters(str)
#=> 12
散列的默认值
Hash.new(0)
创建一个默认值为零的空哈希。让我解释一下“默认值”的含义。首先阅读类方法的文档
当Ruby遇到表达式h[k]+=1时,她要做的第一件事就是将其展开为
h[k] = h[k] + 1
如果h
有一个键k
,则等式右侧的h[k]
是当前计数,因此它将递增1
如果h
没有键k
,则她将等式右侧的h[k]
替换为默认值,因此表达式变为
h[k] = 0 + 1
也就是说,h[k]
被设置为等于1。如果您想知道,在本例中,等式左侧的h[k]
为什么没有设置为等于1,请记住,此表达式实际上是为
只有方法使用默认值,而不是方法
单步执行代码
def count_of_repeated_letters(str)
str.gsub(/[[:punct:] ]/, '').
downcase.
each_char.
with_object(Hash.new(0)) { |c,h| h[c] += 1 }.
count { |_,count| count > 1 }
end
str = "Now is the time; for all good Rubiests; to ring in the New Year"
count_of_repeated_letters(str)
#=> 12
对于上述str
的值,步骤如下
s = str.gsub(/[[:punct:] ]/, '')
#=> "NowisthetimeforallgoodRubieststoringintheNewYear"
这将与正则表达式一起使用,通过将标点字符和空格转换为空字符串来删除它们[[:punct:]
是一个匹配标点符号和空格的字符类。继续
t = s.downcase
#=> "nowisthetimeforallgoodrubieststoringinthenewyear"
d = t.each_char
#=> #<Enumerator: "nowisthetimeforallgoodrubieststoringinthenewyear":each_char>
e = d.with_object(Hash.new(0))
#=> #<Enumerator: #<Enumerator:
# "nowisthetimeforallgoodrubieststoringinthenewyear":each_char>: with_object({})>
f = e.each { |c,h| h[c] += 1 }
#=> {"n"=>4, "o"=>5, "w"=>2, "i"=>5, "s"=>3, "t"=>5, "h"=>2, "e"=>6, "m"=>1,
# "f"=>1, "r"=>4, "a"=>2, "l"=>2, "g"=>2, "d"=>1, "u"=>1, "b"=>1, "y"=>1}
此哈希显示为枚举器生成的每个数组的第二个元素,将在执行计算时生成
继续
t = s.downcase
#=> "nowisthetimeforallgoodrubieststoringinthenewyear"
d = t.each_char
#=> #<Enumerator: "nowisthetimeforallgoodrubieststoringinthenewyear":each_char>
e = d.with_object(Hash.new(0))
#=> #<Enumerator: #<Enumerator:
# "nowisthetimeforallgoodrubieststoringinthenewyear":each_char>: with_object({})>
f = e.each { |c,h| h[c] += 1 }
#=> {"n"=>4, "o"=>5, "w"=>2, "i"=>5, "s"=>3, "t"=>5, "h"=>2, "e"=>6, "m"=>1,
# "f"=>1, "r"=>4, "a"=>2, "l"=>2, "g"=>2, "d"=>1, "u"=>1, "b"=>1, "y"=>1}
最后,
f.count { |_,count| count > 1 }
#=> 12
看。在这最后一步中,我可以编写h.count{k,count{count>1}
,k
和count
作为h
的键值对,但当块计算中未使用块变量时(此处k
),通常用局部变量
或(此处)替换它\u k
替代方法
下面是你如何使用它的方法
我会让其他人回答你的问题,但我不能不告诉你,你所展示的是可怕的Ruby代码。它看起来像是由一个无法摆脱程序思维定势的人写的
Ruby允许您非常轻松地计算字符串中多次出现的字母数
代码
def count_of_repeated_letters(str)
str.gsub(/[[:punct:] ]/, '').
downcase.
each_char.
with_object(Hash.new(0)) { |c,h| h[c] += 1 }.
count { |_,count| count > 1 }
end
str = "Now is the time; for all good Rubiests; to ring in the New Year"
count_of_repeated_letters(str)
#=> 12
示例
def count_of_repeated_letters(str)
str.gsub(/[[:punct:] ]/, '').
downcase.
each_char.
with_object(Hash.new(0)) { |c,h| h[c] += 1 }.
count { |_,count| count > 1 }
end
str = "Now is the time; for all good Rubiests; to ring in the New Year"
count_of_repeated_letters(str)
#=> 12
散列的默认值
Hash.new(0)
创建一个默认值为零的空哈希。让我解释一下“默认值”的含义。首先阅读类方法的文档
当Ruby遇到表达式h[k]+=1时,她要做的第一件事就是将其展开为
h[k] = h[k] + 1
如果h
有一个键k
,则等式右侧的h[k]
是当前计数,因此它将递增1
如果h
没有键k
,则她将等式右侧的h[k]
替换为默认值,因此表达式变为
h[k] = 0 + 1
也就是说,h[k]
被设置为等于1。如果您想知道,在本例中,等式左侧的h[k]
为什么没有设置为等于1,请记住,此表达式实际上是为
只有方法使用默认值,而不是方法
单步执行代码
def count_of_repeated_letters(str)
str.gsub(/[[:punct:] ]/, '').
downcase.
each_char.
with_object(Hash.new(0)) { |c,h| h[c] += 1 }.
count { |_,count| count > 1 }
end
str = "Now is the time; for all good Rubiests; to ring in the New Year"
count_of_repeated_letters(str)
#=> 12
对于上述str
的值,步骤如下
s = str.gsub(/[[:punct:] ]/, '')
#=> "NowisthetimeforallgoodRubieststoringintheNewYear"
这将与正则表达式一起使用,通过将标点字符和空格转换为空字符串来删除它们[[:punct:]
是一个匹配标点符号和空格的字符类。继续
t = s.downcase
#=> "nowisthetimeforallgoodrubieststoringinthenewyear"
d = t.each_char
#=> #<Enumerator: "nowisthetimeforallgoodrubieststoringinthenewyear":each_char>
e = d.with_object(Hash.new(0))
#=> #<Enumerator: #<Enumerator:
# "nowisthetimeforallgoodrubieststoringinthenewyear":each_char>: with_object({})>
f = e.each { |c,h| h[c] += 1 }
#=> {"n"=>4, "o"=>5, "w"=>2, "i"=>5, "s"=>3, "t"=>5, "h"=>2, "e"=>6, "m"=>1,
# "f"=>1, "r"=>4, "a"=>2, "l"=>2, "g"=>2, "d"=>1, "u"=>1, "b"=>1, "y"=>1}
此哈希显示为枚举器生成的每个数组的第二个元素,将在执行计算时生成
继续
t = s.downcase
#=> "nowisthetimeforallgoodrubieststoringinthenewyear"
d = t.each_char
#=> #<Enumerator: "nowisthetimeforallgoodrubieststoringinthenewyear":each_char>
e = d.with_object(Hash.new(0))
#=> #<Enumerator: #<Enumerator:
# "nowisthetimeforallgoodrubieststoringinthenewyear":each_char>: with_object({})>
f = e.each { |c,h| h[c] += 1 }
#=> {"n"=>4, "o"=>5, "w"=>2, "i"=>5, "s"=>3, "t"=>5, "h"=>2, "e"=>6, "m"=>1,
# "f"=>1, "r"=>4, "a"=>2, "l"=>2, "g"=>2, "d"=>1, "u"=>1, "b"=>1, "y"=>1}
最后,
f.count { |_,count| count > 1 }
#=> 12
看。在这最后一步中,我可以编写h.count{k,count{count>1}
,k
和count
作为h
的键值对,但当块计算中未使用块变量时(此处k
),通常用局部变量
或(此处)替换它\u k
替代方法
下面是你如何使用它的方法
我是个初学者,还在努力学习ruby!谢谢你给我看这个,并给我提供了新的信息。我很抱歉没有提供更多关于代码应该做什么的信息。返回字符串中多次出现的字母数。再次感谢您对代码的详细解释。我编辑了我的答案,以符合您对问题的澄清。您提供的解释确实帮助我理解了您的代码。还感谢您为我提供ruby文档,帮助我理解您的代码。我是一名初学者,仍在努力学习ruby!谢谢你给我看这个,并给我提供了新的信息。我很抱歉没有提供更多关于代码应该做什么的信息。返回字符串中多次出现的字母数。再次感谢您对代码的详细解释。我编辑了我的答案,以符合您对代码的澄清