Sam Morency Sam Morency - 2 months ago 5
Ruby Question

Longest Run Program In Ruby

I have a school assignment where i have to find the longest run of adjacent equal characters in a given string with Ruby. My program works fine without the last loop, but once i added it gave me the error:

(repl):47: syntax error, unexpected keyword_end
(repl):53: syntax error, unexpected end-of-input, expecting keyword_end
puts longestRun
^


Here is my Code

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


EDIT: I am a highschool student, and only have a base knowledge of programming.

EDIT: I just made a few stupid mistakes. I appreciate everyone's help. Here is my working program without the use of anything too complicated.

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

Answer

Your code still doesn't work. Try it with the string "cc"(returns 1). Here is a modified version that should work in all cases:

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

You were on the right track but Ruby can make things a lot easier for you. Notice how with_index gets rid of a lot of the complexity. Iterators are a happy thing.

I also changed your method name and variables to camel_case.

Happy coding!

Comments