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/Problem15.py
|
||||
#Matthew Ellison
|
||||
# Created: 01-31-19
|
||||
#Modified: 07-18-20
|
||||
#Modified: 10-30-20
|
||||
#How many routes from the top left corner to the bottom right corner are there through a 20×20 grid if you can only move right and down?
|
||||
#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
|
||||
|
||||
|
||||
@@ -49,16 +48,14 @@ class Problem15(Problem):
|
||||
self.timer.start()
|
||||
|
||||
#Start the recursion at the right location and catch what is returned
|
||||
numberMoves = self.movement(0, 0)
|
||||
self.numOfRoutes = self.movement(0, 0)
|
||||
|
||||
#Stop the timer
|
||||
self.timer.stop()
|
||||
|
||||
#Print the results
|
||||
self.result = "The number of paths from 1 corner of a " + str(self.__gridWidth) + " x " + str(self.__gridHeight) + " grid to the opposite corner is " + str(numberMoves)
|
||||
|
||||
#Throw a flag to show the problem is solved
|
||||
self.solved = True
|
||||
|
||||
#This function acts as a handler for moving the position on the grid and counting the distance
|
||||
#It moves right first, then down
|
||||
def movement(self, currentX: int, currentY: int) -> int:
|
||||
@@ -76,12 +73,19 @@ class Problem15(Problem):
|
||||
numberMoves += self.movement(currentX, currentY + 1)
|
||||
|
||||
return numberMoves
|
||||
|
||||
#Reset the problem so it can be run again
|
||||
def reset(self):
|
||||
super().reset()
|
||||
self.numOfRoutes = 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 number of paths from 1 corner of a {self.__gridWidth} x {self.__gridHeight} grid to the opposite corner is {self.numOfRoutes}"
|
||||
#Returns the number of routes found
|
||||
def getNumberOfRoutes(self) -> int:
|
||||
#If the problem hasn't been solved throw an exception
|
||||
@@ -89,14 +93,6 @@ class Problem15(Problem):
|
||||
raise Unsolved("You must solve the problem before you can get the number of routes")
|
||||
return self.numOfRoutes
|
||||
|
||||
#If you are running this file, automatically start the correct function
|
||||
if __name__ == "__main__":
|
||||
problem = Problem15()
|
||||
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 number of paths from 1 corner of a 20 x 20 grid to the opposite corner is 137846528820
|
||||
|
||||
Reference in New Issue
Block a user