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/Problem13.py
#Matthew Ellison
# Created: 01-31-19
#Modified: 07-17-20
#Modified: 10-30-20
#Work out the first ten digits of the sum of the following one-hundred 50-digit numbers
"""
37107287533902102798797998220837590246510135740250
@@ -125,7 +125,6 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -254,17 +253,22 @@ class Problem13(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The sum of all " + str(len(self.__numbers)) + " numbers is " + str(self.sum) + "\nThe first 10 digits are: " + str(self.sum)[0:10]
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
sum = 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 all {len(self.__numbers)} numbers is {self.sum}\n" \
f"The first 10 digits are: {str(self.sum)[0:10]}"
#Returns the list of 50-digit numbers
def getNumbers(self) -> list:
#If the problem hasn't been solved throw an exception
@@ -278,14 +282,6 @@ class Problem13(Problem):
raise Unsolved("You must solve the problem before you can get the sum of the numbers")
return self.sum
#If you are running this file, automatically start the correct function
if __name__ == "__main__":
problem = Problem13()
print(problem.getDescription()) #Print the description of the problem
problem.solve() #Solve the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
"""Results:
The sum of all 100 numbers is 5537376230390876637302048746832985971773659831892672