Ad
  • Default User Avatar

    wow, I never thought of using spaceship operator. Nicely done

  • Custom User Avatar

    Well, that's after I changed this:

        begin
          s1, s2 = two_sum(arr.dup, target)
        rescue NameError
          s1, s2 = twoSum(arr.dup, target)
        end
    

    For this:

        if defined?(two_sum)
          s1, s2 = two_sum(arr.dup, target)
        else
          s1, s2 = twoSum(arr.dup, target)
        end 
    

    Any idea why the first one gave that error instead of the out of bounds? I mean, hiding the real problem.

  • Default User Avatar
    main.rb:14:in `block (2 levels) in two_sum': undefined method `+' for nil:NilClass (NoMethodError)
    	from main.rb:12:in `loop'
    	from main.rb:12:in `block in two_sum'
    	from main.rb:11:in `loop'
    	from main.rb:11:in `two_sum'
    

    As it says, one of the arguments of the + inside the inner loop is nil, which means the index is out of bounds.

  • Custom User Avatar

    I saw it, but still I have no idea why that happens, sorry. Maybe someone who's more fluent in Ruby could find out why.

  • Default User Avatar

    Hi Chrono79. I pasted the the solution that passes about 10 tests before getting that weird NoMethod error above if you want to replicate. Thank you :-)

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    I'm gonna explain why that happens (I hope), but that part of the code should run only if your function isn't called two_sum.

        begin
          s1, s2 = two_sum(arr.dup, target)
        rescue NameError
          s1, s2 = twoSum(arr.dup, target)
        end
    

    And it's there for compatibility reasons. The Ruby translation used camelCase instead of snake_case at first and then that was changed. No idea why it would pass some tests and fail randomly. It doesn't happen with my solution.

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    I got that twoSum error too with Ruby. I've printed it out below. Mine passed about 8 tests before failing this one.

    main.rb:37:in rescue in block (2 levels) in <main>': undefined method twoSum' for main:Object (NoMethodError)
    Did you mean? two_sum

    Part of me wonders if a test has twoSum as one of the method calls most likely me that's made the mistake though :-)

  • Custom User Avatar

    What language?

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    Thanks for this. Really enjoyed it. It was tricky to work out intially how to solve it. Loved the many different solution types people had. Great.

  • Default User Avatar

    Oh wow. Such a creative way of solving this. Very nice indeed.
    It took a while until I realised the alphabet was extended. I'll definitely steal this :-)

  • Default User Avatar

    Wow I love this solution. So simple, concise and clear.

  • Loading more items...