mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Updated result to function
This commit is contained in:
@@ -33,7 +33,6 @@ class Problem(metaclass=abc.ABCMeta):
|
||||
def __init__(self, description: str):
|
||||
#Instance variables
|
||||
self.timer = Stopwatch()
|
||||
self.result = ""
|
||||
self.description = description
|
||||
self.solved = False
|
||||
#Gets
|
||||
@@ -41,11 +40,9 @@ class Problem(metaclass=abc.ABCMeta):
|
||||
def getDescription(self) -> str:
|
||||
return self.description
|
||||
#Returns the result of solving the problem
|
||||
@abc.abstractmethod
|
||||
def getResult(self) -> str:
|
||||
#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 self.result
|
||||
pass
|
||||
#Returns the time taken to run the problem as a string
|
||||
def getTime(self) -> str:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
|
||||
@@ -56,13 +56,16 @@ class Problem1(Problem):
|
||||
#Throw a flag to show the problem is solved
|
||||
self.solved = True
|
||||
|
||||
#Save the results
|
||||
self.result = "The sum of all numbers < " + str(self.__topNum + 1) + " is " + str(self.fullSum)
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
super().reset()
|
||||
self.fullSum = 0
|
||||
#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 "The sum of all numbers < " + str(self.__topNum + 1) + " is " + str(self.fullSum)
|
||||
#Gets
|
||||
#Returns the requested sum
|
||||
def getSum(self) -> int:
|
||||
|
||||
@@ -58,9 +58,6 @@ class Problem2(Problem):
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Save the results
|
||||
self.result = "The sum of all even Fibonacci numbers less than " + str(self.__topNumber + 1) + " is " + str(self.fullSum)
|
||||
|
||||
#Throw a flag to show the problem is solved
|
||||
self.solved = True
|
||||
|
||||
@@ -68,6 +65,12 @@ class Problem2(Problem):
|
||||
def reset(self):
|
||||
super().reset()
|
||||
self.fullSum = 0
|
||||
#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 "The sum of all even Fibonacci numbers less than " + str(self.__topNumber + 1) + " is " + str(self.fullSum)
|
||||
#Gets
|
||||
#Returns the requested sum
|
||||
def getSum(self) -> int:
|
||||
|
||||
@@ -55,9 +55,6 @@ class Problem3(Problem):
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Save the results
|
||||
self.result = "The largest prime factor of " + str(self.__goalNumber) + " is " + str(self.factors[(len(self.factors) - 1)])
|
||||
|
||||
#Throw a flag to show the problem is solved
|
||||
self.solved = True
|
||||
|
||||
@@ -65,7 +62,12 @@ class Problem3(Problem):
|
||||
def reset(self):
|
||||
super().reset()
|
||||
self.factors.clear()
|
||||
|
||||
#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 "The largest prime factor of " + str(self.__goalNumber) + " is " + str(self.factors[(len(self.factors) - 1)])
|
||||
#Gets
|
||||
#Returns the list of factors of the number
|
||||
def getFactors(self) -> list:
|
||||
|
||||
@@ -64,15 +64,18 @@ class Problem4(Problem):
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Save the results
|
||||
self.result = "The largest palindrome made from the product of two 3-digit numbers is " + str(self.palindromes[len(self.palindromes) - 1])
|
||||
|
||||
#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()
|
||||
self.palindromes.clear()
|
||||
#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 "The largest palindrome made from the product of two 3-digit numbers is " + str(self.palindromes[len(self.palindromes) - 1])
|
||||
#Gets
|
||||
#Returns the list of all palindromes
|
||||
def getPalindromes(self) -> list:
|
||||
|
||||
@@ -71,19 +71,18 @@ class Problem5(Problem):
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Save the results
|
||||
if(currentNum < 0):
|
||||
self.result = "There was an error: Could not find a number that fit the criteria"
|
||||
else:
|
||||
self.result = "The smallest positive number that is evenly divisible by all numbers 1-20 is " + str(self.smallestNum)
|
||||
|
||||
#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()
|
||||
self.smallestNum = 0
|
||||
|
||||
#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 "The smallest positive number that is evenly divisible by all numbers 1-20 is " + str(self.smallestNum)
|
||||
#Gets
|
||||
#Returns the requested number
|
||||
def getNumber(self) -> int:
|
||||
|
||||
Reference in New Issue
Block a user