Skip to content

Commit

Permalink
Add random term generator script for benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludwig-Work committed Aug 12, 2023
1 parent 7a416f6 commit 35fa8bd
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,11 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd



# Python compiled / optimized files
__pycache__/
*.py[cod]
*$py.class
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This repository contains:
- My [presentation slides](./Slides.pdf)
- My [source code](./LazyMathInstructor) (split in several `.cs` files)
- Several input examples: [Binomial formulas](./binomial_formulas.txt), [Examples from the problem description](./problem_examples.txt) and a [long term](./long_term.txt)
- A [term generator](./TermGenerator/termgenerator.py) that can generate random terms of a desired length


## Usage of the program
Expand All @@ -33,4 +34,14 @@ Program writes to STDOUT:
- `NO` if they aren't.

Verbose mode can be enabled by passing `-v` as a command line argument, in which case the program will print each addition,
subtraction and multiplication operation it performs, as well as each term after it was parsed, normalized and ordered deterministically.
subtraction and multiplication operation it performs, as well as each term after it was parsed, normalized and ordered deterministically.


## Usage of the term generator:

Calling the script without any arguments, it generates `N = 20` random terms of approximately `LENGTH = 250` characters.

`N` and `LENGTH` can be passed as command line arguments to adjust the output.
`N` must be an even number because the Lazy Math Instructor program can only work with *pairs* of terms.

The script's STDOUT can be used as STDIN for the main program for benchmarking.
71 changes: 71 additions & 0 deletions TermGenerator/termgenerator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from random import choice, randint
import sys


def print_args_error():
print('\nUsage: termgenerator.py N LENGTH\nwhere\n' +
' N: Number of terms to generate (must be even)\n' +
' LENGTH: Approximate length of each generated term\n' +
'The output of this script can be used as input for the LazyMathInstructor program.')


def read_config():
# Number of terms to be generated
N = 20

# Approximate length of the term to be generated
LENGTH = 250

if len(sys.argv) == 3:
try:
N = int(sys.argv[1])
LENGTH = int(sys.argv[2])
except ValueError:
print_args_error()
sys.exit(1)

if (N <= 0) or (LENGTH <= 0) or (N % 2 == 1):
print_args_error()
sys.exit(1)

elif len(sys.argv) != 1:
print_args_error()
sys.exit(1)

return N, LENGTH



ATOMS = [chr(x) for x in range(ord('a'), ord('z') + 1)] * 4 + [str(x) for x in range(101)]
def random_atom():
return choice(ATOMS)


def random_term(length):
term = random_atom()

while len(term) < length:
choice = randint(0, 101)
if choice < 25:
term = f'({term})'
elif choice < 45:
term = f'{term}+{random_term(randint(1, (length - len(term))))}'
elif choice < 75:
term = f'{term}-{random_term(randint(1, (length - len(term))))}'
else:
term = f'{term}*{random_term(randint(1, (length - len(term))))}'

return term


def main():
N, LENGTH = read_config()

print(N // 2)
for i in range(N):
print(random_term(LENGTH))



if __name__ == '__main__':
main()

0 comments on commit 35fa8bd

Please sign in to comment.