Exemplo n.º 1
0
class Explorer:
    def __init__(self):
        self.collection = MpNumberCollection()
        self.run_time = None

    def explore(self, start, end):
        """There are two cubes that need to be expanded: 2^x, 3^y, 7^z and 3^x, 5^y, and 7^z"""
        assert start < end, "start value must be less than end value"
        assert 0 <= start < end, "start and end values must be greater than 0"

        tic = time.time()

        # The 2, 3, 7 search space.
        prisms = [
            # [2 range, 3 range, 7 range],
            # expanded cube
            [[start, end], [start, end], [start, end]],
            # longs
            [[start, end], [start, end], [0, start]],
            [[0, start], [start, end], [start, end]],
            [[start, end], [0, start], [start, end]],
            # faces
            [[start, end], [0, start], [0, start]],
            [[0, start], [start, end], [0, start]],
            [[0, start], [0, start], [start, end]],
        ]

        for prism in prisms:
            for t in range(*prism[0]):
                tp = 2**t
                for th in range(*prism[1]):
                    thp = 3**th
                    for s in range(*prism[2]):
                        sp = 7**s
                        num = tp * thp * sp
                        if "0" not in str(num):
                            self.collection.add(
                                MpNumberVariant(num, [t, th, 0, s]))

        ##
        # The 5, 3, 7 search space.
        # We've already searched when 5 to the zeroeth power, so we can skip it here.
        for prism in prisms:
            # If the five line (first index 0) starts (second index 0) at zero, set it to 1.
            if prism[0][0] == 0:
                prism[0][0] = 1

        for prism in prisms:
            for t in range(*prism[0]):
                tp = 5**t
                for th in range(*prism[1]):
                    thp = 3**th
                    for s in range(*prism[2]):
                        sp = 7**s
                        num = tp * thp * sp
                        if "0" not in str(num):
                            self.collection.add(
                                MpNumberVariant(num, [0, th, t, s]))

        self.run_time = round(time.time() - tic, 2)
    def test_all_variants(self):
        collection = MpNumberCollection()
        collection.read_json(FIXTURE_PATH)
        variants = collection.all_variants()

        assert type(variants[0]) == MpNumberVariant
        assert len(variants) == 769
    def test_explore(self):
        # Expect an error to be raised if bad numbers are given
        with pytest.raises(Exception):
            # End is before beginning
            Explorer().explore(3, 1)
        with pytest.raises(Exception):
            # Negative numbers
            Explorer().explore(3, -1)

        former_json_path = "tests/fixtures/5-10.json"
        collection_former = MpNumberCollection(json_path=former_json_path)

        new_json_path = "tests/output/t5-10.json"
        os.remove(new_json_path)
        explorer = Explorer()
        explorer.explore(5, 10)
        collection = explorer.collection
        collection.write_json(new_json_path)

        with open(former_json_path) as f:
            former_json = json.load(f)
        with open(new_json_path) as f:
            new_json = json.load(f)

        assert former_json == new_json
Exemplo n.º 4
0
def print_tree(root: int = typer.Argument(
    default=None,
    help="Which root should be printed?",
), ):
    collection = MpNumberCollection(json_path="output/0-475.json")
    tree = Tree(collection)
    if root is not None:
        tree.print(root=root)
    tree.print_summary(root=root)
Exemplo n.º 5
0
    def test_print(self):
        with patch("sys.stdout", new=StringIO()) as fake_out:
            collection = MpNumberCollection(
                json_path="tests/fixtures/5-10.json")
            tree = Tree(collection)
            tree.print(root=42)

            assert "1176" in fake_out.getvalue()

            tree.print(root=0)
            assert "20" in fake_out.getvalue()
    def test_contains(self):
        collection = MpNumberCollection()
        assert not collection.contains("1,0,1,0,1,1,1,0")

        variant = MpNumberVariant(27648, (10, 3, 0, 0))
        collection.add(variant)
        assert collection.contains("1,0,1,0,1,1,1,0")
Exemplo n.º 7
0
    def test_add_child(self):
        collection = MpNumberCollection()
        tree = Tree(collection)
        tree.add_child(15)
        five_root = tree.get_node(5)
        assert five_root.children[0].name == 15

        tree.add_child(1176)
        eight_root = tree.get_node(8)
        assert eight_root.children[0].name == 42

        tree.add_child(177147)

        implicit_node = tree.get_node(1372)
        assert implicit_node.name == 1372
        assert implicit_node.parent.name == 42

        root_42 = tree.get_node(42)
        assert len(root_42.children) == 2
    def test_write_json(self):
        os.remove(OUTPUT_PATH)

        collection = MpNumberCollection()
        collection.read_json(FIXTURE_PATH)
        collection.write_json(OUTPUT_PATH)

        with open(FIXTURE_PATH, "r") as f:
            original_json = json.load(f)

        with open(OUTPUT_PATH, "r") as f:
            parsed_json = json.load(f)

        assert original_json == parsed_json
Exemplo n.º 9
0
 def __init__(self):
     self.collection = MpNumberCollection()
     self.run_time = None
    def test_add(self):
        collection = MpNumberCollection()
        # Test add variant
        variant = MpNumberVariant(27648, (10, 3, 0, 0))
        collection.add(variant)
        assert variant.digit_count in collection.mp_numbers
        assert collection.get(variant.digit_count).variants == [variant]

        # Test when there is an existing mp_number
        variant2 = MpNumberVariant(84672, (6, 3, 0, 2))
        collection.add(variant2)
        assert variant2 in collection.get(variant.digit_count).variants
        assert collection.count() == 1

        # Ensure error is thrown when overwriting a mp_number

        # Test add number
        collection = MpNumberCollection()
        mp_number = MpNumber(variants=[variant])
        collection.add(mp_number)
        assert collection.count() == 1
        assert collection.mp_numbers == {mp_number.digit_count: mp_number}

        # Ensure that error is raised with unknown add type
        with pytest.raises(Exception):
            collection.add(1)
    def test_read_json(self):
        collection = MpNumberCollection()
        collection.read_json(FIXTURE_PATH)

        assert collection.count() == 703
 def test_count(self):
     collection = MpNumberCollection()
     assert collection.count() == 0
 def test_init(self):
     collection = MpNumberCollection()
     assert collection.mp_numbers == {}
Exemplo n.º 14
0
 def test_init(self):
     collection = MpNumberCollection(json_path="tests/fixtures/5-10.json")
     tree = Tree(collection)
     assert tree.collection == collection
Exemplo n.º 15
0
 def test_get_max_mp(self):
     collection = MpNumberCollection(json_path="tests/fixtures/0-475.json")
     tree = Tree(collection)
     assert tree.get_max_mp(root=20) == 10
     assert tree.get_max_mp() == 11