Updated all problems to use fstrings and results()

This commit is contained in:
2020-10-30 16:20:39 -04:00
parent ad458278d4
commit 46216a2807
33 changed files with 280 additions and 452 deletions

View File

@@ -1,7 +1,7 @@
#ProjectEuler/Python/Problem28.py
#Matthew Ellison
# Created: 09-22-19
#Modified: 07-19-20
#Modified: 10-30-20
#What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed by starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,7 +23,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -54,11 +53,9 @@ class Problem28(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The sum of the diagonals in the given grid is " + str(self.sumOfDiagonals)
#Throw a flag to show the problem is solved
self.solved = True
#Sets up the grid
def setupGrid(self) -> list:
#Setup the grid to be the right size and fill it with 0's
@@ -103,6 +100,7 @@ class Problem28(Problem):
finalLocation = True
break
return self.grid
#Finds the sum of the diagonals in the grid
def findSum(self) -> int:
sumOfDiagonals = 0
@@ -120,12 +118,19 @@ class Problem28(Problem):
leftSide += 1
rightSide -= 1
return sumOfDiagonals
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.sumOfDiagonals = 0
#Gets
#Returns the result of solving the problem
def getResult(self):
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before you can see the result")
return f"The sum of the diagonals in the given grid is {self.sumOfDiagonals}"
#Returns the grid
def getGrid(self) -> list:
return self.grid
@@ -137,15 +142,6 @@ class Problem28(Problem):
return self.sumOfDiagonals
#This calls the appropriate functions if the script is called stand alone
if __name__ == "__main__":
problem = Problem28()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
The sum of the diagonals in the given grid is 669171001
It took an average of 212.747 milliseconds to run this problem through 100 iterations