Пример #1
0
 def test_build_spiral(self):
     """Test build_spiral()"""
     expected = [[7, 8, 9],
                 [6, 1, 2],
                 [5, 4, 3]]
     self.assertEquals(expected, spirals.build_spiral(3, 3))
     expected = [[21, 22, 23, 24, 25,],
                 [20, 7, 8, 9, 10,],
                 [19, 6, 1, 2, 11,],
                 [18, 5, 4, 3, 12,],
                 [17, 16, 15, 14, 13,]]
     self.assertEquals(expected, spirals.build_spiral(5, 5))
Пример #2
0
def main():
    """Main program."""

    # This is my first, brute force solution.
    # The spiral of the required dimensions is
    # represented by a matrix and the diagonal
    # sum calculated by visiting each cell in
    # the two diagonals.
    answer = 0
    start = time.time()
    spiral = spirals.build_spiral(1001, 1001)
    answer = spirals.diagonal_sum(spiral)
    end = time.time()
    print("The answer is %d" % answer)
    print("%f seconds elapsed." % (end - start))

    # After verifying the above solution, I
    # ran matrices 3x3, 5x5, 7x7, and 9x9.
    # I noticed the patter in the corners
    # and so I came up with a second solution.
    # I used the data from OEIS sequence A114254
    # to verify that this method of calculating
    # the diagonal sums is correct.
    start = time.time()
    answer = spirals.calculate_diagonal_sum(1001)
    end = time.time()
    print("The answer is %d" % answer)
    print("%f seconds elapsed." % (end - start))

    import pyperclip
    pyperclip.copy(str(answer))
    print("The answer has been placed in the clipboard.")
Пример #3
0
 def test_diagonal_sum(self):
     """Test diagonal_sum()"""
     spiral = spirals.build_spiral(3, 3)
     self.assertEquals(25, spirals.diagonal_sum(spiral))
     spiral = spirals.build_spiral(5, 5)
     self.assertEquals(101, spirals.diagonal_sum(spiral))