Programming Questions

  • Newest
  • Popular Tags
  • Ask Question
  • Dash Insert Ruby
    I'm doing the Dash Insert II challenge (given a string, return a string with dashes between adjacent even numbers, and an asterisk between any adjacent odd numbers), and it works for the most part. However, I noticed that whenever my input is more than 9 characters long, I get an error message. Here are a couple I have seen, just by randomly entering long numbers into my input: #input num: 132322244 (eval):53: (eval):53: compile error (SyntaxError)(eval):49: Invalid char `\16326240014' in expression #input num = 56647221222122212211 (eval):53: (eval):53: compile error (SyntaxError)(eval):49: Invalid char `\17762203671' in expression I wrote this to accept either a string of numbers or an int. When I input a long number in quotes, I have no problem getting an output. Only when I input the number as an int do I get the error on strings greater than 9 characters. Here's my code: def DashInsertII(num) num = num.to_s.split('') new_str = [] #initial check for whether the first number is even or odd if num[0].to_i % 2 == 0 is_even = true else is_even = false end for i in 0..num.length-1 do #case if num is even and not 0 if num[i].to_f % 2 == 0 && num[i].to_f != 0 if is_even == true && i != 0 new_str.push('*') new_str.push(num[i]) elsif is_even == true && i == 0 new_str.push(num[i]) else is_even = true new_str.push(num[i]) end #case if num is odd and not 0 elsif num[i].to_f % 2 != 0 && num[i].to_f != 0 if is_even == false && i != 0 new_str.push('-') new_str.push(num[i]) elsif is_even == false && i == 0 new_str.push(num[i]) else is_even = false new_str.push(num[i]) end #case if num is 0, so that no * or -'s are added else new_str.push(num[i]) end end return new_str.join end # keep this function call here # to see how to enter arguments in Ruby scroll down DashInsertII(STDIN.gets)
    bluem00se posted this question on 6/30/15 | dash, insert, ruby
    Answers
  • +
  • 0
  • -
  • I think this is a problem with the way this site processes code, rather than a problem with your code. I've had problems, too, with large numbers causing problems that don't occur when the program is tested with smaller numbers on some exercises here. Dunno what to tell you other than to report it and hope they give you a badge for finding a bug. Have you tried running it on your own machine, or from somewhere like c9.io?
    Log in to write an answer.