mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2026-02-03 19:52:28 -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):
|
def __init__(self, description: str):
|
||||||
#Instance variables
|
#Instance variables
|
||||||
self.timer = Stopwatch()
|
self.timer = Stopwatch()
|
||||||
self.result = ""
|
|
||||||
self.description = description
|
self.description = description
|
||||||
self.solved = False
|
self.solved = False
|
||||||
#Gets
|
#Gets
|
||||||
@@ -41,11 +40,9 @@ class Problem(metaclass=abc.ABCMeta):
|
|||||||
def getDescription(self) -> str:
|
def getDescription(self) -> str:
|
||||||
return self.description
|
return self.description
|
||||||
#Returns the result of solving the problem
|
#Returns the result of solving the problem
|
||||||
|
@abc.abstractmethod
|
||||||
def getResult(self) -> str:
|
def getResult(self) -> str:
|
||||||
#If the problem hasn't been solved throw an exception
|
pass
|
||||||
if(not self.solved):
|
|
||||||
raise Unsolved("You must solve the problem before you can see the result")
|
|
||||||
return self.result
|
|
||||||
#Returns the time taken to run the problem as a string
|
#Returns the time taken to run the problem as a string
|
||||||
def getTime(self) -> str:
|
def getTime(self) -> str:
|
||||||
#If the problem hasn't been solved throw an exception
|
#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
|
#Throw a flag to show the problem is solved
|
||||||
self.solved = True
|
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
|
#Reset the problem so it can be run again
|
||||||
def reset(self):
|
def reset(self):
|
||||||
super().reset()
|
super().reset()
|
||||||
self.fullSum = 0
|
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
|
#Gets
|
||||||
#Returns the requested sum
|
#Returns the requested sum
|
||||||
def getSum(self) -> int:
|
def getSum(self) -> int:
|
||||||
|
|||||||
@@ -58,9 +58,6 @@ class Problem2(Problem):
|
|||||||
#Stop the timer
|
#Stop the timer
|
||||||
self.timer.stop()
|
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
|
#Throw a flag to show the problem is solved
|
||||||
self.solved = True
|
self.solved = True
|
||||||
|
|
||||||
@@ -68,6 +65,12 @@ class Problem2(Problem):
|
|||||||
def reset(self):
|
def reset(self):
|
||||||
super().reset()
|
super().reset()
|
||||||
self.fullSum = 0
|
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
|
#Gets
|
||||||
#Returns the requested sum
|
#Returns the requested sum
|
||||||
def getSum(self) -> int:
|
def getSum(self) -> int:
|
||||||
|
|||||||
@@ -55,9 +55,6 @@ class Problem3(Problem):
|
|||||||
#Stop the timer
|
#Stop the timer
|
||||||
self.timer.stop()
|
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
|
#Throw a flag to show the problem is solved
|
||||||
self.solved = True
|
self.solved = True
|
||||||
|
|
||||||
@@ -65,7 +62,12 @@ class Problem3(Problem):
|
|||||||
def reset(self):
|
def reset(self):
|
||||||
super().reset()
|
super().reset()
|
||||||
self.factors.clear()
|
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
|
#Gets
|
||||||
#Returns the list of factors of the number
|
#Returns the list of factors of the number
|
||||||
def getFactors(self) -> list:
|
def getFactors(self) -> list:
|
||||||
|
|||||||
@@ -64,15 +64,18 @@ class Problem4(Problem):
|
|||||||
#Stop the timer
|
#Stop the timer
|
||||||
self.timer.stop()
|
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
|
#Throw a flag to show the problem is solved
|
||||||
self.solved = True
|
self.solved = True
|
||||||
#Reset the problem so it can be run again
|
#Reset the problem so it can be run again
|
||||||
def reset(self):
|
def reset(self):
|
||||||
super().reset()
|
super().reset()
|
||||||
self.palindromes.clear()
|
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
|
#Gets
|
||||||
#Returns the list of all palindromes
|
#Returns the list of all palindromes
|
||||||
def getPalindromes(self) -> list:
|
def getPalindromes(self) -> list:
|
||||||
|
|||||||
@@ -71,19 +71,18 @@ class Problem5(Problem):
|
|||||||
#Stop the timer
|
#Stop the timer
|
||||||
self.timer.stop()
|
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
|
#Throw a flag to show the problem is solved
|
||||||
self.solved = True
|
self.solved = True
|
||||||
#Reset the problem so it can be run again
|
#Reset the problem so it can be run again
|
||||||
def reset(self):
|
def reset(self):
|
||||||
super().reset()
|
super().reset()
|
||||||
self.smallestNum = 0
|
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
|
#Gets
|
||||||
#Returns the requested number
|
#Returns the requested number
|
||||||
def getNumber(self) -> int:
|
def getNumber(self) -> int:
|
||||||
|
|||||||
Reference in New Issue
Block a user