Ad
  • Default User Avatar

    not converting the numbers to floats before applying the operator?

  • Custom User Avatar

    Yustynn did a great job explaining what I meant.

  • Default User Avatar

    I'm new to programming, but I think he's talking about having two iterators instead of one.

    In the original solution, you had ruby run through each element one by one to filter out unwanted elements. Then you go through those selected elements one by one again to sum them up.

    His solution does it all one shot. In his inject, he goes through the elements one by one but performs both actions (filtering and sumation) before moving on to the next element. Meaning that instead of running through all the elements twice (once to filter, once to sum), he does it once and he's done.

    So worst case: all integer values. All elements are run through once, none are rejected. Then all have to be run through yet again to sum. Two run throughs instead of one, so supposedly 2x the time taken.

  • Custom User Avatar

    Could you explain further? I am trying to get my head around why it'll require twice as much computing in worst case.

  • Custom User Avatar

    100 % agree. We should not give away efficiency so easy.

  • Default User Avatar

    Interesting that you should first filter the arguements, and then use an reduce method. In my case, I used only an inject method with a ternary operator checking for type Fixnum. I imagine that the singular inject method would be speedier, but I'll have to check. Very clever nonetheless!

  • Default User Avatar

    It would be great if either of you could post a comparison using the ruby benchmark module - It'd be interesting to see if Ruby optimizes the code, so that it performs better than the theoretical bound.

  • Custom User Avatar

    Excellent point. Your solution runs much faster on very large inputs.

  • Custom User Avatar

    Very good use of a 1 liner but I don't like it because you it'll run in worst case 2n time where you can do it in n time.

  • Default User Avatar

    How are you getting an expected result of -3 from this?
    The result should be -3.66666666... which this solution does evaluate to correctly.

  • Custom User Avatar

    the preloaded code is not actually preloaded. bob and alice variables don't even exists.

  • Custom User Avatar

    doesn't seem to work for all the expressions?
    try this:
    3 - 3 * 8 / 3 / 3 - 4
    Expected: -3, Actual: -1

  • Custom User Avatar

    I keep getting:
    TypeError: nil is not a string

    Can you tell me what tests are you running?

  • Custom User Avatar

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