Exemplo n.º 1
0
def _differentiate_polynomial(value, sample_args, context, num_variables):
  """Generates a question for differentiating a polynomial."""
  is_question = context is None
  if context is None:
    context = composition.Context()

  if value is not None:
    num_variables = value.coefficients.ndim

  entropy, sample_args = sample_args.peel()
  max_derivative_order = 3
  derivative_order = random.randint(1, max_derivative_order)
  entropy = max(0, entropy - math.log10(max_derivative_order))

  derivative_axis = random.randint(0, num_variables - 1)
  if value is None:
    coefficients = _generate_polynomial(
        num_variables, entropy, derivative_order, derivative_axis)
  else:
    coefficients = _sample_integrand(
        value.coefficients, derivative_order, derivative_axis, entropy)

  (entity,) = context.sample(
      sample_args, [composition.Polynomial(coefficients)])

  value = coefficients
  for _ in range(derivative_order):
    value = polynomials.differentiate(value, axis=derivative_axis)
  nth = display.StringOrdinal(derivative_order)

  if entity.has_expression():
    polynomial = entity.expression
    variables = entity.polynomial_variables
  else:
    variables = [sympy.Symbol(context.pop()) for _ in range(num_variables)]
    polynomial = entity.handle.apply(*variables)
  variable = variables[derivative_axis]

  if is_question:
    template = _template(context.module_count, derivative_order, len(variables))
    answer = polynomials.coefficients_to_polynomial(value, variables).sympy()
    return example.Problem(
        question=example.question(
            context, template, eq=polynomial, var=variable, nth=nth),
        answer=answer)
  else:
    fn_symbol = context.pop()
    variables_string = ', '.join(str(variable) for variable in variables)
    assert len(variables) == 1  # since below we don't specify var we diff wrt
    return composition.Entity(
        context=context,
        value=composition.Polynomial(value),
        description='Misalkan {fn} ({variables}) menjadi turunan ke-{nth} dari {eq}.',
        handle=composition.FunctionHandle(fn_symbol),
        fn=fn_symbol, variables=variables_string, nth=nth, eq=polynomial)
Exemplo n.º 2
0
def nearest_integer_root(sample_args):
  """E.g., "Calculate the cube root of 35 to the nearest integer."."""
  context = composition.Context()

  # With at least 50% probability, pick square or cube root (these are most
  # important roots!).
  if random.choice([False, True]):
    one_over_exponent = random.randint(2, 3)
  else:
    one_over_exponent = random.randint(2, 10)

  entropy, sample_args = sample_args.peel()
  value = number.integer(entropy, signed=False)
  answer = int(round(value ** (1 / one_over_exponent)))

  templates = [
      'What is {value} to the power of 1/{one_over_exponent}, to the nearest'
      ' integer?',
  ]

  if one_over_exponent != 2:  # "What is the second root of 4?" never used.
    ordinal = str()
    templates += [
        'What is the {ordinal} root of {value} to the nearest integer?',
    ]

  if one_over_exponent == 2:
    templates += [
        'What is the square root of {value} to the nearest integer?',
    ]
  elif one_over_exponent == 3:
    templates += [
        'What is the cube root of {value} to the nearest integer?',
    ]

  template = random.choice(templates)

  ordinal = display.StringOrdinal(one_over_exponent)
  return example.Problem(
      question=example.question(
          context, template, value=value, ordinal=ordinal,
          one_over_exponent=one_over_exponent),
      answer=answer)
Exemplo n.º 3
0
def kth_biggest(sample_args, count=None):
    """Asks for the kth biggest value in a list."""
    sample_args = sample_args()
    context = composition.Context()

    entropy, sample_args = sample_args.peel()
    values = _unique_values(entropy, count=count)
    count = len(values)

    display_multichoice = random.choice([False, True])
    if display_multichoice:
        _mark_choice_letters_used(count, context)

    entities = context.sample(sample_args, values)
    sorted_entities = sorted(entities, key=_entity_sort_key)
    ordinal = random.randint(1, count)

    if random.choice([False, True]):
        # Do from biggest.
        answer = sorted_entities[-ordinal]
        adjective = 'terbesar'
    else:
        # Do from smallest.
        answer = sorted_entities[ordinal - 1]
        adjective = 'terkecil'

    if ordinal > 1:
        adjective = str(display.StringOrdinal(ordinal)) + ' ' + adjective

    if display_multichoice:
        return _kth_biggest_multichoice_question(context=context,
                                                 entities=entities,
                                                 adjective=adjective,
                                                 answer=answer)
    else:
        return _kth_biggest_list_question(context=context,
                                          entities=entities,
                                          adjective=adjective,
                                          answer=answer)
Exemplo n.º 4
0
 def testCreate_errorIfNegative(self):
   with self.assertRaisesRegexp(ValueError, 'Unsupported ordinal'):
     display.StringOrdinal(-1)
Exemplo n.º 5
0
 def testBasic(self):
   ordinal = display.StringOrdinal(0)
   self.assertEqual(str(ordinal), 'zeroth')
   ordinal = display.StringOrdinal(10)
   self.assertEqual(str(ordinal), 'tenth')