def test_with_9(self):
     n = 9
     sequences, operations = optimal_sequence(n)
     self.assertEqual([[1, 3, 9]], sequences)
     self.assertEqual([['multiply_by_three', 'multiply_by_three']],
                      operations)
     self.assert_sequences_and_operations(n, sequences, operations)
Пример #2
0
def test_optimal_sequence(n, expected):
    result = optimal_sequence(n)
    assert len(result) == expected
    assert result[-1] == n
    for i in range(1, len(result)):
        assert any(
            (result[i] == result[i - 1] + 1, result[i] == 2 * result[i - 1],
             result[i] == 3 * result[i - 1]))
 def test_with_7(self):
     n = 7
     sequences, operations = optimal_sequence(n)
     self.assertEqual([[1, 2, 6, 7], [1, 2, 6, 7], [1, 3, 6, 7]], sequences)
     self.assertEqual([['multiply_by_two', 'multiply_by_three', 'add_one'],
                       ['add_one', 'multiply_by_three', 'add_one'],
                       ['multiply_by_three', 'multiply_by_two', 'add_one']],
                      operations)
     self.assert_sequences_and_operations(n, sequences, operations)
 def test_with_2(self):
     n = 2
     sequences, operations = optimal_sequence(n)
     self.assertEqual([[1, 2], [1, 2]], sequences)
     self.assertEqual([['multiply_by_two'], ['add_one']], operations)
     self.assert_sequences_and_operations(n, sequences, operations)
 def test_with_1(self):
     n = 1
     sequences, operations = optimal_sequence(n)
     self.assertEqual([[1]], sequences)
     self.assertEqual([[]], operations)
     self.assert_sequences_and_operations(n, sequences, operations)
 def test_with_0(self):
     with self.assertRaisesRegex(AssertionError, ''):
         optimal_sequence(0)
 def test_with_96234(self):
     n = 96234
     sequences, operations = optimal_sequence(n)
     self.assertEqual([[
         1, 3, 9, 10, 11, 22, 66, 198, 594, 1782, 5346, 16038, 16039, 32078,
         96234
     ],
                       [
                           1, 2, 6, 7, 21, 22, 66, 198, 594, 1782, 5346,
                           16038, 16039, 32078, 96234
                       ],
                       [
                           1, 2, 6, 7, 21, 22, 66, 198, 594, 1782, 5346,
                           16038, 16039, 32078, 96234
                       ],
                       [
                           1, 3, 6, 7, 21, 22, 66, 198, 594, 1782, 5346,
                           16038, 16039, 32078, 96234
                       ],
                       [
                           1, 3, 9, 10, 11, 33, 66, 198, 594, 1782, 5346,
                           16038, 16039, 32078, 96234
                       ],
                       [
                           1, 3, 9, 10, 11, 33, 99, 198, 594, 1782, 5346,
                           16038, 16039, 32078, 96234
                       ],
                       [
                           1, 3, 9, 10, 11, 33, 99, 297, 594, 1782, 5346,
                           16038, 16039, 32078, 96234
                       ],
                       [
                           1, 3, 9, 10, 11, 33, 99, 297, 891, 1782, 5346,
                           16038, 16039, 32078, 96234
                       ],
                       [
                           1, 3, 9, 10, 11, 33, 99, 297, 891, 2673, 5346,
                           16038, 16039, 32078, 96234
                       ],
                       [
                           1, 3, 9, 10, 11, 33, 99, 297, 891, 2673, 8019,
                           16038, 16039, 32078, 96234
                       ],
                       [
                           1, 3, 9, 10, 11, 22, 66, 198, 594, 1782, 5346,
                           16038, 16039, 48117, 96234
                       ],
                       [
                           1, 2, 6, 7, 21, 22, 66, 198, 594, 1782, 5346,
                           16038, 16039, 48117, 96234
                       ],
                       [
                           1, 2, 6, 7, 21, 22, 66, 198, 594, 1782, 5346,
                           16038, 16039, 48117, 96234
                       ],
                       [
                           1, 3, 6, 7, 21, 22, 66, 198, 594, 1782, 5346,
                           16038, 16039, 48117, 96234
                       ],
                       [
                           1, 3, 9, 10, 11, 33, 66, 198, 594, 1782, 5346,
                           16038, 16039, 48117, 96234
                       ],
                       [
                           1, 3, 9, 10, 11, 33, 99, 198, 594, 1782, 5346,
                           16038, 16039, 48117, 96234
                       ],
                       [
                           1, 3, 9, 10, 11, 33, 99, 297, 594, 1782, 5346,
                           16038, 16039, 48117, 96234
                       ],
                       [
                           1, 3, 9, 10, 11, 33, 99, 297, 891, 1782, 5346,
                           16038, 16039, 48117, 96234
                       ],
                       [
                           1, 3, 9, 10, 11, 33, 99, 297, 891, 2673, 5346,
                           16038, 16039, 48117, 96234
                       ],
                       [
                           1, 3, 9, 10, 11, 33, 99, 297, 891, 2673, 8019,
                           16038, 16039, 48117, 96234
                       ]], sequences)
     self.assertEqual(
         [[
             'multiply_by_three', 'multiply_by_three', 'add_one', 'add_one',
             'multiply_by_two', 'multiply_by_three', 'multiply_by_three',
             'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
             'multiply_by_three', 'add_one', 'multiply_by_two',
             'multiply_by_three'
         ],
          [
              'multiply_by_two', 'multiply_by_three', 'add_one',
              'multiply_by_three', 'add_one', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_two', 'multiply_by_three'
          ],
          [
              'add_one', 'multiply_by_three', 'add_one',
              'multiply_by_three', 'add_one', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_two', 'multiply_by_three'
          ],
          [
              'multiply_by_three', 'multiply_by_two', 'add_one',
              'multiply_by_three', 'add_one', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_two', 'multiply_by_three'
          ],
          [
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'add_one', 'multiply_by_three', 'multiply_by_two',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_two', 'multiply_by_three'
          ],
          [
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'add_one', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_two', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_two', 'multiply_by_three'
          ],
          [
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'add_one', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_two', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_two', 'multiply_by_three'
          ],
          [
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'add_one', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_two',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_two', 'multiply_by_three'
          ],
          [
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'add_one', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_two', 'multiply_by_three', 'add_one',
              'multiply_by_two', 'multiply_by_three'
          ],
          [
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'add_one', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_two', 'add_one',
              'multiply_by_two', 'multiply_by_three'
          ],
          [
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'add_one', 'multiply_by_two', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_three', 'multiply_by_two'
          ],
          [
              'multiply_by_two', 'multiply_by_three', 'add_one',
              'multiply_by_three', 'add_one', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_three', 'multiply_by_two'
          ],
          [
              'add_one', 'multiply_by_three', 'add_one',
              'multiply_by_three', 'add_one', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_three', 'multiply_by_two'
          ],
          [
              'multiply_by_three', 'multiply_by_two', 'add_one',
              'multiply_by_three', 'add_one', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_three', 'multiply_by_two'
          ],
          [
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'add_one', 'multiply_by_three', 'multiply_by_two',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_three', 'multiply_by_two'
          ],
          [
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'add_one', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_two', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_three', 'multiply_by_two'
          ],
          [
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'add_one', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_two', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_three', 'multiply_by_two'
          ],
          [
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'add_one', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_two',
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'multiply_by_three', 'multiply_by_two'
          ],
          [
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'add_one', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_two', 'multiply_by_three', 'add_one',
              'multiply_by_three', 'multiply_by_two'
          ],
          [
              'multiply_by_three', 'multiply_by_three', 'add_one',
              'add_one', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_three', 'multiply_by_three',
              'multiply_by_three', 'multiply_by_two', 'add_one',
              'multiply_by_three', 'multiply_by_two'
          ]], operations)
     self.assert_sequences_and_operations(n, sequences, operations)
