这是python 2.7中的工作代码,所有测试用例均已通过Google传递。这是我抓了一段时间纸之后想到的最好的解决方案:
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. # since for valid results, LastGearRadius >= 1 and FirstGearRadius = 2 * LastGearRadius # thus for valid results FirstGearRadius >= 2 if FirstGearRadius < 2: return [-1,-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]
请参阅此图像,以了解我如何编写此代码:



