Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lamichhanesuresh/ab9ac38d5e4520d54d7ad9cd1fe7f090 to your computer and use it in GitHub Desktop.
Save lamichhanesuresh/ab9ac38d5e4520d54d7ad9cd1fe7f090 to your computer and use it in GitHub Desktop.
Gearing_Up_For_Destruction
from fractions import Fraction
def answer(pegs):
arrLength = len(pegs)
if ((not pegs) or arrLength == 1):
return [-1,-1]
even = True if (arrLength % 2 == 0) else False
sum = (- pegs[0] + pegs[arrLength - 1]) if even else (- pegs[0] - pegs[arrLength -1])
if (arrLength > 2):
for index in xrange(1, arrLength-1):
sum += 2 * (-1)**(index+1) * pegs[index]
FirstGearRadius = Fraction(2 * (float(sum)/3 if even else sum)).limit_denominator()
#now that we have the radius of the first gear, we should again check the input array of pegs to verify that
#the pegs radius' is atleast 1.
currentRadius = FirstGearRadius
for index in xrange(0, arrLength-2):
CenterDistance = pegs[index+1] - pegs[index]
NextRadius = CenterDistance - currentRadius
if (currentRadius < 1 or NextRadius < 1):
return [-1,-1]
else:
currentRadius = NextRadius
return [FirstGearRadius.numerator, FirstGearRadius.denominator]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment