mirror of
https://bitbucket.org/Mattrixwv/projecteulerpython.git
synced 2025-12-06 17:43:58 -05:00
Updated all problems to use fstrings and results()
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#ProjectEuler/Python/Problem17.py
|
||||
#Matthew Ellison
|
||||
# Created: 02-04-19
|
||||
#Modified: 07-18-20
|
||||
#Modified: 10-30-20
|
||||
#If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
|
||||
#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 math
|
||||
|
||||
@@ -59,9 +58,6 @@ class Problem17(Problem):
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Save the results
|
||||
self.result = "The sum of all the letters in all the numbers " + str(self.__startNum) + '-' + str(self.__stopNum) + " is " + str(self.letterCount)
|
||||
|
||||
#Throw a flag to show the problem is solved
|
||||
self.solved = True
|
||||
|
||||
@@ -168,6 +164,7 @@ class Problem17(Problem):
|
||||
|
||||
#Return the string
|
||||
return numberString
|
||||
|
||||
#Get the number of alphabetic characters in the string passed in
|
||||
def getNumberChars(self, number: str) -> int:
|
||||
sumOfLetters = 0
|
||||
@@ -179,12 +176,19 @@ class Problem17(Problem):
|
||||
|
||||
#Return the number
|
||||
return sumOfLetters
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
super().reset()
|
||||
self.letterCount = 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"The sum of all the letters in all the numbers {self.__startNum}-{self.__stopNum} is {self.letterCount}"
|
||||
#Returns the number of letters asked for
|
||||
def getLetterCount(self):
|
||||
#If the problem hasn't been solved throw an exception
|
||||
@@ -193,15 +197,6 @@ class Problem17(Problem):
|
||||
return self.letterCount
|
||||
|
||||
|
||||
#If you are running this file, automatically start the correct function
|
||||
if __name__ == "__main__":
|
||||
problem = Problem17()
|
||||
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:
|
||||
The sum of all the letters in all the numbers 1-1000 is 21124
|
||||
It took an average of 3.095 milliseconds to run this problem through 100 iterations
|
||||
|
||||
Reference in New Issue
Block a user