Skip to content

naderawadala/math-expressions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Math-Expressions

Math-Expressions is a web service written on Go that evaluates simple math word problems. It provides endpoints for parsing and evaluating expressions, validating their syntax, and retrieving error logs of invalid evaluations.

Installation

Clone the repository, all you need as a prerequisite is to have the Go programming language installed on your machine.

git clone https://github.com/naderawadala/math-expressions.git

In order to run the project run the following terminal command in the directory of the main.go file:

go run main.go

The server will be listening on http://localhost:8080

Routes

POST /evaluate

Description: Evaluates a math expression and returns the result of the operation if valid, otherwise returns an error with the reason why the math expression is invalid.

Valid operators are: "plus", "minus", "divided by", "multiplied by". Trying to use any other operator will return an Unsupported operation error.

Request Body:

{
  "expression": "<a simple math problem>"
}

Response Body:

{
  "result": "<result from expression operations>"
}
{
  "error": "<error description>"
}

Example:

Request:

{
  "expression": "What is 5 plus 13?"
}

Response:

{
  "result": 18
}

Request:

{
  "expression": "What is 1 plus plus 2?"
}

Response:

{
  "error": "Invalid syntax: expected a number, got 'plus'"
}

POST /validate

Description: Validates a math expression and returns whether it is a valid expression or not. If invalid also returns the reason.

Request Body:

{
  "expression": "<a simple math problem>"
}

Response Body:

{
  "valid": "<true or false>"
  "reason": "<if false, returns why the expression is invalid, otherwise this does not get returned>"
}

Example:

Request:

{
  "expression": "What is 5 plus 13?"
}

Response:

{
  "valid": true
}

Request:

{
  "expression": "What is 1 plus plus 2?"
}

Response:

{
  "valid": false
  "reason": "Invalid syntax: expected a number, got 'plus'"
}

GET /errors

Description: Returns all the invalid expressions stored in memory.

Response Body:

{
"result": [
  {
    "expression": "<a simple math problem>",
    "endpoint": "<endpoint URL where the error occured>",
    "frequency": "<number of times the expression failed>",
    "type": "<error type, i.e 'unsupported operation' or 'invalid syntax'>"
  },
  ...
]
}

Example:

{
    "result": [
        {
            "expression": "What is 5 plus 4 minus ten?",
            "endpoint": "/evaluate",
            "frequency": 1,
            "type": "invalid syntax: expected a number, got 'ten?'"
        },
        {
            "expression": "What is 5 plus 4 10?",
            "endpoint": "/validate",
            "frequency": 3,
            "type": "unsupported operation: '10?'"
        },
        {
            "expression": "What is 5 plus 4 minus?",
            "endpoint": "/evaluate",
            "frequency": 1,
            "type": "unsupported operation: 'minus?'"
        }
    ]
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages