Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/yjg30737/pymeg
Python mathematical expression generator
https://github.com/yjg30737/pymeg
expression math-problem-generator mathematical-expressions python python-math python3 random-math-problem-generator
Last synced: 7 days ago
JSON representation
Python mathematical expression generator
- Host: GitHub
- URL: https://github.com/yjg30737/pymeg
- Owner: yjg30737
- License: mit
- Created: 2021-11-17T10:44:49.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-05-08T10:13:19.000Z (over 2 years ago)
- Last Synced: 2024-09-18T00:05:28.141Z (about 2 months ago)
- Topics: expression, math-problem-generator, mathematical-expressions, python, python-math, python3, random-math-problem-generator
- Language: Python
- Homepage:
- Size: 2.76 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# pymeg
## Table of Contents
* [General Info](#general-info)
* [Class Overview](#class-overview)
* [Setup](#setup)
* [Example](#example)## General Info
Python mathematical expression generator## Class Overview
* AbstractExpStructAbstract class of ExpStruct.
This class has four constants.
```python
AbstractExpStruct.PLUS = 0
AbstractExpStruct.MINUS = 1
AbstractExpStruct.MULTIPLY = 2
AbstractExpStruct.DIVIDE = 3
```* ExpStruct
Class which can store the rules of expression. inherits the AbstractExpStruct.
ExpStruct has methods like below.
```python
set_op(lst) # set the operators to be used to expression to generate as AbstractExpStruct's constant.
# For example, if you want to use only plus and minus then write like this
# expStruct.set_op([AbstractExpStruct.PLUS, AbstractExpStruct.Minus])get_op() -> self.__op_lst # get the operators to be used as list of AbstractExpStruct's constant.
set_oper_cnt(cnt) # set the number of operands of expression to be generated.
set_range(min_, max_, types: list) # set min, max number to use of types(plus, minus ...).
```
* ExpGenerator
Class which generate the randomized expression with given ExpStruct instance
Using `get_problem` static method to generate the expression. Argument is `ExpStruct`.
Result is string-typed mathematical expression.
```
problem = ExpStruct()
ExpGenerator.get_problem(problem)
```## Setup
`python -m pip install pymeg`## Example
### Code Sample
```pythonfrom pymeg.expGenerator import ExpGenerator
from pymeg.expStruct import ExpStructproblem = ExpStruct()
problem.set_oper_cnt(4)
problem.set_range(20, 100, [ExpStruct.PLUS, ExpStruct.MINUS])
problem.set_range(2, 10, [ExpStruct.MULTIPLY])
problem.set_range(2, 4, [ExpStruct.DIVIDE])
print(ExpGenerator.get_problem(problem))
print(ExpGenerator.get_problem(problem))
print(ExpGenerator.get_problem(problem))
ext = ExpGenerator.get_problem(problem)
print(ext)
print(eval(ext))
```### Result
Note: Problem will be generated randomly depending on operand count set by user.
```python
54+25-54*9
6*6-31/3
2/4*8/3
9*3*6/2
81.0
```