Beispiel #1
0
    def test_03(self):
        """One cluster of only two wires in parallel; some other wires. String wire ids and junctions."""
        wires = {
            'wire1': ('X', 'Y'),
            'wire2': ('Y', 'X'),
            'other1': ('X', 'Z'),
            'other2': ('W', 'Y')
        }
        resistances = {'wire1': 2, 'wire2': 2 / 3, 'other1': 4, 'other2': 2}
        try:
            new_wires, new_resistances = quiz1.replace_parallels(
                wires, resistances)
        except RecursionError:
            self.fail(
                "A runtime error occurred, check if your code recurses infinitely"
            )
        wires_wrapper = WireDictionary(new_wires,
                                       {frozenset(['wire1', 'wire2'])})
        resistances_wrapper = ResistanceDictionary(
            new_resistances, {frozenset(['wire1', 'wire2'])})

        try:
            wires_wrapper.assertEqual({
                'wire1': ('X', 'Y'),
                'other1': ('X', 'Z'),
                'other2': ('W', 'Y')
            })
            resistances_wrapper.assertEqual({
                'wire2': 0.5,
                'other1': 4,
                'other2': 2
            })
        except AssertionError as ex:
            self.fail(str(ex))
Beispiel #2
0
 def test_01(self):
     """Test that something is returned"""
     try:
         result = quiz1.replace_parallels({"1": (0, 1)}, {"1": 2})
         self.assertTrue(result is not None,
                         "Your function should return something")
     except RecursionError:
         self.fail(
             "A recursion error occurred, check if your code recurses infinitely"
         )
Beispiel #3
0
    def test_05(self):
        """Two groups of 2 and >2 parallel wires, some tuple wire ids and junctions."""
        wires = {
            (0, 0): (("a", "a"), "b"),
            (0, 1): ("b", ("a", "a")),
            (0, 2): (("a", "a"), "b"),
            (0, 3): ("b", ("a", "a")),
            (1, 1): (("A", "B"), ("a", "a")),
            (1, 2): (("A", "B"), ("C", "D")),
            (2, 2): (("E", "F"), ("C", "D")),
            "wire": (("C", "D"), ("E", "F"))
        }
        resistances = {
            (0, 0): 16,
            (0, 1): 72,
            (0, 2): 80,
            (0, 3): 90,
            (1, 1): 3,
            (1, 2): 5,
            (2, 2): 20,
            "wire": 80
        }
        try:
            new_wires, new_resistances = quiz1.replace_parallels(
                wires, resistances)
        except RecursionError:
            self.fail(
                "A runtime error occurred, check if your code recurses infinitely"
            )
        equivalent_wires = {
            frozenset([(0, 0), (0, 1), (0, 2), (0, 3)]),
            frozenset([(2, 2), "wire"])
        }
        wires_wrapper = WireDictionary(new_wires, equivalent_wires)
        resistances_wrapper = ResistanceDictionary(new_resistances,
                                                   equivalent_wires)

        expected_wires = {
            (0, 1): ("b", ("a", "a")),
            (1, 1): (("a", "a"), ("A", "B")),
            (1, 2): (("A", "B"), ("C", "D")),
            "wire": (("C", "D"), ("E", "F"))
        }
        expected_resistances = {(0, 3): 10, (1, 1): 3, (1, 2): 5, "wire": 16}
        try:
            wires_wrapper.assertEqual(expected_wires)
            resistances_wrapper.assertEqual(expected_resistances)
        except AssertionError as ex:
            self.fail(str(ex))
Beispiel #4
0
    def test_04(self):
        """One cluster with 4 parallel wires, tuple wire ids are tuples, some tuple junctions."""
        wires = {
            (0, 0): (("a", "a"), "b"),
            (0, 1): ("b", ("a", "a")),
            (0, 2): (("a", "a"), "b"),
            (0, 3): ("b", ("a", "a")),
            (1, 1): (("A", "B"), ("a", "a")),
            (1, 2): (("a", "a"), ("C", "D")),
            (2, 2): (("A", "B"), ("C", "D"))
        }
        resistances = {
            (0, 0): 28,
            (0, 1): 35,
            (0, 2): 44,
            (0, 3): 77,
            (1, 1): 3,
            (1, 2): 5,
            (2, 2): 10
        }
        try:
            new_wires, new_resistances = quiz1.replace_parallels(
                wires, resistances)
        except RecursionError:
            self.fail(
                "A runtime error occurred, check if your code recurses infinitely"
            )
        wires_wrapper = WireDictionary(
            new_wires, {frozenset([(0, 0), (0, 1), (0, 2), (0, 3)])})
        resistances_wrapper = ResistanceDictionary(
            new_resistances, {frozenset([(0, 0), (0, 1), (0, 2), (0, 3)])})

        expected_wires = {
            (0, 1): ("b", ("a", "a")),
            (1, 1): (("A", "B"), ("a", "a")),
            (1, 2): (("a", "a"), ("C", "D")),
            (2, 2): (("A", "B"), ("C", "D"))
        }
        expected_resistances = {(0, 2): 10, (1, 1): 3, (1, 2): 5, (2, 2): 10}
        try:
            wires_wrapper.assertEqual(expected_wires)
            resistances_wrapper.assertEqual(expected_resistances)
        except AssertionError as ex:
            self.fail(str(ex))
Beispiel #5
0
    def test_02(self):
        """No wires in parallel. Wire ids and junctions are both strings."""
        wires = {'0': ('A', 'B'), '1': ('B', 'C'), '2': ('C', 'A')}
        resistances = {'0': 5, '1': 2, '2': 3}
        try:
            new_wires, new_resistances = quiz1.replace_parallels(
                wires, resistances)
        except RecursionError:
            self.fail(
                "A runtime error occurred, check if your code recurses infinitely"
            )
        wires_wrapper = WireDictionary(new_wires, set())
        resistances_wrapper = ResistanceDictionary(new_resistances, set())

        try:
            wires_wrapper.assertEqual({
                '0': ('A', 'B'),
                '1': ('B', 'C'),
                '2': ('C', 'A')
            })
            resistances_wrapper.assertEqual({'0': 5, '1': 2, '2': 3})
        except AssertionError as ex:
            self.fail("No wires in parallel, nothing to simplify: " + str(ex))
Beispiel #6
0
    def test_06(self):
        """Big test (a lot of wires) to check performance: 10000 groups of 3 parallel wires."""
        wires = {
            str(i): (str(int(i) // 3), str((int(i) + 3) // 3))
            for i in range(30000)
        }
        resistances = {
            str(i): 12 if i % 3 == 0 else (28 if i % 3 == 1 else 42)
            for i in range(30000)
        }
        try:
            new_wires, new_resistances = quiz1.replace_parallels(
                wires, resistances)
        except RecursionError:
            self.fail(
                "A runtime error occurred, check if your code recurses infinitely"
            )
        equivalent_wires = {
            frozenset([str(i * 3), str(i * 3 + 1),
                       str(i * 3 + 2)])
            for i in range(10000)
        }
        wires_wrapper = WireDictionary(new_wires, equivalent_wires)
        resistances_wrapper = ResistanceDictionary(new_resistances,
                                                   equivalent_wires)

        expected_wires = {
            str(i): (str(int(i) // 3), str((int(i) + 3) // 3))
            for i in range(0, 30000, 3)
        }
        expected_resistances = {str(i): 7 for i in range(0, 30000, 3)}
        try:
            wires_wrapper.assertEqual(expected_wires)
            resistances_wrapper.assertEqual(expected_resistances)
        except AssertionError as ex:
            self.fail(str(ex))