Updated to use new library layout

This commit is contained in:
2021-07-24 16:13:05 -04:00
parent d18b3fa9f6
commit 84555edd31
39 changed files with 515 additions and 709 deletions

View File

@@ -1,7 +1,7 @@
#ProjectEuler/ProjectEulerPython/Problems/Problem36.py
#Matthew Ellison
# Created: 06-29-21
#Modified: 06-29-21
#Modified: 07-24-21
#Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
@@ -23,9 +23,8 @@
from Problems.Problem import Problem
from Unsolved import Unsolved
import Algorithms
import NumberAlgorithms
import StringAlgorithms
class Problem36(Problem):
@@ -34,14 +33,14 @@ class Problem36(Problem):
#Functions
#Constructor
def __init__(self):
def __init__(self) -> None:
super().__init__("Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.")
self.palindromes = []
self.sumOfPal = 0
#Operational functions
#Solve the problem
def solve(self):
def solve(self) -> None:
#If the problem has already been solved do nothing and end the function
if(self.solved):
return
@@ -49,18 +48,20 @@ class Problem36(Problem):
#Start the timer
self.timer.start()
#Start with 1, check if it is a palindrome in base 10 and 2, and continue to __max_num
for num in range(1, self.__max_num + 1):
#Check if num is a palindrome
if(Algorithms.isPalindrome(str(num))):
if(StringAlgorithms.isPalindrome(str(num))):
#Convert num to base 2 and see if that is a palindrome
binNum = Algorithms.toBin(num)
if(Algorithms.isPalindrome(binNum)):
binNum = NumberAlgorithms.toBin(num)
if(StringAlgorithms.isPalindrome(binNum)):
#Add num to the list of palindromes
self.palindromes.append(num)
#Get the sum of all palindromes in the list
self.sumOfPal = sum(self.palindromes)
#Stop the timer
self.timer.stop()
@@ -68,7 +69,7 @@ class Problem36(Problem):
self.solved = True
#Reset the problem so it can be run again
def reset(self):
def reset(self) -> None:
super().reset()
self.palindromes = []
self.sum = 0
@@ -76,23 +77,18 @@ class Problem36(Problem):
#Gets
#Returns a string with the solution to the problem
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")
self.solvedCheck("result")
return f"The sum of all base 10 and base 2 palindromic numbers < {self.__max_num} is {self.sumOfPal}"
#Return the list of palindromes < MAX_NUM
def getPalindromes(self) -> list:
#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 palindromes")
self.solvedCheck("list of palindromes")
return self.palindromes
#Return the sum of all elements in the list of palindromes
def getSumOfPalindromes(self) -> int:
#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 sum of the palindromes")
self.solvedCheck("sum of all palindromes")
return self.sumOfPal
""" Results:
The sum of all base 10 and base 2 palindromic numbers < 999999 is 872187
It took an average of 295.861 milliseconds to run this problem through 100 iterations