Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. # Gearing Up for Destruction
  2. # ==========================
  3.  
  4. # As Commander Lambda's personal assistant, you've been assigned the task of configuring the LAMBCHOP doomsday device's axial orientation gears. It should be pretty
  5. # simple - just add gears to create the appropriate rotation ratio. But the problem is, due to the layout of the LAMBCHOP and the complicated system of beams and pipes supporting it,
  6. # the pegs that will support the gears are fixed in place.
  7.  
  8. # The LAMBCHOP's engineers have given you lists identifying the placement of groups of pegs along various support beams. You need to place a gear on each peg (otherwise the gears
  9. # will collide with unoccupied pegs). The engineers have plenty of gears in all different sizes stocked up, so you can choose gears of any size, from a radius of 1 on up. Your goal
  10. # is to build a system where the last gear rotates at twice the rate (in revolutions per minute, or rpm) of the first gear, no matter the direction. Each gear (except the last)
  11. # touches and turns the gear on the next peg to the right.
  12.  
  13. # Given a list of distinct positive integers named pegs representing the location of each peg along the support beam, write a function answer(pegs) which, if there is a solution,
  14. # returns a list of two positive integers a and b representing the numerator and denominator of the first gear's radius in its simplest form in order to achieve the goal above,
  15. # such that radius = a/b. The ratio a/b should be greater than or equal to 1. Not all support configurations will necessarily be capable of creating the proper rotation ratio, so if
  16. # the task is impossible, the function answer(pegs) should return the list [-1, -1].
  17.  
  18. # For example, if the pegs are placed at [4, 30, 50], then the first gear could have a radius of 12, the second gear could have a radius of 14, and the last one a radius of 6. Thus,
  19. # the last gear would rotate twice as fast as the first one. In this case, pegs would be [4, 30, 50] and answer(pegs) should return [12, 1].
  20.  
  21. # The list pegs will be given sorted in ascending order and will contain at least 2 and no more than 20 distinct positive integers, all between 1 and 10000 inclusive.
  22.  
  23. from fractions import Fraction
  24.  
  25. def invert(matrix):
  26. n = len(matrix)
  27. inverse = [[Fraction(0) for col in range(n)] for row in range(n)]
  28.  
  29. for i in range(n):
  30. inverse[i][i] = Fraction(1)
  31.  
  32. for i in range(n):
  33. for j in range(n):
  34. if i != j:
  35. if matrix[i][i] == 0:
  36. return false
  37. ratio = matrix[j][i] / matrix[i][i]
  38. for k in range(n):
  39. inverse[j][k] = inverse[j][k] - ratio * inverse[i][k]
  40. matrix[j][k] = matrix[j][k] - ratio * matrix[i][k]
  41.  
  42. for i in range(n):
  43. a = matrix[i][i]
  44. if a == 0:
  45. return false
  46. for j in range(n):
  47. inverse[i][j] = inverse[i][j] / a
  48. return inverse
  49.  
  50.  
  51.  
  52. def answer(pegs):
  53. # your code herel
  54.  
  55.  
  56. if len(pegs) == 2:
  57. radius_second = (Fraction(pegs[1] - pegs[0]) / Fraction(3))
  58.  
  59. if radius_second<1:
  60. return [-1,-1]
  61.  
  62. radius_first = radius_second * Fraction(2)
  63.  
  64.  
  65. return [radius_first.numerator, radius_first.denominator]
  66.  
  67.  
  68.  
  69. distance = [0] * (len(pegs)-1)
  70.  
  71.  
  72.  
  73. for i in xrange(0,len(pegs)-1):
  74. distance[i] = pegs[i+1] - pegs[i]
  75.  
  76. #radius = np.linalg.solve(build_matrix(len(pegs)-1), distance)
  77.  
  78. a = build_matrix(len(pegs)-1)
  79. a_inv = invert(a)
  80.  
  81.  
  82. radius_list = []
  83. for i in range(0, len(a_inv)):
  84. current_gear_radius = 0
  85. for j in range(0, len(a_inv[i])):
  86. current_gear_radius += a_inv[i][j] * distance[j]
  87. radius_list.append(current_gear_radius)
  88.  
  89. for radius in radius_list:
  90. if radius<1:
  91. return [-1, -1]
  92.  
  93. first_gear_radius = radius_list[-1] * Fraction(2)
  94.  
  95.  
  96.  
  97. return first_gear_radius.numerator,first_gear_radius.denominator
  98.  
  99. def build_matrix(n):
  100. mat = [[0 for x in range(n)] for y in range(n)]
  101.  
  102. mat[0][0] = 1
  103. mat[0][n-1] = 2
  104.  
  105. for i in range(1,n):
  106. mat[i][i-1] = 1
  107. mat[i][i] = 1
  108.  
  109. return mat
  110.  
  111. print answer([4,30,50])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement