def _question(self): d = self.difficulty if d == 1: # Positive integer solution a = r_int(5, '+') b = r_int(10, '-') * a left_side = '{a}*x+{b}'.format(a=a, b=b) right_side = '0' elif d == 2: # Real solution/no solution/infinite solutions a = r_int(10, '-+0', weights={0: 2}) b = r_int(10, '-+0', weights={0: 4}) left_side = '{a}*x+{b}'.format(a=a, b=b) right_side = '0' else: # Real solution/no solution/infinite solutions, any number of terms. left_side, right_side = self._hard_diff_left_and_right( x_terms=self.x_terms, non_x_terms=self.non_x_terms) # Add '0' if the side is empty. left_side = left_side or '0' right_side = right_side or '0' final_string = '='.join([left_side, right_side]) final_string = final_string.replace('+-', '-') return final_string.replace('x', self.var_name)
def test_0_excluded(self): s = set() small_n = 1 # Odds of missing the bug for enough reps is extremely small # (about 10e-200 for 1k reps) for _ in range(REPETITIONS): s.update({ r_int(small_n, '+'), r_int(small_n, '-'), r_int(small_n, '-+'), }) self.assertNotIn(0, s)
def test_0_included(self): s = set() small_n = 1 # Odds of missing the bug for enough reps is extremely small # (about 10e-200 for 1k reps) for _ in range(10 * 4): s.update({ r_int(small_n, '+0'), r_int(small_n, '-0'), r_int(small_n, '-0+'), r_int(small_n, ) }) self.assertIn(0, s)
def test_weight_effect_on_odds(self): nums_lst = [] weight_1 = 3 for _ in range(REPETITIONS): nums_lst.append(r_int((0, 2), weights={1: weight_1})) ratio_1 = nums_lst.count(1) / len(nums_lst) weight_ratio_1 = weight_1 / (1 + weight_1 + 1) self.assertAlmostEqual(weight_ratio_1, ratio_1, delta=.01)
def _non_x_terms_strings(non_x_terms): return [str(r_int(10, '-+')) for _ in range(non_x_terms)]
def _x_terms_strings(x_terms): return ['{}*x'.format(r_int(10, '-+')) for _ in range(x_terms)]
def test_weight_of_negative_or_0_doesnt_raise(self): self.assertIsNotNone(r_int((-1, 2), weights={-1: 4, 0: 4}))
def test_0bound_exactly_0(self): self.assertEquals(r_int(0), 0)
def test_excluded(self): for _ in range(REPETITIONS): bound = 3 excluded_n = randint(-bound, bound) self.assertNotEqual(excluded_n, r_int(bound, excluded={excluded_n}))
def test_only_positives(self): for _ in range(REPETITIONS): self.assertGreater(r_int(100, '+'), 0)
def test_only_negatives(self): for _ in range(REPETITIONS): self.assertLess(r_int(100, '-'), 0)
def test_within_lower_bound(self): for _ in range(REPETITIONS): bound = randint(0, 100) self.assertGreaterEqual(r_int(bound), -bound)
def test_within_upper_bound(self): for _ in range(REPETITIONS): bound = randint(0, 100) self.assertLessEqual(r_int(bound), bound)
def test_is_int(self): self.assertTrue(isinstance(r_int(10000), int))
def test_XX_bounds_exactly_X(self): self.assertEquals(r_int((4, 4)), 4)