mathgenerator

mathgenerator

Fork of https://github.com/lukew3/mathgenerator
Adding more Physics and Computer Science questions
As well as randomly generated questions

A math problem generator, created for the purpose of giving teachers and students the means to easily get access to random math exercises to suit their needs.

To try out generators, go to https://mathgenerator-demo.netlify.app

See CONTRIBUTING.md for information about how to contribute.

Table of Contents

Installation

The project can be install via pip

pip install mathgenerator

Usage

Here is an example of how you would generate an addition problem:

import mathgenerator

#generate an addition problem
problem, solution = mathgenerator.addition()

#another way to generate an addition problem using genById()
problem, solution = mathgenerator.gen_by_id(0)

You may prefer to use import mathgenerator as mg and run functions like mg.addition() so that you don't have to type as much.
You can also use gen_by_name() to generate a random question. If no subject is specified, a random question from the whole set is chosen. If a subject is specified but no topic, a random question from that subject is chosen.
Note, if a subject and a topic are both specified, the same type of question will always be returned. Problem/solution pairs are generated with either:

  • mathgenerator.<generator_name>() - generates a problem, solution set from the given generator name.
  • mathgenerator.genById(id) - generates a problem, solution set with generator id provided by the id parameter

You can also use get_gen_list() to return a list of all generators included in the library in the format:

[funcname, subjectname]

Documentation

Documentation can be found at https://lukew3.github.io/mathgenerator

 1"""
 2.. include:: ../README.md
 3"""
 4
 5from .algebra import *
 6from .basic_math import *
 7from .calculus import *
 8from .computer_science import *
 9from .geometry import *
10from .misc import *
11from .physics import *
12from .statistics import *
13
14from ._gen_list import gen_list
15
16
17# [funcname, subjectname]
18def get_gen_list():
19    return gen_list
20
21def gen_by_id(id, *args, **kwargs):
22    if id < len(gen_list):
23        return globals()[gen_list[id][0]](*args, **kwargs)
24    else:
25        print(f"Error finding a question matching id: {id}")
26    return (1,1)
27
28def gen_by_name(subject='',topic=''):
29    # If no subject is specified, a random question from the whole set is chosen
30    # If a subject is specified but no topic, a random question from that subject is chosen
31    if subject == '':
32        if topic == '':
33            return globals()[random.choice(gen_list)[0]]()
34        else:
35            for id in range(len(gen_list)):
36                if gen_list[id][0] == topic:
37                    return globals()[gen_list[id][0]]()
38            print(f"Error finding a question matching topic: {topic}")
39    else:
40        if topic == '':
41            items = [item for item in gen_list if item[1] == subject]
42            if len(items) > 0:
43                return globals()[random.choice(items)[0]]()
44            else:
45                print(f"Error finding a question matching subject: {subject}")
46        else:
47            for id in range(len(gen_list)):
48                if gen_list[id][0] == topic and gen_list[id][1] == subject:
49                    return globals()[gen_list[id][0]]()
50            print(f"Error finding a question matching subject: {subject}, topic: {topic}")
51    return (1,1)
52
53# Legacy Functions
54def getGenList():
55    return gen_list
56
57def genById(id, *args, **kwargs):
58    return globals()[gen_list[id][0]](*args, **kwargs)
def get_gen_list():
19def get_gen_list():
20    return gen_list
def gen_by_id(id, *args, **kwargs):
22def gen_by_id(id, *args, **kwargs):
23    if id < len(gen_list):
24        return globals()[gen_list[id][0]](*args, **kwargs)
25    else:
26        print(f"Error finding a question matching id: {id}")
27    return (1,1)
def gen_by_name(subject='', topic=''):
29def gen_by_name(subject='',topic=''):
30    # If no subject is specified, a random question from the whole set is chosen
31    # If a subject is specified but no topic, a random question from that subject is chosen
32    if subject == '':
33        if topic == '':
34            return globals()[random.choice(gen_list)[0]]()
35        else:
36            for id in range(len(gen_list)):
37                if gen_list[id][0] == topic:
38                    return globals()[gen_list[id][0]]()
39            print(f"Error finding a question matching topic: {topic}")
40    else:
41        if topic == '':
42            items = [item for item in gen_list if item[1] == subject]
43            if len(items) > 0:
44                return globals()[random.choice(items)[0]]()
45            else:
46                print(f"Error finding a question matching subject: {subject}")
47        else:
48            for id in range(len(gen_list)):
49                if gen_list[id][0] == topic and gen_list[id][1] == subject:
50                    return globals()[gen_list[id][0]]()
51            print(f"Error finding a question matching subject: {subject}, topic: {topic}")
52    return (1,1)
def getGenList():
55def getGenList():
56    return gen_list
def genById(id, *args, **kwargs):
58def genById(id, *args, **kwargs):
59    return globals()[gen_list[id][0]](*args, **kwargs)