Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 查找字符串中最常出现的字符/字母_Ruby_String - Fatal编程技术网

Ruby 查找字符串中最常出现的字符/字母

Ruby 查找字符串中最常出现的字符/字母,ruby,string,Ruby,String,试图获取字符串中出现次数最多的字母 到目前为止: puts "give me a string" words = gets.chomp.split counts = Hash.new(0) words.each do |word| counts[word] += 1 end 除了请求字符串外,不会再运行。我做错了什么?这应该可以: puts "give me a string" result = gets.chomp.split(//).reduce(Hash.new(0)) { |h,

试图获取字符串中出现次数最多的字母

到目前为止:

puts "give me a string"
words = gets.chomp.split
counts = Hash.new(0)
words.each do |word|
  counts[word] += 1
end
除了请求字符串外,不会再运行。我做错了什么?

这应该可以:

puts "give me a string"
result = gets.chomp.split(//).reduce(Hash.new(0)) { |h, v| h.store(v, h[v] + 1); h }.max_by{|k,v| v}
puts result.to_s
输出:

@Alan ➜  test rvm:(ruby-2.2@europa)  ruby test.rb
give me a string
aa bbb cccc ddddd
["d", 5]
或在irb中:

:008 > 'This is some random string'.split(//).reduce(Hash.new(0)) { |h, v| h.store(v, h[v] + 1); h }.max_by{|k,v| v}
 => ["s", 4]

如果您在irb中运行此操作,那么计算机可能会认为您键入的ruby代码是要分析的文本:

irb(main):001:0> puts "give me a string"
give me a string
=> nil
irb(main):002:0> words = gets.chomp.split
counts = Hash.new(0)
words.each do |word|
counts[word] += 1
end=> ["counts", "=", "Hash.new(0)"]
irb(main):003:0> words.each do |word|
irb(main):004:1* counts[word] += 1
irb(main):005:1> end
NameError: undefined local variable or method `counts' for main:Object
    from (irb):4:in `block in irb_binding'
    from (irb):3:in `each'
    from (irb):3
    from /Users/agrimm/.rbenv/versions/2.2.1/bin/irb:11:in `<main>'
irb(main):006:0> 
给予


然后,您可以处理这样一个事实,即
拆分本身并不是您想要的。:)

您可以立即处理整个字符串,而不是逐字计数

  str = gets.chomp
  hash = Hash.new(0)
  str.each_char do |c|
    hash[c] += 1 unless c == " " #used to filter the space
  end
在获得字母数后,您可以使用找到计数最高的字母

max = hash.values.max
然后将其与散列中的键匹配,就完成了:)

或者简化上述方法

hash.max_by{ |key,value| value }
其紧凑形式为:

  hash = Hash.new(0)
  gets.chomp.each_char { |c| hash[c] += 1 unless c == " " }
  puts hash.max_by{ |key,value| value }

这将返回给定字符串中出现的最高字符:

puts "give me a string"

characters = gets.chomp.split("").reject { |c| c == " " }
counts = Hash.new(0)

characters.each { |character| counts[character] += 1 }

print counts.max_by { |k, v| v }

你能根据你的问题调整一个或多个答案吗?单词的概念或拆分方法如何与你的目标相关?我想我需要先用单词拆分字符串,然后才能单独拆分字母。我是个新手,正处于“握手/蜜月阶段”。除了尽可能多地学习之外,我现在真的没有什么目标了::(ruby example.rb example.rb:3:语法错误,意外的“|”,预期的“}{h,v | h.store(v,h[v]+1);h}示例。rb:3:语法错误,意外的“|”,预期的“={h,v | h.store(v,h[v]+1);h}我正在bash中运行此命令!@blubg您是否正在显示分析结果(例如
put counts
):(没有返回任何内容。唯一的错误是它没有输出结果。向最后一行添加
puts
,就像上面的一样。请在对工作答案进行向下表决之前尝试调试一下。@blubg刚刚更新了代码。它缺少
puts
print
语句-easy enough to fix:)我选择了
打印
,因此返回的结果显示在
[x,5]
format-比puts IMO好看一点。希望能有帮助!我没有在这条帖子中否决任何东西,也没有质疑为什么会有人这样做。我需要125个声誉,然后才能这样做:/另外,感谢编辑。我花了很长时间才弄明白为什么,但我还是设法做到了!)很遗憾,我无法向您发送私人消息。关于您为我实现此目的的方法,我有几个问题re:max=hash.values.max(您使用了max两次,所以我对max变量和方法很好奇)还有,有没有办法合并“mode”?啊,这么多东西>\u是的,我用它来说明你的想法:)max=hash.values.max`;其中,第一个
max
是存储hash.values.max结果的变量。第二个
max
是一个从hash.values的结果数组中查找最大值的方法;方法
values
返回散列中所有值的数组:)希望这对您有所启发,呵呵
hash.max_by{ |key,value| value }
  hash = Hash.new(0)
  gets.chomp.each_char { |c| hash[c] += 1 unless c == " " }
  puts hash.max_by{ |key,value| value }
puts "give me a string"

characters = gets.chomp.split("").reject { |c| c == " " }
counts = Hash.new(0)

characters.each { |character| counts[character] += 1 }

print counts.max_by { |k, v| v }