Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import sqrt
- import itertools
- import random
- class Main:
- def __init__(self):
- self.MAX_RANGE = 10 #determines the highest possible value of the numbers to be permutated
- self.AMOUNT_PER_SOLUTION_TYPE = 5 #determines how many perms of each type will be output based on their amount of solutions (0, 1, 2)
- self.permutation_list_filtered = [] #a list of permutations containing the same amount of vars for all 3 types of solutions, only including int solutions
- self.permutation_list_generation() #creates and fills self.permutation_list_filtered
- def quadratic_formula(self, a, b, c): #puts vars a, b and c through the quadratic formula
- #print(a, b, c) #DEBUG
- try:
- solution1 = (-b + sqrt(b**2 - 4*a*c)) / (2*a) #defines the first solution to the 2nd degree equation using the abc formula
- except (ValueError, ZeroDivisionError): #secures against errors caused by trying to find the root of a negative number or dividing by 0
- solution1 = None
- try:
- solution2 = (-b - sqrt(b**2 - 4*a*c)) / (2*a) #the second solution
- except (ValueError, ZeroDivisionError): #secures against errors caused by trying to find the root of a negative number or dividing by 0
- solution2 = None
- solution_list = [solution1, solution2] #puts the two solutions into a list
- solution_list_no_decimals = [] #the list to which the solutions are appended to once the ints with decimals have been eliminated
- #this turns ints with decimals into proper ints to not confuse the program later on
- for solution in solution_list: #cycles through each solution
- #print(solution) #DEBUG
- if type(solution) == float and solution.is_integer(): #if the solution is both a float and an int (= an int with decimals, such as 4.00)
- solution_list_no_decimals.append(int(solution))
- else:
- solution_list_no_decimals.append(solution) #turns the solution into an int, removing its decimals
- return solution_list_no_decimals #returns a list of all the possible solutions
- def permutation_list_generation(self): #returns a list of ints under a max range, that are permutated into 3-long tuples. is later filtered to only find ones that give int solutions when put into the quadratic formula, and balanced to include the same amount of vars that give each type of solutions (0, 1, 2)
- permutation_list = [] #the unfiltered list of permutations
- #permutates every number under the max range into 3-long tuples and cycles through them
- for permutation in itertools.permutations(range(self.MAX_RANGE), 3):
- permutation_list.append(permutation) #appends the permutations to the raw permutation list
- permutation_list_0_solutions = []
- permutation_list_1_solution = []
- permutation_list_2_solutions = []
- solutions = self.quadratic_formula(permutation[0], permutation[1], permutation[2]) #puts the contents of current tuple through the quadratic formula
- print(permutation) #DEBUG
- print(solutions) #DEBUG
- #puts permutations which solutions do not include floats into the list corresponding with their amount of solutions
- if all(type(solution) != float for solution in solutions): #cycles through the solutions, appending the corresponding permutation to the list only if neither of them are a float
- if all(solution is int for solution in solutions): #if all the solutions are ints (2 solutions)
- permutation_list_2_solutions.append(permutation)
- elif None in solutions and any(solution is int for solution in solutions): #if the solutions are an int and a None (1 solution)
- permutation_list_1_solution.append(permutation)
- elif all(solution == None for solution in solutions): #if all the solutions are None (0 solutions)
- permutation_list_0_solutions.append(permutation)
- else:
- continue #continues to next iteration if the current permutation's solution includes a float
- print(len(permutation_list_0_solutions)) #DEBUG
- print(len(permutation_list_1_solution)) #DEBUG
- print(len(permutation_list_2_solutions)) #DEBUG
- #appends an equal amount of random permutations with each type of solution to a list
- for _ in range(self.AMOUNT_PER_SOLUTION_TYPE):
- try:
- self.permutation_list_filtered.append(random.choice(permutation_list_0_solutions))
- except IndexError:
- pass
- for _ in range(self.AMOUNT_PER_SOLUTION_TYPE):
- try:
- self.permutation_list_filtered.append(random.choice(permutation_list_1_solution))
- except IndexError:
- pass
- for _ in range(self.AMOUNT_PER_SOLUTION_TYPE):
- try:
- self.permutation_list_filtered.append(random.choice(permutation_list_2_solutions))
- except IndexError:
- pass
- #raises an exception if the amount of filtered perms is less than 3 times the set amount per solution type
- if len(self.permutation_list_filtered) < 3 * self.AMOUNT_PER_SOLUTION_TYPE:
- raise Exception(f"Not enough filtered permutations in permutation_list_filtered (only {len(self.permutation_list_filtered)} present)")
- Main = Main()
- for permutation in Main.permutation_list_filtered:
- print(permutation)
- print(Main.quadratic_formula(permutation[0], permutation[1], permutation[2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement