From f6e411a5dae4294390e65c111c1a6df4cd43106d Mon Sep 17 00:00:00 2001 From: MacWas Date: Thu, 3 Aug 2023 20:45:16 +0100 Subject: [PATCH 1/2] fix: padding 0s not removed Currently question and answer gets rid of insignificant 0's. This should not happen, as it is technically part of the answer. This representation is how it is taught in school for exams, and is also clearer for seeing that the sign bit flips. --- mathgenerator/computer_science.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mathgenerator/computer_science.py b/mathgenerator/computer_science.py index c3a3898..bb39253 100644 --- a/mathgenerator/computer_science.py +++ b/mathgenerator/computer_science.py @@ -32,11 +32,11 @@ def binary_2s_complement(maxDigits=10): | Ex. Problem | Ex. Solution | | --- | --- | - | 2's complement of $1011 = $ | $101$ | + | 2's complement of $1011 = $ | $0101$ | """ digits = random.randint(1, maxDigits) question = ''.join([str(random.randint(0, 1)) - for i in range(digits)]).lstrip('0') + for i in range(digits)]) answer = [] for i in question: @@ -56,7 +56,7 @@ def binary_2s_complement(maxDigits=10): answer.insert(0, '1') problem = f"2's complement of ${question} = $" - solution = ''.join(answer).lstrip('0') + solution = ''.join(answer) return problem, f'${solution}$' From 090c8f49cbda8a34c19d4de5937ab973b2f20057 Mon Sep 17 00:00:00 2001 From: MacWas Date: Thu, 3 Aug 2023 21:18:32 +0100 Subject: [PATCH 2/2] fix: complex to polar makes sense As pointed out in Issue #423, the `complex_to_polar` function did not make sense. Now it gives a complex number in component form, for a student to convert into polar form. This is not a perfect function, but at least it is usable now. In future, we may want to have the theta be chosen from amongst a set of multiples/fractions of pi, that tend to make for nicer questions and answers. --- mathgenerator/misc.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/mathgenerator/misc.py b/mathgenerator/misc.py index 80b6b13..b83eaf6 100644 --- a/mathgenerator/misc.py +++ b/mathgenerator/misc.py @@ -176,7 +176,7 @@ def complex_to_polar(min_real_imaginary_num=-20, max_real_imaginary_num=20): | Ex. Problem | Ex. Solution | | --- | --- | - | $19.42(-19.0\theta + i-4.0\theta)$ | $-2.93$ | + | Convert $-14.0+13.0i$ into polar form | $19.1(cos(2.39)+isin(2.39))$ | """ num = complex( random.randint(min_real_imaginary_num, max_real_imaginary_num), @@ -185,9 +185,13 @@ def complex_to_polar(min_real_imaginary_num=-20, max_real_imaginary_num=20): b = num.imag r = round(math.hypot(a, b), 2) theta = round(math.atan2(b, a), 2) + print(a) + print(b) - problem = rf'${r}({a}\theta + i{b}\theta)$' - return problem, f'${theta}$' + + solution = rf'${r}(cos({theta})+isin({theta}))$' + problem = rf'Convert ${a}+{b}i$ into polar form' + return problem, solution def decimal_to_roman_numerals(max_decimal=4000):