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/Problem22.py
#Matthew Ellison
# Created: 03-20-19
#Modified: 07-19-20
#Modified: 10-30-20
#What is the total of all the name scores in the file?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,9 +23,7 @@
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
import Algorithms
class Problem22(Problem):
@@ -416,7 +414,6 @@ class Problem22(Problem):
#Start the timer
self.timer.start()
#Sort all the names
self.__names.sort()
#Step through every name adding up the values of the characters
@@ -431,15 +428,12 @@ class Problem22(Problem):
for cnt in range(0, len(self.sums)):
self.prod.append(self.sums[cnt] * (cnt + 1))
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The answer to the question is " + str(sum(self.prod))
#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()
@@ -447,6 +441,12 @@ class Problem22(Problem):
self.prod.clear()
#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 answer to the question is {sum(self.prod)}"
#Returns the vecot of the names being scored
def getNames(self) -> list:
return self.__names
@@ -457,14 +457,6 @@ class Problem22(Problem):
raise Unsolved("You must solve the problem before can you see the sum")
return sum(self.prod)
#This ensures the correct function is called if this is called as a stand along script
if __name__ == "__main__":
problem = Problem22()
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 answer to the question is 871198282