Git Product home page Git Product logo

sparkcalc's Introduction

sparkcalc

Summary

Write a calculator library.

Requirements:

  • Handle binary operations, e.g. 5 ­ 6, 8 * 4
  • The calculator should be able to handle a full arithmetic expression at once, for example: 1 + 1 ­ 4 * 4
  • The library must take Order of Operations into account
  • No need to handle parentheses, e.g. (1 + 1 ­ 4) * 4
  • Must be extendable so that it accepts user­defined operands

Objectives:

  • OOP
  • Best practices
  • Algorithms

Thoughts

Need to convert infix notation into a tree or find another alternative. Dijkstra's shunting yard method can be used here; it might be shorter to convert to RPN instead and process a stack than build and traverse a tree.

From an OOP perspective, we might consider operators to be tokenized into a subclass of a general "Operator", each having specific attributes regarding symbol and precedence/weight, etc. From a practical perspective, this might be overkill.

For simplicity's sake, let's assume that we'll only accept positive integers.

In addition to listed requirements, library should be self-contained for ease of demonstration/review.

Choosing Python to solve problem since it is great for modeling concepts. The runtime environment also comes with a built-in unit test library, making it easy to show that the project works (and doesn't work) under various scenarios.

Using it

Assumes Python 2.7 is installed and available in the PATH. Assumes GNU Make is installed.

Run make to execute tests/demos.

To try using the library interactively, run python2.7 from this directory and try this:

>>> from infix import eval_expr
>>> eval_expr('4 + 4 * 8 / 2 + 10')
30

sparkcalc's People

Contributors

memoia avatar

Watchers

James Cloos avatar  avatar

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.