Files
ProjectEulerPython/Problems/Problem24.py

97 lines
3.1 KiB
Python

#ProjectEuler/Python/Problem24.py
#Matthew Ellison
# Created: 03-24-19
#Modified: 07-19-20
#What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
#Unless otherwise listed, all of my non-standard imports can be gotten from my pyClasses repository at https://bitbucket.org/Mattrixwv/pyClasses
"""
Copyright (C) 2020 Matthew Ellison
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from Problems.Problem import Problem
from Stopwatch import Stopwatch
from Unsolved import Unsolved
import Algorithms
class Problem24(Problem):
#Variables
__neededPerm = 1000000 #The number of the permutation that you need
__nums = "0123456789" #All of the characters that we need to get the permutations of
#Functions
#Constructor
def __init__(self):
super().__init__("What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?")
self.permutations = []
#Operational functions
#Solve the problem
def solve(self):
#If the problem has already been solved do nothing and end the function
if(self.solved):
return
#Start the timer
self.timer.start()
#Get all permutations of the string
permutations = Algorithms.getPermutations(self.__nums)
#Stop the timer
self.timer.stop()
#Save the results
self.result = "The 1 millionth permutation is " + str(permutations[self.__neededPerm - 1])
#Throw a flag to show the problem is solved
self.solved = True
#Reset the problem so it can be run again
def reset(self):
super().reset()
self.permutations.clear()
#Gets
#Returns a list with all of the permutations
def getPermutationsList(self) -> list:
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before can you see the list of permutations")
return self.permutations
#Returns the requested permutation
def getPermutation(self) -> str:
#If the problem hasn't been solved throw an exception
if(not self.solved):
raise Unsolved("You must solve the problem before can you see the ")
return self.permutations[self.__neededPerm - 1]
#This calls the appropriate functions if the script is called stand alone
if __name__ == "__main__":
problem = Problem24()
print(problem.getDescription()) #Print the description
problem.solve() #Call the function that answers the problem
#Print the results
print(problem.getResult())
print("It took " + problem.getTime() + " to solve this algorithm")
""" Results:
The 1 millionth permutation is 2783915460
It took 7.363 seconds to run this algorithm
"""