def test_f6_simplification_line(): """Is this Fraction's simplification line correct?""" assert Fraction(('+', Product([Item(10), Item(5)]), Product([Item(5), Item(9)])))\ .simplification_line().printed == \ wrap_nb('\\frac{10\\times \\bcancel{5}}{\\bcancel{5}\\times 9}')
class sub_object(object): def __init__(self, numbers_to_use, **options): nb_list = list(numbers_to_use) hole = Item(Value('...')) self.hidden_one = None visible_one = None self.product = Item(Product([nb_list[0], nb_list[1]]).evaluate()) if isinstance(nb_list[1], Fraction): self.hidden_one = nb_list[1] visible_one = nb_list[0] else: nb1 = randomly.pop(nb_list) nb2 = randomly.pop(nb_list) nb_list = [nb1, nb2] self.hidden_one = Item(randomly.pop(nb_list)) visible_one = randomly.pop(nb_list) factors = [visible_one, hole] self.holed_product = Product([randomly.pop(factors), randomly.pop(factors)]) self.holed_product.set_compact_display(False) def q(self, **options): m_expr = Equality([self.holed_product, self.product]).printed return _("Which number can fill the hole in: {math_expr}?")\ .format(math_expr=shared.machine.write_math_style2(m_expr)) def a(self, **options): return shared.machine.write_math_style2(self.hidden_one.printed)
def test_product_sum_2_3x_times_sum_minus4_6x_is_not_reducible(): """Is this Product not reducible?""" p = Product([ Sum([Item(2), Monomial(('+', 3, 1))]), Sum([Item(-4), Monomial(('+', 6, 1))]) ]) assert not p.is_reducible()
def test_f5_simplification_line(): """Is this Fraction's simplification line correct?""" assert Fraction(('+', Product([Item(8), Item(3)]), Product([Item(5), Item(6)])))\ .simplification_line().printed == \ wrap_nb('\\frac{\\bcancel{2}\\times 4\\times \\bcancel{3}}{5\\times' ' \\bcancel{2}\\times \\bcancel{3}}')
def test_f3_simplification_line(): """Is this Fraction's simplification line correct?""" assert Fraction(('+', Product([Item(7), Item(6)]), Product([Item(3), Item(3)])))\ .simplification_line().printed == \ wrap_nb('\\frac{7\\times \\bcancel{3}\\times 2}' '{\\bcancel{3}\\times 3}')
def test_product_9_times_minus2_times_7ab_printed(): """Is this Product correctly printed?""" assert Product([Item(9), Product([Item(-2), Item(7), Item('a'), Item('b')])])\ .printed == wrap_nb('9\\times (-2)\\times 7ab')
def test_fractions_by_items_01_next_step(): """Is this Product's calculation's next step correct?""" p = Product([ Fraction((Item(3), Item(5))), Item(8), Fraction((Item(7), Item(11))), Item(4), Fraction((Item(13), Item(17))) ]) assert p.calculate_next_step().printed == \ wrap_nb('\\frac{3\\times 8\\times 7\\times 4\\times 13}{5\\times 11' '\\times 17}')
def __init__(self, numbers_to_use, **options): super().setup("minimal", **options) super().setup("numbers", nb=numbers_to_use, **options) super().setup("nb_variants", nb=numbers_to_use, **options) product = Product([self.nb1, self.nb2]) self.product_str = product.printed self.result = product.evaluate() if self.context == 'mini_problem': super().setup("mini_problem_wording", q_id=os.path.splitext(os.path.basename(__file__))[0], **options)
def complicated_sum_to_reduce(): return Sum([ Monomial(('+', 3, 1)), Item(4), Sum([ Monomial(('+', 3, 1)), Monomial(('+', 5, 2)), Product([Item(-7), Item('a'), Item('b')]) ]), Product([Item('-a'), Item('b')]), Monomial(('+', 5, 2)), Item(2), Monomial(('-', 2, 1)), Product([Item(2), Item('a'), Item('b')]) ])
def __init__(self, numbers_to_use, **options): super().__init__(numbers_to_use, result_fct=lambda x, y: Product([x, y]), wording=_("How much is the quotient of {nb1} by " "{nb2}?"), permute_nb1_nb2_result=True, **options)
def big_product(): s = Sum(['x', 3]) s.set_exponent(3) p = Product(Monomial((1, 2))) p.set_exponent(3) p1 = Product([2, 3]) p1.set_exponent(3) return Product([ Monomial((2, 1)), Monomial((-4, 2)), s, Item(5), p, Item(('+', -1, 2)), p1 ])
def __init__(self, build_data, **options): super().setup("minimal", **options) super().setup("numbers", nb=build_data, **options) super().setup("nb_variants", nb=build_data, **options) self.transduration = 8 if not is_integer(self.nb1) or not is_integer(self.nb2): self.transduration = 15 product = Product([self.nb1, self.nb2]) self.product_str = product.printed self.result = product.evaluate() if self.context == 'mini_problem': self.transduration = 25 super().setup('mini_problem_wording', q_id=os.path.splitext(os.path.basename(__file__))[0], **options)
def __init__(self, numbers_to_use, **options): MULTIPLE_QUESTIONS = {2: _("What is the double of {nb1}?"), 3: _("What is the triple of {nb1}?"), 4: _("What is the quadruple of {nb1}?")} super().__init__(numbers_to_use, result_fct=lambda x, y: Product([x, y]), wording=MULTIPLE_QUESTIONS[numbers_to_use[0]], shuffle_nbs=False, swap_nb1_nb2=True)
def __init__(self, numbers_to_use, **options): PARTS_QUESTIONS = { 2: _("What is the half of {nb1}?"), 3: _("What is the third of {nb1}?"), 4: _("What is the quarter of {nb1}?") } super().__init__(numbers_to_use, result_fct=lambda x, y: Product([x, y]), wording=PARTS_QUESTIONS[numbers_to_use[0]], shuffle_nbs=False, permute_nb1_nb2_result=True)
def __init__(self, numbers_to_use, **options): nb_list = list(numbers_to_use) hole = Item(Value('...')) self.hidden_one = None visible_one = None self.product = Item(Product([nb_list[0], nb_list[1]]).evaluate()) if isinstance(nb_list[1], Fraction): self.hidden_one = nb_list[1] visible_one = nb_list[0] else: nb1 = randomly.pop(nb_list) nb2 = randomly.pop(nb_list) nb_list = [nb1, nb2] self.hidden_one = Item(randomly.pop(nb_list)) visible_one = randomly.pop(nb_list) factors = [visible_one, hole] self.holed_product = Product([randomly.pop(factors), randomly.pop(factors)]) self.holed_product.set_compact_display(False)
def neg2_inside_exp_sum_of_product_of_1plus1(): return Item(('+', -2, Sum([Product([Sum([1, 1])])])))
def test_neg2_inside_exp_sum_of_product_of_1plus0_printed(): """Is Item(('+', -2, Sum([Product([Sum([1, 0])])]))) correctly printed?""" assert Item(('+', -2, Sum([Product([Sum([1, 0])])]))).printed == \ wrap_nb('-2')
def test_neg2_inside_exp_sum_of_product_of_sum_of_2_printed(): """Is Item(('+', -2, Sum([Product([Sum([2])])]))) correctly printed?""" assert Item(('+', -2, Sum([Product([Sum([2])])]))).printed == \ wrap_nb('(-2)^{2}')
def test_complicated_sum_01(): """Is this Sum correctly printed as 2-10x^{2}+9?""" assert Sum([Item(2), Product([Polynomial([Monomial(('-', 10, 2)), Monomial(('+', 9, 0))])])]).printed == \ wrap_nb('2-10x^{2}+9')
def __init__(self, numbers_to_use, picture='true', **options): super().setup("minimal", **options) if numbers_to_use[0] < 11: raise ValueError('numbers_to_use[0] == {} whereas it should be ' '>= 11'.format(str(numbers_to_use[0]))) numbers_to_use = (numbers_to_use[0] / 10, ) + numbers_to_use[1:] super().setup("numbers", nb=numbers_to_use, shuffle_nbs=False, **options) super().setup("length_units", **options) super().setup("intercept_theorem_figure", butterfly=True, **options) self.figure.setup_labels([True, False, True, True, False, True], segments_list=self.figure.small + self.figure.side) self.line1 = self.figure.small[1].length_name self.line2 = self.figure.side[1].length_name self.wording = _('The drawn figure is out of shape. {newline} ' 'The lengths are given in {length_unit}. ' '{newline} {newline} ' 'Prove that {line1} is parallel to {line2}.') setup_wording_format_of(self) self.ratio1 = shared.machine.write_math_style1( Equality([ Quotient(('+', self.figure.small[0].length_name, self.figure.side[0].length_name)), Quotient(('+', self.figure.small[0].length, self.figure.side[0].length)) ]).printed) self.ratio2 = shared.machine.write_math_style1( Equality([ Quotient(('+', self.figure.small[2].length_name, self.figure.side[2].length_name)), Quotient(('+', self.figure.small[2].length, self.figure.side[2].length)) ]).printed) self.crossproduct = shared.machine.write_math_style1( Equality([ Quotient(('+', Product([ Item(self.figure.small[0].length), Item(self.figure.side[2].length) ]), Item(self.figure.side[0].length))), Item(self.figure.small[2].length) ]).printed) self.equal_ratios = shared.machine.write_math_style1( self.figure.ratios_for_converse().into_str( as_a_quotients_equality=True)) ans_variant = options.get('ans_variant', 'default') ans_texts = { 'default': _('\\begin{tabular}{ll}' 'We have: & ' '{main_vertex_name} {belongs_to} {chunk0_length_name}' ' \\\\ ' ' & ' '{main_vertex_name} {belongs_to} {chunk1_length_name}' ' \end{tabular}'), 'alternative2': _('{vertex1_name}, {main_vertex_name} and ' '{point0_name} on one hand, ' '{vertex2_name}, {main_vertex_name} and ' '{point1_name} on the other hand,' 'are aligned in the same order. ') } ratios = _('\\begin{multicols}{2} ' 'On one hand: {ratio1} ' 'and on the other hand: {ratio2} ' '\end{multicols} ' 'ans as: {crossproduct} ' 'then: {equal_ratios} {newline} ') conclusion = _('Hence by the converse of the intercept theorem, ' '{line1} is parallel to {line2}.') self.answer_wording = ans_texts[ans_variant] + ratios + conclusion setup_wording_format_of(self, w_prefix='answer_')
def fp1(): return Product([Fraction((14, 7)), Fraction((12, 12))]) @pytest.fixture
def test_sum_3squareplus5(): """Is this Sum correctly printed as 3^{2}+5?""" p = Product([Item(3)]) p.set_exponent(2) assert Sum([p, Item(5)]).printed == wrap_nb('3^{2}+5')
def __init__(self, q_kind='default_nothing', **options): self.derived = True # The call to the mother class __init__() method will set the # fields matching optional arguments which are so far: # self.q_kind, self.q_subkind # plus self.options (modified) Q_Structure.__init__(self, q_kind, AVAILABLE_Q_KIND_VALUES, **options) # The purpose of this next line is to get the possibly modified # value of **options options = self.options init_caller = INIT_CALLER[q_kind] self.expandable_objct = None self.numeric_aux = None if q_kind == 'any_basic_expd': randomly_drawn = randomly.decimal_0_1() if randomly_drawn <= 0.25: self.expandable_objct = Expandable((RANDOMLY, 'monom0_polyn1'), randomly_reversed=0.5) elif randomly_drawn <= 0.50: self.expandable_objct = Expandable((RANDOMLY, 'monom1_polyn1'), randomly_reversed=0.5) else: self.expandable_objct = Expandable((RANDOMLY, 'polyn1_polyn1')) elif q_kind in ['monom0_polyn1', 'monom1_polyn1']: self.expandable_objct = Expandable((RANDOMLY, q_kind), randomly_reversed=0.5) elif q_kind == 'monom01_polyn1': self.expandable_objct = Expandable( (RANDOMLY, randomly.pop(['monom0_polyn1', 'monom1_polyn1'])), randomly_reversed=0.5) elif q_kind == 'polyn1_polyn1': self.expandable_objct = Expandable((RANDOMLY, 'polyn1_polyn1')) elif q_kind == 'sum_of_any_basic_expd': if self.q_subkind in ['harder', 'with_a_binomial']: # __ choices = ['monom0_polyn1', 'monom1_polyn1'] drawn_types = list() drawn_types.append(randomly.pop(choices)) if self.q_subkind == 'with_a_binomial': drawn_types.append('any_binomial') else: drawn_types.append('minus_polyn1_polyn1') aux_expd_list = list() for t in drawn_types: if t == 'any_binomial': aux_expd_list.append( BinomialIdentity((RANDOMLY, 'any'), **options)) else: aux_expd_list.append(Expandable((RANDOMLY, t))) final_list = list() for i in range(len(aux_expd_list)): final_list.append(randomly.pop(aux_expd_list)) self.expandable_objct = Sum(final_list) elif self.q_subkind == 'easy': choices = ['monom0_polyn1', 'monom1_polyn1'] aux_expd_list = list() aux_expd_list.append( Expandable((RANDOMLY, randomly.pop(choices)))) if randomly.heads_or_tails(): aux_expd_list.append(Expandable((RANDOMLY, 'sign_exp'))) else: aux_expd_list.append( Monomial((RANDOMLY, 15, randomly.integer(0, 2)))) final_list = list() for i in range(len(aux_expd_list)): final_list.append(randomly.pop(aux_expd_list)) self.expandable_objct = Sum(final_list) else: choices = [ 'monom0_polyn1', 'monom0_polyn1', 'monom1_polyn1', 'monom1_polyn1', 'polyn1_polyn1', 'minus_polyn1_polyn1' ] drawn_types = list() drawn_types.append(randomly.pop(choices)) drawn_types.append(randomly.pop(choices)) aux_expd_list = list() for element in drawn_types: aux_expd_list.append(Expandable((RANDOMLY, element))) aux_expd_list.append(Monomial((RANDOMLY, 15, 2))) final_list = list() for i in range(len(aux_expd_list)): final_list.append(randomly.pop(aux_expd_list)) self.expandable_objct = Sum(final_list) elif q_kind in ['sign_expansion', 'sign_expansion_short_test']: sign_exp_kind = options.get('sign_exp_kind', 0) if q_kind == 'sign_expansion_short_test': sign_exp_kind = 1 if sign_exp_kind == 0: sign_exp_kind = randomly.integer(1, 5) # Creation of the terms aux_terms_list = list() aux_expd_1 = Expandable((Monomial( (randomly.sign(), 1, 0)), Polynomial((RANDOMLY, 15, 2, 2)))) aux_expd_2 = Expandable((Monomial( (randomly.sign(), 1, 0)), Polynomial((RANDOMLY, 15, 2, 2)))) aux_expd_3 = Expandable((Monomial( (randomly.sign(), 1, 0)), Polynomial((RANDOMLY, 15, 2, 2)))) long_aux_expd = Expandable((Monomial( (randomly.sign(), 1, 0)), Polynomial((RANDOMLY, 15, 2, 3)))) if q_kind == 'sign_expansion_short_test': long_aux_expd = Expandable((Monomial( ('-', 1, 0)), Polynomial((RANDOMLY, 15, 2, 3)))) aux_monomial = Monomial((RANDOMLY, 15, 2)) # 1st kind: a Monomial and ± (long Polynomial) # (like in a short test) if sign_exp_kind == 1: aux_terms_list.append(long_aux_expd) aux_terms_list.append(aux_monomial) # 2d kind: ± (x+3) ± (4x - 7) elif sign_exp_kind == 2: aux_terms_list.append(aux_expd_1) aux_terms_list.append(aux_expd_2) # 3d kind: ± (x+3) ± (4x - 7) ± (x² - 5x) elif sign_exp_kind == 3: aux_terms_list.append(aux_expd_1) aux_terms_list.append(aux_expd_2) aux_terms_list.append(aux_expd_3) # 4th kind: ± (x+3) ± (4x - 7) ± Monomial elif sign_exp_kind == 4: aux_terms_list.append(aux_expd_1) aux_terms_list.append(aux_expd_2) aux_terms_list.append(aux_monomial) # 5th kind: ± (x+3) ± Monomial ± (long Polynomial) elif sign_exp_kind == 5: aux_terms_list.append(aux_expd_2) aux_terms_list.append(aux_monomial) aux_terms_list.append(long_aux_expd) # add as many possibilities as wanted, # don't forget to increase the last number here: # sign_exp_kind = randomly.integer(1, 5) (what's a bit above) # Now let's distribute the terms randomly final_terms_list = list() for i in range(len(aux_terms_list)): final_terms_list.append(randomly.pop(aux_terms_list)) self.expandable_objct = Sum(final_terms_list) elif q_kind in [ 'numeric_sum_square', 'numeric_difference_square', 'numeric_squares_difference' ]: # __ self.expandable_objct = init_caller( (options['couple'][0], options['couple'][1]), **options) if q_kind in ['numeric_sum_square', 'numeric_difference_square']: self.numeric_aux = Sum( [options['couple'][0], options['couple'][1]]).reduce_() self.numeric_aux.set_exponent(2) else: # squares_difference's case aux1 = Sum([options['couple'][0], options['couple'][1]]).reduce_() temp = options['couple'][1].clone() temp.set_sign('-') aux2 = Sum([options['couple'][0], temp]).reduce_() self.numeric_aux = Product([aux1, aux2]) else: if q_kind == 'any_binomial': q_kind = 'any' self.expandable_objct = init_caller((RANDOMLY, q_kind), **options) # Creation of the expression: number = 0 if 'expression_number' in options \ and is_.a_natural_int(options['expression_number']): # __ number = options['expression_number'] self.expression = Expression(number, self.expandable_objct) if self.numeric_aux is not None: self.numeric_aux = Expression(number, self.numeric_aux)
def __init__(self, numbers_to_use, **options): super().setup("minimal", **options) if self.variant == 'with_a_decimal': second_couple = shared.mc_source.next('int_deci_clever_pairs') elif self.variant == 'integers_only': second_couple = shared.mc_source.next('intpairs_2to500', clever_in=[4, 5]) elif self.variant == 'fifths_series': second_couple = shared.mc_source.next('intpairs_2to500', clever=5, union={ 'table_name': 'int_deci_clever_pairs', 'clever': 5 }) elif self.variant == 'quarters_series': second_couple = shared.mc_source.next('intpairs_2to500', clever=4, union={ 'table_name': 'int_deci_clever_pairs', 'clever': 4 }) else: second_couple = shared.mc_source.next( 'intpairs_2to500', clever_in=[4, 5, 10], union={'table_name': 'int_deci_clever_pairs'}) # We shuffle the numbers in a special way if len(numbers_to_use) == 1: # There is only one number in addition to the 2 special ones # so either we put it in the first place, or in the middle # (not at the end, otherwise, the 2 special ones would be at the # two first places) special_ones = list(second_couple) random.shuffle(special_ones) all_nb = [list(numbers_to_use).pop(0), special_ones.pop(0)] random.shuffle(all_nb) all_nb += [special_ones.pop(0)] else: # It is assumed there are 2 numbers_to_use (so 4 altogether) # In order to avoid having the two "special" numbers in a row at # the two first places, we will shuffle all numbers in a special # way. if random.choice([True, False]): # In this case, the first number will NOT be from the "special" # ones, so no matter what follows nb_to_use = list(numbers_to_use) random.shuffle(nb_to_use) all_nb = [nb_to_use.pop(0)] remaining = nb_to_use + list(second_couple) else: # In this case, it will, so, at the second place we put a # number from the other source (the user specified one) first_couple = list(numbers_to_use) random.shuffle(first_couple) nb_to_use = list(second_couple) random.shuffle(nb_to_use) all_nb = [nb_to_use.pop(0), first_couple.pop(0)] remaining = nb_to_use + first_couple random.shuffle(remaining) all_nb += remaining super().setup("numbers", nb=all_nb, shuffle_nbs=False, **options) product = Product( [getattr(self, 'nb' + str(i + 1)) for i in range(self.nb_nb)]) self.product_str = product.printed self.result = product.evaluate()
def __init__(self, q_kind='default_nothing', **options): self.derived = True # The call to the mother class __init__() method will set the # fields matching optional arguments which are so far: # self.q_kind, self.q_subkind # plus self.options (modified) Q_Structure.__init__(self, q_kind, AVAILABLE_Q_KIND_VALUES, **options) # The purpose of this next line is to get the possibly modified # value of **options options = self.options # That's the number of the question, not of the expressions it might # contain ! self.number = "" if 'number_of_questions' in options: self.number = options['number_of_questions'] self.objct = None # 1st OPTION if q_kind == 'fraction_simplification': root = randomly.integer(2, 19, weighted_table=[ 0.225, 0.225, 0, 0.2, 0, 0.2, 0, 0, 0, 0.07, 0, 0.0375, 0, 0, 0, 0.0375, 0, 0.005 ]) factors_list = [j + 1 for j in range(10)] ten_power_factor1 = 1 ten_power_factor2 = 1 if 'with_ten_powers' in options \ and is_.a_number(options['with_ten_powers']) \ and options['with_ten_powers'] <= 1 \ and options['with_ten_powers'] >= 0: # __ if randomly.decimal_0_1() < options['with_ten_powers']: ten_powers_list = [10, 10, 100, 100] ten_power_factor1 = randomly.pop(ten_powers_list) ten_power_factor2 = randomly.pop(ten_powers_list) self.objct = Fraction( ('+', root * randomly.pop(factors_list) * ten_power_factor1, root * randomly.pop(factors_list) * ten_power_factor2)) # 2d & 3d OPTIONS # Fractions Products | Quotients elif q_kind in ['fractions_product', 'fractions_quotient']: # In some cases, the fractions will be generated # totally randomly if randomly.decimal_0_1() < 0: lil_box = [n + 2 for n in range(18)] a = randomly.pop( lil_box, weighted_table=FRACTION_PRODUCT_AND_QUOTIENT_TABLE) b = randomly.pop( lil_box, weighted_table=FRACTION_PRODUCT_AND_QUOTIENT_TABLE) lil_box = [n + 2 for n in range(18)] c = randomly.pop( lil_box, weighted_table=FRACTION_PRODUCT_AND_QUOTIENT_TABLE) d = randomly.pop( lil_box, weighted_table=FRACTION_PRODUCT_AND_QUOTIENT_TABLE) f1 = Fraction((randomly.sign(plus_signs_ratio=0.75), Item((randomly.sign(plus_signs_ratio=0.80), a)), Item( (randomly.sign(plus_signs_ratio=0.80), b)))) f2 = Fraction((randomly.sign(plus_signs_ratio=0.75), Item((randomly.sign(plus_signs_ratio=0.80), c)), Item( (randomly.sign(plus_signs_ratio=0.80), d)))) # f1 = f1.simplified() # f2 = f2.simplified() # In all other cases (80%), we'll define a "seed" a plus two # randomly numbers i and j to form the Product | Quotient: # a×i / b × c / a × j # Where b is a randomly number coprime to a×i # and c is a randomly number coprime to a×j else: a = randomly.integer(2, 8) lil_box = [i + 2 for i in range(7)] i = randomly.pop(lil_box) j = randomly.pop(lil_box) b = randomly.coprime_to(a * i, [n + 2 for n in range(15)]) c = randomly.not_coprime_to(b, [n + 2 for n in range(30)], excepted=a * j) f1 = Fraction( (randomly.sign(plus_signs_ratio=0.75), Item((randomly.sign(plus_signs_ratio=0.80), a * i)), Item((randomly.sign(plus_signs_ratio=0.80), b)))) f2 = Fraction( (randomly.sign(plus_signs_ratio=0.75), Item((randomly.sign(plus_signs_ratio=0.80), c)), Item((randomly.sign(plus_signs_ratio=0.80), a * j)))) if randomly.heads_or_tails(): f3 = f1.clone() f1 = f2.clone() f2 = f3.clone() if q_kind == 'fractions_quotient': f2 = f2.invert() if q_kind == 'fractions_product': self.objct = Product([f1, f2]) elif q_kind == 'fractions_quotient': self.objct = Quotient(('+', f1, f2, 1, 'use_divide_symbol')) # 4th OPTION # Fractions Sums elif q_kind == 'fractions_sum': randomly_position = randomly\ .integer(0, 16, weighted_table=FRACTIONS_SUMS_SCALE_TABLE) chosen_seed_and_generator = FRACTIONS_SUMS_TABLE[randomly_position] seed = randomly.integer(2, chosen_seed_and_generator[1]) # The following test is only intended to avoid having "high" # results too often. We just check if the common denominator # will be higher than 75 (arbitrary) and if yes, we redetermine # it once. We don't do it twice since we don't want to totally # forbid high denominators. if seed * chosen_seed_and_generator[0][0] \ * chosen_seed_and_generator[0][1] >= 75: # __ seed = randomly.integer(2, chosen_seed_and_generator[1]) lil_box = [0, 1] gen1 = chosen_seed_and_generator[0][lil_box.pop()] gen2 = chosen_seed_and_generator[0][lil_box.pop()] den1 = Item(gen1 * seed) den2 = Item(gen2 * seed) temp1 = randomly.integer(1, 20) temp2 = randomly.integer(1, 20) num1 = Item(temp1 // gcd(temp1, gen1 * seed)) num2 = Item(temp2 // gcd(temp2, gen2 * seed)) f1 = Fraction((randomly.sign(plus_signs_ratio=0.7), num1, den1)) f2 = Fraction((randomly.sign(plus_signs_ratio=0.7), num2, den2)) self.objct = Sum([f1.simplified(), f2.simplified()]) # 5th # still to imagine:o) # Creation of the expression: number = 0 if 'expression_number' in options \ and is_.a_natural_int(options['expression_number']): # __ number = options['expression_number'] self.expression = Expression(number, self.objct)
def __init__(self, numbers_to_use, **options): nb_list = list(numbers_to_use) self.nb1, self.nb2 = random.sample(nb_list, 2) self.product = Product([self.nb1, self.nb2]).evaluate()
def a(self, **options): if self.product == 12: return _("{product1} or {product2}")\ .format(product1=shared.machine.write_math_style2( Product([2, 6]).printed), product2=shared.machine.write_math_style2( Product([3, 4]).printed)) elif self.product == 16: return _("{product1} or {product2}")\ .format(product1=shared.machine.write_math_style2( Product([2, 8]).printed), product2=shared.machine.write_math_style2( Product([4, 4]).printed)) elif self.product == 18: return _("{product1} or {product2}")\ .format(product1=shared.machine.write_math_style2( Product([2, 9]).printed), product2=shared.machine.write_math_style2( Product([3, 6]).printed)) elif self.product == 24: return _("{product1} or {product2}")\ .format(product1=shared.machine.write_math_style2( Product([3, 8]).printed), product2=shared.machine.write_math_style2( Product([4, 6]).printed)) elif self.product == 36: return _("{product1} or {product2}")\ .format(product1=shared.machine.write_math_style2( Product([6, 6]).printed), product2=shared.machine.write_math_style2( Product([4, 9]).printed)) else: return shared.machine.write_math_style2( Product([self.nb1, self.nb2]).printed)
def test_sum_neg1plusx_bis(): """Is this Sum correctly printed as -1+x?""" assert Sum([Product(Item(-1)), Item('x')]).printed == wrap_nb('-1+x')
def test_sum_1plusx(): """Is this Sum correctly printed as 1+x?""" assert Sum([Product(Item(1)), Item('x')]).printed == wrap_nb('1+x')
def fp0(): return Product([Fraction((5, 4)), Fraction((5, 5))]) @pytest.fixture
def fp2(): return Product([Fraction((9, -2)), Fraction((-8, 10))]) @pytest.fixture
def __init__(self, build_data, **options): super().setup("minimal", **options) if self.variant == 'with_a_decimal': second_couple = shared.mc_source.next('int_deci_clever_pairs') elif self.variant == 'integers_only': second_couple = shared.mc_source.next('intpairs_2to500', clever_in=[4, 5]) elif self.variant == 'fifths_series': second_couple = shared.mc_source.next('intpairs_2to500', clever=5, union={ 'table_name': 'int_deci_clever_pairs', 'clever': 5}) elif self.variant == 'quarters_series': second_couple = shared.mc_source.next('intpairs_2to500', clever=4, union={ 'table_name': 'int_deci_clever_pairs', 'clever': 4}) else: second_couple = shared.mc_source.next('intpairs_2to500', clever_in=[4, 5, 10], union={ 'table_name': 'int_deci_clever_pairs'}) # We shuffle the numbers in a special way if len(build_data) == 1: # There is only one number in addition to the 2 special ones # so either we put it in the first place, or in the middle # (not at the end, otherwise, the 2 special ones would be at the # two first places) special_ones = list(second_couple) random.shuffle(special_ones) all_nb = [list(build_data).pop(0), special_ones.pop(0)] random.shuffle(all_nb) all_nb += [special_ones.pop(0)] else: # It is assumed there are 2 build_data (so 4 altogether) # In order to avoid having the two "special" numbers in a row at # the two first places, we will shuffle all numbers in a special # way. if random.choice([True, False]): # In this case, the first number will NOT be from the "special" # ones, so no matter what follows nb_to_use = list(build_data) random.shuffle(nb_to_use) all_nb = [nb_to_use.pop(0)] remaining = nb_to_use + list(second_couple) else: # In this case, it will, so, at the second place we put a # number from the other source (the user specified one) first_couple = list(build_data) random.shuffle(first_couple) nb_to_use = list(second_couple) random.shuffle(nb_to_use) all_nb = [nb_to_use.pop(0), first_couple.pop(0)] remaining = nb_to_use + first_couple random.shuffle(remaining) all_nb += remaining super().setup("numbers", nb=all_nb, shuffle_nbs=False, **options) self.transduration = 15 product = Product([getattr(self, 'nb' + str(i + 1)) for i in range(self.nb_nb)]) self.product_str = product.printed self.result = product.evaluate()