Google foobar gearing_up_for_destruction

后端 未结 9 690
醉话见心
醉话见心 2020-12-29 11:12

I was doing the google foobar challenge but ran out of time on the following challenge i am trying to see what i did wrong.

Challenge

As

9条回答
  •  余生分开走
    2020-12-29 11:43

    My python code uses fairly basic operations to get things done without brute-forcing it. However I'm lazy and didn't really comment, so you're gonna have to figure it out yourself. It passed the foobar solution thing, so it definitely works.

    def answer(pegs):
    distances = [pegs[x+1]-pegs[x] for x in range(len(pegs)-1)]
    x = 0 
    for i in distances: #gets d_(n)-d_(n-1)+d_(n-2)...+-d_(1)
        x = i - x
    #this tests if firstGearRadius is positive or negative
    if len(distances)%2 == 0: #if it's positive
        solution =  [x*-2,1] 
    elif len(distances)%2 == 1: #if it's negative
        if x*2 % 3 == 0: #if the numerator is divisible by 3
            solution = [x*2/3,1]
        else:
            solution = [x*2,3]
    #finds sizes of the gears
    gearSizes = [float(solution[0])/float(solution[1])]
    x = gearSizes[0]
    for i in distances:
        x = i - x
        gearSizes.append(x)
    if any([True for x in gearSizes if x<=1]): #gears must be at least 1 unit radius
        return [-1,-1]
    else:
        return solution
    

提交回复
热议问题