Пример #8
0
 def test_5(self):
     self.assertEqual([1, 2, 4, 5], optimal_sequence(5))
Пример #9
0
 def test_1(self):
     self.assertEqual([1], optimal_sequence(1))
Пример #10
0
 def test_14(self):
     self.assertEqual([1, 2, 6, 7, 14], optimal_sequence(14))
Пример #11
0
 def test_96234(self):
     self.assertEqual([
         1, 3, 9, 10, 11, 22, 66, 198, 594, 1782, 5346, 16038, 16039, 32078,
         96234
     ], optimal_sequence(96234))
Пример #12
0
Input:
1
Output:
0
1

Sample 2.
Input:
5
Output:
3
1 2 4 5
Here, we first multiply 1 by 2 two times and then add 1. Another possibility is to first multiply by 3 and then add 1 two times. Hence “1 3 4 5” is also a valid output in this case.

Sample 3.
Input:
96234
Output:
14
1 3 9 10 11 22 66 198 594 1782 5346 16038 16039 32078 96234
Again, another valid output in this case is “1 3 9 10 11 33 99 297 891 2673 8019 16038 16039 48117 96234”.
"""
from primitive_calculator import optimal_sequence
from test.asserts import assert_equal

assert_equal([1], optimal_sequence(1), "sample 1")
assert_equal([1, 2, 4, 5], optimal_sequence(5), "sample 2")
assert_equal([
    1, 3, 9, 10, 11, 22, 66, 198, 594, 1782, 5346, 16038, 16039, 32078, 96234
], optimal_sequence(96234), "sample 3")