Programming Questions

  • Newest
  • Popular Tags
  • Ask Question
  • Run Length in Ruby
    Hello all! I keep getting an error when trying to solve this Run Length medium difficulty question. Any ideas? Thanks in advance!! def RunLength(str) counter = 0 letter = str[0] ans = [] str.each_char do |char| if char == letter counter += 1 elsif char != letter ans << counter ans << letter letter = char counter = 1 end end ans << counter ans << letter ans.join("").to_s end
    juljun14 posted this question on 4/2/14 | ruby
    Answers
  • +
  • 1
  • -
  • Swarleh: If it's a different letter, I change the value of the counter back to 1. If I do counter +=1, it will add 1 to whatever value it's already at. Frozen: Thanks so much for checking my program. I will e-mail Coderbyte to see what's happening.
  • +
  • 1
  • -
  • Nope, runs fine for me if I paste it into a script an run it locally, but if I paste it into the challange it fails, might be a problem in the ruby-port of the challange.
  • +
  • 0
  • -
  • counter = 1 Could it be that you mean to say counter += 1 in the elsif condition?
    Log in to write an answer.