So I am currently on the gearing up for destruction challenge, and I believe I have written a code that solves this problem. Any time the code is supposed to return [-1,-1] my code passes, but whenever I have to return an actual radius ratio it does not pass even though my code is returning the answer. The first example case is supposed to return [12,1] which my code does but it still fails. I have tried using the int function to make sure I am returning two integers. If I manually write "return [12,1]" it will pass that first example case but not with my code.
from fractions import Fraction
def solution(s):
pointList = []
pegDist = []
check = []
if(not s or len(s) < 2):
return[-1,-1]
if(len(s)==2):
firstGear = ((s[1]-s[0])/3 * 2).as_integer_ratio()
for i in firstGear:
pointList.append(i)
return pointList
for peg in s:
if s.index(peg) == 0:
pointList.append(2)
elif s.index(peg) == len(s) - 1:
pointList.append(1)
else:
pointList.append(1)
if s.index(peg) != len(s) - 1:
pegDist.append(abs(peg-s[s.index(peg)+1]))
holder = pointList
while(True):
try:
for x in pointList:
if(pointList.index(x)>0 and pointList.index(x)<len(pointList)-1):
pointList[pointList.index(x)] = abs(pegDist[pointList.index(x)-1]-pointList[pointList.index(x)-1])
for x in pointList:
if(pointList.index(x)!=len(pointList)-1):
check.append(x+pointList[pointList.index(x)+1])
if(check[len(check)-1]!=pegDist[len(check)-1]):
check = []
pointList = holder
pointList[0]+=1
pointList[len(pointList)-1]=pointList[0]/2
if(check==pegDist):
ratio = Fraction(holder[0])
break
except IndexError:
return [-1,-1]
return[ratio.numerator,ratio.denominator]