Skip to content

Instantly share code, notes, and snippets.

@pepasflo
Last active April 17, 2019 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pepasflo/fbf44276a165b229f600257c0dc2a1d0 to your computer and use it in GitHub Desktop.
Save pepasflo/fbf44276a165b229f600257c0dc2a1d0 to your computer and use it in GitHub Desktop.
mathterp: A series of problems for the puzzles guild around implementing a simple math interpreter compiler

mathterp

This is a series of problems / challenges around evaluating simple math expressions.

Problem A

Write a program which can evaluate infix addition operations of non-negative integers.

Example inputs:

1+1
4+13+7853

(and a single number by itself evaluates to itself, i.e. 42 => 42)

Problem B

Add support for infix subtraction operations.

4-1
12+36-5-19+7

This introduces notion that not all operators have the associativity property. (5-3)-1 is not the same as 5-(3-1). Addition is associative (grouping doesn't matter) but subtraction is not associative (grouping does matter).

Problem C

Add support for multiplication and division.

5+3*2-8/2+1457

This introduces the concept of operator precedence. Multiplication and division take precedence over addition and subtraction.

Note also that multiplication is associative while division is not.

Problem D

Add support for the unary negation sign.

-5
42*-3

Problem E

Add support for parenthesis to force operation order.

5-(3-1)
12*(6/2)
5+((4/2)*7)
(((((((1+1)))))))

Concepts / Approach

These problems touch on a variety of fundamental topics in computer science.

Solutions to the problem may be implemented as interpreters or compilers. An interpreter may be interactive (featuring a REPL).

Some of these problems may be solved by implementing a lexer / parser, or by other means, such as translating the expression into reverse-polish notation and implementing a stack evaluator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment