Ad
  • Custom User Avatar

    Well, description limits n to 0 <= n < 1000000000. I think gsub(/.../, '\0,') would be shorter and faster than scanning for variable length.

  • Custom User Avatar
  • Custom User Avatar

    One char longer than my solution. :P

  • Custom User Avatar

    Or if not display the exact input, at least give a hint as to what sort of input is causing the failure.

  • Custom User Avatar

    You're welcome! So glad I could be helpful. :)

  • Custom User Avatar

    missmaggiemo, thank you for pointing out some flaws in my test cases, now corrected.

  • Custom User Avatar

    Hmm, I clearly haven't made it clear :-) that the objective is to validate the string.

    New wording "This method accepts a single argument, a string to be validated.

    The string can be of any length and have any contents. But to be considered valid, it must have zero or more blanks followed by zero to eight numeric digits followed by, again, zero or more blanks.

    If it is valid, the number within the string + 1 is returned. Otherwise, the string 'invalid' is returned. A completely blank string is considered valid, equal to zero.

    '   7   '     returns 8

    '     a  8 '     returns 'invalid'

    '    ' returnss 1
    'abc1' returns 'invalid'

    An easy problem.

    The only slight challenge is to produce a simple method that has no regular expressions and no nesting; no explict nesting with 'for' or 'while' or whatever, no block nesting with things like 'map' or 'each'; obviously I have no way of enforcing this!"

  • Custom User Avatar

    I think this problem needs either a better explanation of the expected input (e.g., inputs will include only a string of letters and/or numbers surrounded by spaces, but there will be no spaces in between the numbers/letters), or better explanation as to what the expedcted output of, say, " 1 8 " or " 0 " would be.