Advertisement
duquesne9

Code Wars - Common Denominators

Oct 19th, 2019
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. ##5 Kyu - Common Denominators
  2.  
  3. def is_common(arr, test_den):
  4.     for j in range(len(arr)):
  5.         if test_den % arr[j][1] != 0:   ##Check all denominators are factors of test
  6.             return False
  7.     return True
  8.    
  9. def convertFracts(lst):
  10.     max_d = 0                           ##Variable to hold largest existing denominator
  11.     for i in range(len(lst)):           ##Find the largest denominator in lst
  12.         if lst[i][1] > max_d:
  13.             max_d = lst[i][1]
  14.     play = True
  15.     counter = 2
  16.     test = 0                             ##Just to keep test in the right scope for for loop below
  17.     while play == True:
  18.         test = max_d * counter           ##Creates test lowest common denominator
  19.         if not is_common(lst, test):     ##Test not valid for all denominators    
  20.             counter += 1
  21.         else:                            ##Test is valid, break out of loop
  22.             play = False
  23.     for k in lst:                      
  24.         factor = test//k[1]              ##how to get to lowest common denominator
  25.         for m in range(len(k)):          ##raises all values to match lowest common denominator
  26.             k[m] = k[m] * factor
  27.     return lst
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement