mathgen

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2020 License: MIT Imports: 7 Imported by: 0

README

Math generator

GoDoc

As a dad, I frequently to play math quiz with my daughter. Bu I'm too lazy to frequently to create math questions. This' reason why I create this lib/api/web, that I can help me to generate questions. This repository will evolue frequently, I think, to support more topic.

Use as library

Install library:

  go get -u github.com/ledongthuc/mathgen

Usage:

  questionAndAnswers, _ := mathgen.AddIntegerN(3, 20)
  fmt.Println("Addends:", questionAndAnswers.Addends)
  fmt.Println("Sum:", questionAndAnswers.Sum)
  fmt.Println("Format:", questionAndAnswers.String())

More detail supported functions is cover at: https://godoc.org/github.com/ledongthuc/mathgen

Run demo application

Startup

Start with docker:

docker run -p 8080:8080 ledongthuc/mathgen-web:latest

Start from source code:

make clean; make test; make run;
API
Addition

POST http://localhost:8080/api/addition/generate

Request:

{
  "number_of_addends 2,
  "max_sum": 10
}

Response:

{
    "addends": [
        3,
        4
    ],
    "sum": 7,
    "question": "3 + 4 = ",
    "result": "3 + 4 = 7"
}
Subtraction

POST http://localhost:8080/api/subtraction/generate

Request:

{
  "max_minuend": 20,
  "number_of_subtrahends": 1
}

Response:

{
    "minuend": 9,
    "subtrahends": [
        5
    ],
    "difference": 4,
    "question": "9 - 5 = ",
    "result": "9 - 5 = 4"
}
Web

Local: http://localhost:8080

Free site: https://mathgen.thuc.space

Demo

Documentation

Overview

Package mathgen is a library for math's questions & answers generation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AdditionResult

type AdditionResult struct {
	Addends []int64
	Sum     int64
}

Addition Result of addition operator

func AddIntegerN

func AddIntegerN(numberOfAddends int, maxSum int64) (AdditionResult, error)

AddIntegerN generate a operator with sum. Parameter numberOfAddends defines addends in question, it should be greater than 1. Parameter maxSum defines maxsimum of Sum result, it should be greater than 0. If maxSum is greater than or equal numberOfAddends. Question always has pattern: 1 + 1 + 1 ....

Example
package main

import (
	"fmt"

	"github.com/ledongthuc/mathgen"
)

func main() {
	questionAndAnswers, _ := mathgen.AddIntegerN(3, 20)
	fmt.Println("Addends:", questionAndAnswers.Addends)
	fmt.Println("Sum:", questionAndAnswers.Sum)
	fmt.Println("Print Question:", questionAndAnswers.StringQuestion())
	fmt.Println("Print Full:", questionAndAnswers.String())
}
Output:

func AddIntegers

func AddIntegers(maxSum int64) (AdditionResult, error)

AddIntegers generate a operator with sum of 2 addends. Parameter maxSum defines maxsimum of Sum result, it should be greater than 0. If maxSum = 2, addends always is 1 + 1

Example
package main

import (
	"fmt"

	"github.com/ledongthuc/mathgen"
)

func main() {
	questionAndAnswers, _ := mathgen.AddIntegers(20)
	fmt.Println("Addends:", questionAndAnswers.Addends)
	fmt.Println("Sum:", questionAndAnswers.Sum)
	fmt.Println("Print Question:", questionAndAnswers.StringQuestion())
	fmt.Println("Print Full:", questionAndAnswers.String())
}
Output:

func (AdditionResult) String

func (a AdditionResult) String() string

String of AdditionResult will present struct data with question answer format a + b + c + ... = sum

func (AdditionResult) StringQuestion

func (a AdditionResult) StringQuestion() string

StringQuestion of AdditionResult will present struct data with question format a + b + c + ... =

type SubtractionResult

type SubtractionResult struct {
	Minuend     int64
	Subtrahends []int64
	Difference  int64
}

SubtractionResult of addition operator

func SubtractIntegerN

func SubtractIntegerN(numberOfSubtrahends int, maxMinuend int64) (SubtractionResult, error)

SubtractIntegerN generate a operator with subtraction. Parameter numberOfSubtrahends defines subtrahends in question, it should be greater than 0. Parameter maxMinuend defines maxsimum of minuend result, it should be greater than 0. If maxMinuend = 1, addends always is 1 - 1 - 0 ....

Example
package main

import (
	"fmt"

	"github.com/ledongthuc/mathgen"
)

func main() {
	questionAndAnswers, _ := mathgen.SubtractIntegerN(3, 20)
	fmt.Println("Minuend:", questionAndAnswers.Minuend)
	fmt.Println("Subtrahends:", questionAndAnswers.Subtrahends)
	fmt.Println("Difference:", questionAndAnswers.Difference)
	fmt.Println("Print Question:", questionAndAnswers.StringQuestion())
	fmt.Println("Print Full:", questionAndAnswers.String())
}
Output:

func SubtractIntegers

func SubtractIntegers(maxMinuend int64) (SubtractionResult, error)

SubtractIntegers generate a operator with subtraction of 1 minuend and 1 subtrahend. Parameter maxMinuend defines maxsimum of minuend, it should be greater than 0. If maxMinuend = 1, addends always is 1 - 1

Example
package main

import (
	"fmt"

	"github.com/ledongthuc/mathgen"
)

func main() {
	questionAndAnswers, _ := mathgen.SubtractIntegers(20)
	fmt.Println("Minuend:", questionAndAnswers.Minuend)
	fmt.Println("Subtrahends:", questionAndAnswers.Subtrahends)
	fmt.Println("Difference:", questionAndAnswers.Difference)
	fmt.Println("Print Question:", questionAndAnswers.StringQuestion())
	fmt.Println("Print Full:", questionAndAnswers.String())
}
Output:

func (SubtractionResult) String

func (s SubtractionResult) String() string

String of SubtractionResult will present struct data with question answer format a - b - c - d ... = difference

func (SubtractionResult) StringQuestion

func (s SubtractionResult) StringQuestion() string

StringQuestion of SubtractionResult will present struct data with question format a - b - c - d ... =

Directories

Path Synopsis
web

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL