March 24 - Build a Prefix Notation Calculator in Ruby
Building a calculator is a popular interview question and a good approach for learning a programming language. In this guide we're going to build a Prefix Notation calculator in Ruby, which enables users to enter an operator, followed by any set of numbers to calculate.
Guide Tasks
- Read Tutorial
- Watch Guide Video
- Complete the Exercise
Video locked
This video is viewable to users with a Bottega Bootcamp license
Already a Bottega Student? Sign In
Summary
Build a Prefix Notation Calculator in Ruby.
Exercise Description
Build a method that performs addition
, subtraction
, multiplication
, and division
on any number of values passed to the method. Additionally, ensure to handle improperly entered requests (bonus points if you build your own error handling class).
Example Input/Output
pn_calculator "+ 0 5 1 9 11" # => 26 pn_calculator "- 10 200" # => -190 pn_calculator "* 2 2" # => 4 pn_calculator "/ 0 5 1 9 11" # => 0.0 pn_calculator("* 10 b").to_s # => "Values need to be integers" pn_calculator("l 0 5").to_s # => "Operator is not valid, needs to be +, -, *, /"
Real World Usage
This is a popular interview coding question since it requires developers to showcase skill when it comes to working with mathematical calculations in Ruby. Additionally, it offers a number of potential implementation options, including a wide range of options when it comes to managing improper input from users.