From 892b64ee876b1b3fdbd726f1e52e8ce74d804637 Mon Sep 17 00:00:00 2001 From: KafkaNoNeko Date: Sun, 22 Aug 2021 15:39:21 -0700 Subject: [PATCH 1/7] add gen_poly --- .../problem_bank_helpers.py | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/problem_bank_helpers/problem_bank_helpers.py b/src/problem_bank_helpers/problem_bank_helpers.py index a21e483..6c8f6e5 100644 --- a/src/problem_bank_helpers/problem_bank_helpers.py +++ b/src/problem_bank_helpers/problem_bank_helpers.py @@ -3,6 +3,8 @@ from collections import defaultdict import numpy as np +import sympy as sp +import random as rd import sigfig def create_data2(): @@ -191,4 +193,23 @@ def sign_str(number): if (number < 0): return " - " else: - return " + " \ No newline at end of file + return " + " + +def gen_poly(var): + """Returns a polynomial of at least 2 terms and at most 3 terms in a desired variable + + Args: + var (sympy symbol): the variable of the polynomial + + Returns: + expr: A polynomial of at least 2 terms and at most 3 terms in the desired variable + """ + + # Generate list of coefficients. + # Use size = random.randint(2,3) to generate at most 3 terms + coeff_list = list(np.random.randint(low = -9,high = 10,size = rd.randint(2,3))) + + # generate the expression + expr = sum(co*var**i for i, co in enumerate(reversed(coeff_list),1)) + + return expr \ No newline at end of file From d2aacb465527c444df4db977ea479a385b92144b Mon Sep 17 00:00:00 2001 From: KafkaNoNeko Date: Sun, 22 Aug 2021 15:45:54 -0700 Subject: [PATCH 2/7] add sympy_to_str --- .../problem_bank_helpers.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/problem_bank_helpers/problem_bank_helpers.py b/src/problem_bank_helpers/problem_bank_helpers.py index 6c8f6e5..5a00681 100644 --- a/src/problem_bank_helpers/problem_bank_helpers.py +++ b/src/problem_bank_helpers/problem_bank_helpers.py @@ -212,4 +212,21 @@ def gen_poly(var): # generate the expression expr = sum(co*var**i for i, co in enumerate(reversed(coeff_list),1)) - return expr \ No newline at end of file + return expr + +# function for converting a sympy expression to string for display +# this is needed for the json conversion +def sympy_to_str(expr): + """Returns the string representation of a sympy expression for display on PL + + Args: + expr (sympy expression): a sympy polynomial + + Returns: + str_expr (string): the string representation of the sympy expression + """ + str_expr = str(expr) + str_expr = str_expr.replace("**", "^") + str_expr = str_expr.replace("*", "") + + return str_expr \ No newline at end of file From f9b67fa746ae638158765f7b936f6ce7d264a5af Mon Sep 17 00:00:00 2001 From: KafkaNoNeko Date: Sun, 22 Aug 2021 15:46:59 -0700 Subject: [PATCH 3/7] update description --- src/problem_bank_helpers/problem_bank_helpers.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/problem_bank_helpers/problem_bank_helpers.py b/src/problem_bank_helpers/problem_bank_helpers.py index 5a00681..4ed8c93 100644 --- a/src/problem_bank_helpers/problem_bank_helpers.py +++ b/src/problem_bank_helpers/problem_bank_helpers.py @@ -214,10 +214,8 @@ def gen_poly(var): return expr -# function for converting a sympy expression to string for display -# this is needed for the json conversion def sympy_to_str(expr): - """Returns the string representation of a sympy expression for display on PL + """Returns the string representation of a sympy expression for display (json conversion) on PL Args: expr (sympy expression): a sympy polynomial @@ -229,4 +227,5 @@ def sympy_to_str(expr): str_expr = str_expr.replace("**", "^") str_expr = str_expr.replace("*", "") - return str_expr \ No newline at end of file + return str_expr + From 5099395f47b870826e8fa72e9a2292b34bf56c87 Mon Sep 17 00:00:00 2001 From: KafkaNoNeko Date: Sun, 22 Aug 2021 15:56:50 -0700 Subject: [PATCH 4/7] add gen_rand_poly --- .../problem_bank_helpers.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/problem_bank_helpers/problem_bank_helpers.py b/src/problem_bank_helpers/problem_bank_helpers.py index 4ed8c93..24b69b8 100644 --- a/src/problem_bank_helpers/problem_bank_helpers.py +++ b/src/problem_bank_helpers/problem_bank_helpers.py @@ -229,3 +229,21 @@ def sympy_to_str(expr): return str_expr +def gen_rand_poly(var, true_poly): + """Returns an incorrect polynomial to be used in answer options + + Args: + var (sympy symbol): the variable of the polynomial + true_poly (sympy expression): the correct polynomial + + Returns: + expr (string): the string representation of the incorrect polynomial + """ + expr = gen_poly(var) + + while (expr == true_poly): + expr = gen_poly(var) + + expr = sympy_to_str(expr) + + return expr \ No newline at end of file From eef695e59a6ade186eba67a8d6a62b0c36f793a8 Mon Sep 17 00:00:00 2001 From: KafkaNoNeko Date: Sun, 22 Aug 2021 16:12:57 -0700 Subject: [PATCH 5/7] add gen_rand_num --- .../problem_bank_helpers.py | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/problem_bank_helpers/problem_bank_helpers.py b/src/problem_bank_helpers/problem_bank_helpers.py index 24b69b8..8953b03 100644 --- a/src/problem_bank_helpers/problem_bank_helpers.py +++ b/src/problem_bank_helpers/problem_bank_helpers.py @@ -246,4 +246,24 @@ def gen_rand_poly(var, true_poly): expr = sympy_to_str(expr) - return expr \ No newline at end of file + return expr + +def gen_rand_num(low, high, range, true_ans): + """Returns a random number outside of a specified range of the true answer + + Args: + low (integer, float): the lower bound of the interval within which a random number is desired to be generated + high (integer, float): the upper bound of the interval within which a random number is desired to be generated + range (integer, float): specifies the range from the true answer within which a number cannot be generated + true_ans (intger, float): the correct answer + + Returns: + randnum (float): an unrounded random number outside of a specified range of the true answer + """ + + randnum = rd.uniform(low, high) + + while(randnum > (1-range)*true_ans and randnum < (1+range)*true_ans): + randnum = rd.uniform(low, high) + + return randnum \ No newline at end of file From 5c5206911d7063327cffff13af46143f8998d228 Mon Sep 17 00:00:00 2001 From: KafkaNoNeko Date: Sun, 22 Aug 2021 16:21:02 -0700 Subject: [PATCH 6/7] some error checking --- src/problem_bank_helpers/problem_bank_helpers.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/problem_bank_helpers/problem_bank_helpers.py b/src/problem_bank_helpers/problem_bank_helpers.py index 8953b03..647c09c 100644 --- a/src/problem_bank_helpers/problem_bank_helpers.py +++ b/src/problem_bank_helpers/problem_bank_helpers.py @@ -262,8 +262,17 @@ def gen_rand_num(low, high, range, true_ans): """ randnum = rd.uniform(low, high) - + + count = 0 + k = 10 + while(randnum > (1-range)*true_ans and randnum < (1+range)*true_ans): + count += 1 randnum = rd.uniform(low, high) + + # break infinite loops + if (count > k): + randnum = np.inf + break return randnum \ No newline at end of file From 915306f71a47ca8c0579491ba6c872d4ffae38f6 Mon Sep 17 00:00:00 2001 From: KafkaNoNeko Date: Sun, 22 Aug 2021 16:22:46 -0700 Subject: [PATCH 7/7] error checking for interval --- src/problem_bank_helpers/problem_bank_helpers.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/problem_bank_helpers/problem_bank_helpers.py b/src/problem_bank_helpers/problem_bank_helpers.py index 647c09c..fc6ba51 100644 --- a/src/problem_bank_helpers/problem_bank_helpers.py +++ b/src/problem_bank_helpers/problem_bank_helpers.py @@ -261,6 +261,10 @@ def gen_rand_num(low, high, range, true_ans): randnum (float): an unrounded random number outside of a specified range of the true answer """ + # return infinity if a wrong interval is specified + if (low > high): + return np.inf + randnum = rd.uniform(low, high) count = 0