示例#1
0
def base_conversion(min_entropy, max_entropy):
    """E.g., "What is 17 base 8 in base 10?"."""
    context = composition.Context()

    from_base = random.randint(2, 16)
    while True:
        to_base = random.randint(2, 16)
        if to_base != from_base:
            break

    # Entropy used up in selecting bases.
    entropy_used = math.log10(16 * 15)
    entropy = random.uniform(min_entropy - entropy_used,
                             max_entropy - entropy_used)

    value = number.integer(entropy, signed=True)
    template = random.choice([
        '{from_str} (basis {from_base}) ke basis {to_base}',
        'Ubah {from_str} (basis {from_base}) menjadi basis {to_base}.',
        'Apa {from_str} (basis {from_base}) di basis {to_base}?',
    ])
    return example.Problem(question=example.question(
        context,
        template,
        from_str=display.NumberInBase(value, from_base),
        from_base=from_base,
        to_base=to_base),
                           answer=display.NumberInBase(value, to_base))
示例#2
0
 def testBasic(self):
   self.assertEqual(str(display.NumberInBase(1, 10)), '1')
   self.assertEqual(str(display.NumberInBase(-1, 10)), '-1')
   self.assertEqual(str(display.NumberInBase(1, 2)), '1')
   self.assertEqual(str(display.NumberInBase(-1, 2)), '-1')
   self.assertEqual(str(display.NumberInBase(2, 2)), '10')
   self.assertEqual(str(display.NumberInBase(-2, 2)), '-10')
   self.assertEqual(str(display.NumberInBase(10, 16)), 'a')
   self.assertEqual(str(display.NumberInBase(16, 16)), '10')
   self.assertEqual(str(display.NumberInBase(256, 16)), '100')
   self.assertEqual(str(display.NumberInBase(-75483, 10)), '-75483')
def add_or_sub_in_base(sample_args):
  """Module for addition and subtraction in another base."""
  context = composition.Context()
  entropy, sample_args = sample_args.peel()
  entropy_p, entropy_q = _entropy_for_pair(entropy)
  p = number.integer(entropy_p, signed=True)
  q = number.integer(entropy_q, signed=True)
  base = random.randint(2, 16)
  if random.choice([False, True]):
    answer = p + q
    template = 'In base {base}, what is {p} + {q}?'
  else:
    answer = p - q
    template = 'In base {base}, what is {p} - {q}?'
  return example.Problem(
      question=example.question(
          context,
          template,
          base=base,
          p=display.NumberInBase(p, base),
          q=display.NumberInBase(q, base)),
      answer=display.NumberInBase(answer, base))