Advertisement
Guest User

Untitled

a guest
Nov 8th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.02 KB | None | 0 0
  1. from math import sqrt
  2. import itertools
  3. import random
  4.  
  5.  
  6. class Main:
  7.  
  8.    
  9.    
  10.    
  11.     def __init__(self):
  12.  
  13.        
  14.         self.MAX_RANGE = 10 #determines the highest possible value of the numbers to be permutated
  15.         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)
  16.         self.permutation_list_filtered = [] #a list of permutations containing the same amount of vars for all 3 types of solutions, only including int solutions
  17.  
  18.         self.permutation_list_generation() #creates and fills self.permutation_list_filtered
  19.  
  20.  
  21.  
  22.  
  23.    
  24.     def quadratic_formula(self, a, b, c): #puts vars a, b and c through the quadratic formula
  25.  
  26.  
  27.  
  28.         #print(a, b, c) #DEBUG
  29.  
  30.        
  31.         try:
  32.             solution1 = (-b + sqrt(b**2 - 4*a*c)) / (2*a) #defines the first solution to the 2nd degree equation using the abc formula
  33.        
  34.         except (ValueError, ZeroDivisionError): #secures against errors caused by trying to find the root of a negative number or dividing by 0
  35.  
  36.             solution1 = None
  37.  
  38.        
  39.  
  40.  
  41.         try:
  42.             solution2 = (-b - sqrt(b**2 - 4*a*c)) / (2*a) #the second solution
  43.        
  44.         except (ValueError, ZeroDivisionError): #secures against errors caused by trying to find the root of a negative number or dividing by 0
  45.  
  46.             solution2 = None
  47.  
  48.  
  49.  
  50.  
  51.         solution_list = [solution1, solution2] #puts the two solutions into a list
  52.         solution_list_no_decimals = [] #the list to which the solutions are appended to once the ints with decimals have been eliminated
  53.  
  54.  
  55.  
  56.  
  57.         #this turns ints with decimals into proper ints to not confuse the program later on
  58.        
  59.         for solution in solution_list: #cycles through each solution
  60.            
  61.             #print(solution) #DEBUG
  62.  
  63.             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)
  64.                 solution_list_no_decimals.append(int(solution))
  65.                
  66.             else:
  67.                 solution_list_no_decimals.append(solution) #turns the solution into an int, removing its decimals
  68.  
  69.  
  70.  
  71.  
  72.         return solution_list_no_decimals #returns a list of all the possible solutions
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.     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)
  81.  
  82.        
  83.         permutation_list = [] #the unfiltered list of permutations
  84.  
  85.         #permutates every number under the max range into 3-long tuples and cycles through them
  86.         for permutation in itertools.permutations(range(self.MAX_RANGE), 3):
  87.             permutation_list.append(permutation) #appends the permutations to the raw permutation list
  88.  
  89.  
  90.            
  91.            
  92.             permutation_list_0_solutions = []
  93.             permutation_list_1_solution = []
  94.             permutation_list_2_solutions = []
  95.  
  96.  
  97.             solutions = self.quadratic_formula(permutation[0], permutation[1], permutation[2]) #puts the contents of current tuple through the quadratic formula
  98.            
  99.             print(permutation) #DEBUG
  100.             print(solutions) #DEBUG
  101.  
  102.             #puts permutations which solutions do not include floats into the list corresponding with their amount of solutions
  103.             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
  104.                
  105.                 if all(solution is int for solution in solutions): #if all the solutions are ints (2 solutions)
  106.                     permutation_list_2_solutions.append(permutation)
  107.                
  108.                 elif None in solutions and any(solution is int for solution in solutions): #if the solutions are an int and a None (1 solution)
  109.                     permutation_list_1_solution.append(permutation)
  110.                
  111.                 elif all(solution == None for solution in solutions): #if all the solutions are None (0 solutions)
  112.                     permutation_list_0_solutions.append(permutation)
  113.  
  114.  
  115.                
  116.  
  117.  
  118.             else:
  119.                 continue #continues to next iteration if the current permutation's solution includes a float
  120.            
  121.  
  122.  
  123.         print(len(permutation_list_0_solutions)) #DEBUG
  124.         print(len(permutation_list_1_solution)) #DEBUG
  125.         print(len(permutation_list_2_solutions)) #DEBUG
  126.        
  127.        
  128.         #appends an equal amount of random permutations with each type of solution to a list
  129.  
  130.         for _ in range(self.AMOUNT_PER_SOLUTION_TYPE):
  131.                
  132.             try:  
  133.                 self.permutation_list_filtered.append(random.choice(permutation_list_0_solutions))
  134.             except IndexError:
  135.                 pass
  136.  
  137.  
  138.         for _ in range(self.AMOUNT_PER_SOLUTION_TYPE):
  139.  
  140.             try:  
  141.                 self.permutation_list_filtered.append(random.choice(permutation_list_1_solution))
  142.             except IndexError:
  143.                 pass
  144.  
  145.  
  146.         for _ in range(self.AMOUNT_PER_SOLUTION_TYPE):
  147.  
  148.             try:  
  149.                 self.permutation_list_filtered.append(random.choice(permutation_list_2_solutions))
  150.             except IndexError:
  151.                 pass
  152.  
  153.  
  154.         #raises an exception if the amount of filtered perms is less than 3 times the set amount per solution type
  155.         if len(self.permutation_list_filtered) < 3 * self.AMOUNT_PER_SOLUTION_TYPE:
  156.  
  157.             raise Exception(f"Not enough filtered permutations in permutation_list_filtered (only {len(self.permutation_list_filtered)} present)")
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169. Main = Main()
  170.  
  171.  
  172. for permutation in Main.permutation_list_filtered:
  173.     print(permutation)
  174.     print(Main.quadratic_formula(permutation[0], permutation[1], permutation[2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement