Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # this passes all but Test Case 7
- # now let's try to crak the test cases
- # ;)
- def distances(pegs):
- l = len(pegs) - 1
- return [pegs[i+1] - pegs[i] for i in range(l)]
- def poop(pegs):
- l = len(pegs) - 1
- def solve(pegs):
- d = distances(pegs)
- m = len(d) - 1
- sign = 1
- g = [0 for i in range(l)]
- # this is what elimination turns out to be
- for i in range(m):
- sign *= -1
- d[i+1] -= d[i]
- # get our first value
- x = d[-1]
- g[0] = x/(sign + 0.5)
- # and the rest of them!
- d = distances(pegs)
- for i in range(m):
- g[i+1] = d[i] - g[i]
- return g, sign
- def check(g):
- # gears ain't NEGATIVE
- for i, x in enumerate(g):
- if x <= 0:
- # print 'Negative gear:', i, x
- return False
- if g[0] < 1 and abs(g[0] - 1.0) > 0.01:
- # print 'Too small'
- return False
- return True
- def fraction(ans, sign):
- if sign == -1:
- return [int(round(ans)), 1]
- elif sign == 1:
- # .33333 vs .00001
- if abs(ans - round(ans)) < 0.01:
- return [int(round(ans)), 1]
- else:
- return [int(round(3*ans)), 3]
- gears, sign = solve(pegs)
- ans = gears[0]
- if check(gears):
- return fraction(ans, sign)
- else:
- return [-1, -1]
- def answer(pegs):
- correct_answer = poop(pegs)
- return correct_answer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement