我有一个学校作业,我必须在Ruby中找到给定字符串中最长的相邻相等字符。我的程序没有最后一个循环正常工作,但一旦我添加它给了我错误:
(repl):47: syntax error, unexpected keyword_end
(repl):53: syntax error, unexpected end-of-input, expecting keyword_end
puts longestRun
^
这是我的代码
puts 'What is your string?'
givenString = gets.chomp
def maxBlock(str)
maxRun = 0
currentRun = 1
characterCounter = 1
if str.length == 0
maxRun = 0
#If no input, longest run is zero
elsif str.length == 1
maxRun = 1
#If string is one character, longest run is 1
elsif str.length == 2 and str[characterCounter] != str[characterCounter + 1]
maxRun = 1
#if string is two chars and they do not equal, longest run is 1
elsif str.length == 3 and str[0] != str[1] and str[1] != str[2]
maxRun = 1
#if string is three chars and they do not equal, longest run is 1
else
str.each_char do|st|
#Go through each char, compare it to the next, find longest run
if st == str[characterCounter]
currentRun++
if currentRun > maxRun
maxRun = currentRun
end
else
currentRun = 1
end
characterCounter++
end
end
end
longestRun = maxBlock(givenString)
puts longestRun
编辑:我是一名高中生,只具备编程基础知识。
编辑:我刚犯了一些愚蠢的错误。我感谢大家的帮助。这是我的工作程序,没有使用太复杂的东西。
puts 'What is your string?'
givenString = gets.chomp
def maxBlock(str)
maxRun = 0
currentRun = 1
characterCounter = 0
if str.length == 0
maxRun = 0
#If no input, longest run is zero
elsif str.length == 1
maxRun = 1
#If string is one character, longest run is 1
elsif str.length == 2 and str[characterCounter] != str[characterCounter + 1]
maxRun = 1
#if string is two chars and they do not equal, longest run is 1
elsif str.length == 3 and str[0] != str[1] and str[1] != str[2]
maxRun = 1
#if string is three chars and they do not equal, longest run is 1
else
characterCounter += 1
str.each_char do|st|
#Go through each char, compare it to the next, find longest run
if st == str[characterCounter]
currentRun += 1
if currentRun > maxRun
maxRun = currentRun
end
else
currentRun = 1
end
characterCounter += 1
end
end
return maxRun
end
longestRun = maxBlock(givenString)
puts longestRun
答案 0 :(得分:1)
有这方面的算法,但Ruby提供了一些不错的快捷方式。例如:
def longest_string str
str.scan(/((\p{Alnum})\2+)/).collect { |grp1, grp2| grp1 }.sort_by(&:size).last
end
longest_string 'foo baaar quuuux'
#=> "uuuu"
这基本上只捕获重复字符的所有运行,按长度对捕获的子字符串进行排序,然后返回长度排序数组的最后一个元素。
如果您要进行辅助排序,例如先按长度排序,然后按字母顺序排序,则可以使用Enumerable#sort_by的块形式替换Enumerable#sort。例如:
def longest_string str
str.scan(/((\p{Alnum})\2+)/).
collect { |grp1, grp2| grp1 }.
sort {|a, b| [a.size, a] <=> [b.size, b] }.
last
end
longest_string 'foo quux baar'
#=> "uu"
答案 1 :(得分:1)
这是你可以做到的一种方式。
str = "111 wwwwwwwwaabbbbbbbbbbb$$$$****"
r = /
(.) # Match any character in capture group 1
\1* # Match the contents of capture group 1 zero or more times
/x # Free-spacing regex definition mode
str.gsub(r).max_by(&:size)
#=> "bbbbbbbbbbb"
我使用了String#gsub的形式而没有第二个参数或块,因为它返回一个生成与正则表达式匹配的字符串的枚举器。然后我将该枚举器链接到方法Enumerable#max_by以找到最长的连续字符串。换句话说,我只使用gsub
来生成匹配而不是执行替换。
当然可以写str.gsub(/(.)\1*/).max_by(&:size)
。
答案 2 :(得分:0)
以下是适用于所有情况的简化版本:
puts 'What is your string?'
given_string = gets.chomp
def max_block(str)
max_run = 0
current_run = 1
str.each_char.with_index do |st, idx|
if st == str[idx + 1]
current_run += 1
else
current_run = 1
end
max_run = current_run if current_run > max_run
end
max_run
end
longest_run = max_block(given_string)
puts longest_run
你走在正确的轨道上,但Ruby可以让你的事情变得更轻松。注意with_index
如何摆脱很多复杂性。迭代器,哦,是的。
我还将您的方法名称和变量更改为camel_case。
快乐的编码!