Giter VIP home page Giter VIP logo

calculator-kata's Introduction

Calculator Kata

Write a calculator that reads mathematical terms containing floating point numbers, +, -, * and / as well as parenthesis.

Calculate the result by applying the "usual" mathematical rules:

  • operators are used in infix notation: 2 + 3
  • multiply/div have higher precedence then add/sub
  • parenthesis have higher precedence then operators
  • numbers can contain a mix of integers and floating point numbers
  • calculation should follow mathematical rules, i.e. 2 / 0 is an error and not something like NaN or -Inf

The calculator should

  • calculate the result of a single mathematical expression (no matter how long)
  • print the result in a human readable style
  • report any error and refuse the calculation (error reporting style doesn't matter)
  • handle arbitrary long terms (i.e. tens of megabytes of input) well (i.e. in terms of memory usage as well as running time)

Specification of the input language

The calculator processes input that is read as UTF-8 encoded characters. A formal specification of the input language is defined by the following EBNF:

(* An expression defines the start of the production. *)
expr = number (* A bare number is a valid expression*)
     | ( expr, S, operator, S, number ) (* left-recursive operator chained expression *)
     | ( "(" S, expr, S, ")" ); (* parenthesis surround an expression *)

(* The list of available operators. *)
operator = "+" | "-" | "*" | "/";

(* Definition of a number - either 0.xyz or abc.xyz *)
number = zero_fraction 
       | non_zero_fraction;

non_zero_fraction = non_zero_digit { digit } { "." digit { digit } };

zero_fraction = "0" { "." digit { digit } };

non_zero_digit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";

digit = "0" | non_zero_digit;

(* Any whitespace used as a separator including none. *)
S = "" | { " " | "\n" | "\t" | "\r" | "\f" | "\b" } ;

Note that expressions in parenthesis can also contain parenthesis.

Examples

The following lines are all valid input sequences:

0
0.123
807.1328
17 * 19
(21 - 3) * 8

Implementation Restrictions

  • Only use the standard library for the production code; do not rely on external libraries (except for testing/benchmarking)
  • Apply an appropriate structure (i.e. packages, types, ...)
  • Write idiomatic code
  • Write unit tests to

Testcases

A couple of test files with increasing sizes are provided in testdata. The file names roughly tell the file's sizes. You can use them for functional as well as for benchmark tests.

The results are

  • testdata/1k -> 248253190541.03891
  • testdata/10k -> 214512826488689376
  • testdata/100k -> 481585283158357967896576
  • testdata/1m -> 830415166156152287340265472
  • testdata/10m -> 6780466519056739908798922614519103488

calculator-kata's People

Contributors

halimath avatar

Watchers

 avatar

Forkers

svergin

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.