Ad
  • Custom User Avatar

    Ternary at the end isn't needed, just use the empty check.

  • Custom User Avatar

    You need to make the tests public to clarify the intent of the problem. It's unnecessarily vague honestly.

  • Custom User Avatar

    Reduce would be cleaner.

  • Custom User Avatar

    Then it'd just be a polish / reverse polish stack calculator like a good number of 6th kyu runs.

  • Custom User Avatar

    Mine's pretty straightforward as well. Reduce takes operators as symbols, which is incredibly useful in this case.

  • Custom User Avatar

    You can use the index to make the numbers more succinct.

    In Ruby, reduce can take operators as symbols, making this a very easy solution to implement. Just wait until the second number is added, and you just need to get them out of the stack and reduce using the given operator.

    Now, if we wanted to stream this along a little bit more I would keep returning self on the number methods regardless and define == to give the head value of the stack. That's most of the reason I bothered with a stack is I thought the test cases might pull shenanigans on that account and stream numbers. Guess not.

  • Custom User Avatar

    In Ruby, you shouldn't do pointless things like Type Checking when trying to convey a point or do an exercise. You want people to get the math, not the catch-all nonsense. Abstract it out.