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/Problem21.py
#Matthew Ellison
# Created: 03-18-19
#Modified: 07-19-20
#Modified: 10-30-20
#Evaluate the sum of all the amicable numbers under 10000
#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
import Algorithms
@@ -71,14 +70,9 @@ class Problem21(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result += "All amicable numbers less than 10000 are\n"
for num in self.amicable:
self.result += str(num) + '\n'
self.result += "The sum of all of these amicable numbers is " + str(sum(self.amicable))
#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()
@@ -86,6 +80,18 @@ class Problem21(Problem):
self.amicable.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")
result = "All amicable numbers less than 10000 are\n"
for num in self.amicable:
result += f"{num}\n"
result += f"The sum of all of these amicable numbers is {sum(self.amicable)}"
return result
#Returns a vector with all of the amicable number calculated
def getAmicable(self) -> list:
#If the problem hasn't been solved throw an exception
@@ -99,14 +105,6 @@ class Problem21(Problem):
raise Unsolved("You must solve the problem before you can get the sum of the amicable numbers")
return sum(self.amicable)
#Run the correct function if this script is called stand along
if __name__ == "__main__":
problem = Problem21()
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:
All amicable numbers less than 10000 are