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/Problem19.py
#Matthew Ellison
# Created: 03-13-19
#Modified: 07-18-20
#Modified: 10-30-20
#How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
"""
You are given the following information, but you may prefer to do some research for yourself.
@@ -34,7 +34,6 @@ A leap year occurs on any year evenly divisible by 4, but not on a century unles
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
@@ -84,11 +83,9 @@ class Problem19(Problem):
#Stop the timer
self.timer.stop()
#Save the results
self.result = "There are " + str(self.totalSundays) + " Sundays that landed on the first of the month from " + str(self.__startYear) + " to " + str(self.__endYear)
#Throw a flag to show the problem is solved
self.solved = True
#Return the day of the week that the date you pass into it is on
def getDay(self, month: int, day: int, year: int) -> DAYS:
#Make sure the numebrs are within propper bounds
@@ -155,6 +152,7 @@ class Problem19(Problem):
return DAYS.SATURDAY
else:
return DAYS.ERROR
#Returns true if the year passed to it is a leap year
def isLeapYear(self, year: int) -> bool:
if(year < 1):
@@ -168,12 +166,19 @@ class Problem19(Problem):
elif((year % 4) == 0):
return True
return False
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.totalSundays = 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"There are {self.totalSundays} Sundays that landed on the first of the month from {self.__startYear} to {self.__endYear}"
#Returns the total sundays that were asked for
def getTotalSundays(self):
#If the problem hasn't been solved throw an exception
@@ -182,16 +187,6 @@ class Problem19(Problem):
return self.totalSundays
#Run automatically if the script was called stand alone
if __name__ == "__main__":
problem = Problem19()
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:
There are 171 Sundays that landed on the first of the month from 1901 to 2000
It took an average of 386.694 milliseconds to run this problem through 100 iterations