Skip to content

joao-coimbra/sanic-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

57 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sanic Calculator

This is a simple calculator API built with Sanic.

License Data da última versão

Table of Contents

📌 Resume

This is a simple calculator API built using the Sanic framework in Python. It provides endpoints for performing basic arithmetic operations, calculating square roots and powers, calculating mean, harmonic mean and moda.

⚡ Technology

Technology used within the project:

🔧 Installation

  1. Clone the repository:

    git clone https://github.com/joao-coimbra/sanic-calculator.git
  2. Install the dependencies:

    cd calculator-api
    pip install -r requirements.txt
  3. Start the API server:

    python server.py

    sanic CLI

    sanic server.app

    The server will start running at https://localhost:8000.

📍 Endpoints

Addition

Takes list of numbers and returns the sum.

POST /api/add
Parameter Type Description
nums array Required. list of numbers

Request Body:

{
  "nums": [2, 3, 7]
}

Response Body:

{
  "result": 12
}

Subtraction

Takes list of numbers and returns the subtract.

POST /api/subtract
Parameter Type Description
nums array Required. list of numbers

Request Body:

{
  "nums": [10, 7]
}

Response Body:

{
  "result": 3
}

Multiplication

Takes list of numbers and returns the multiply.

POST /api/multiply
Parameter Type Description
nums array Required. list of numbers

Request Body:

{
  "nums": [10, 7, 3]
}

Response Body:

{
  "result": 210
}

Division

Divides one number by another.

POST /api/divide
Parameter Type Description
num float Required. number to be divided
divisor int Required. divisor number

Request Body:

{
  "num": 7,
  "divisor": 2
}

Response Body:

{
  "result": 3.5
}

Square Root

Calculates the square root of a number.

POST /api/sqrt
Parameter Type Description
num int Required. number to get square root

Request Body:

{
  "num": 9
}

Response Body:

{
  "result": 3
}

Power

Raises one number to the power of another.

POST /api/power
Parameter Type Description
base float Required. number to get its power
exponent int Required. calculation exponent

Request Body:

{
  "base": 5,
  "exponent": 3
}

Response Body:

{
  "result": 125
}

Arithmetic Mean

Calculates the arithmetic mean of a list of numbers.

POST /api/mean
Parameter Type Description
nums array Required. list of numbers

Request Body:

{
  "nums": [5, 3, 7, 2]
}

Response Body:

{
  "result": 4.25
}

Harmonic Mean

Calculates the harmonic mean of a list of numbers.

POST /api/hmean
Parameter Type Description
nums array Required. list of numbers

Request Body:

{
  "nums": [5, 3, 7, 2]
}

Response Body:

{
	"result": 3.40080971659919
}

Moda

Calculates the mode (most frequent values) of a list of numbers.

POST /api/moda
Parameter Type Description
nums array Required. list of numbers

Request Body:

{
  "nums": [5, 3, 7, 2, 5, 2]
}

Response Body:

{
  "result": [5, 2]
}

🛠️ Usage

To use this API, you can send HTTP requests to the appropriate endpoints. For example, to add two numbers together, you can send a POST request to the /api/add endpoint with a JSON body like this:

{
  "nums": [5, 2]
}

And the API will respond with a JSON body like this:

{
  "result": 7
}

💎 Authors

name name name

📜 License

This project is licensed under the terms of the MIT license.