Created
November 11, 2016 19:39
-
-
Save 1lann/be45311db1bd8cbbe6650b0a3e9d1977 to your computer and use it in GitHub Desktop.
gearing_up_for_destruction Google foobar solution. I'm posting this because I'm angry that foobar glitched up and ate my solution, and `submit` didn't actually go through. `verify` however said it passed all tests. Oh well, I'll post it here for science.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from fractions import Fraction | |
def invert(matrix): | |
n = len(matrix) | |
inverse = [[Fraction(0) for col in range(n)] for row in range(n)] | |
for i in range(n): | |
inverse[i][i] = Fraction(1) | |
for i in range(n): | |
for j in range(n): | |
if i != j: | |
if matrix[i][i] == 0: | |
return false | |
ratio = matrix[j][i] / matrix[i][i] | |
for k in range(n): | |
inverse[j][k] = inverse[j][k] - ratio * inverse[i][k] | |
matrix[j][k] = matrix[j][k] - ratio * matrix[i][k] | |
for i in range(n): | |
a = matrix[i][i] | |
if a == 0: | |
return false | |
for j in range(n): | |
inverse[i][j] = inverse[i][j] / a | |
return inverse | |
def answer(pegs): | |
if len(pegs) < 2: | |
return [-1, -1] | |
if len(pegs) == 2: | |
x = (Fraction(pegs[1] - pegs[0]) / Fraction(3)) * Fraction(2) | |
if (x.numerator < 1) or (x.numerator < x.denominator): | |
return [-1, -1] | |
return [x.numerator, x.denominator] | |
matrix = [] | |
rowNum = 0 | |
deltas = [] | |
for loc in pegs: | |
deltas.append(Fraction(pegs[rowNum + 1] - pegs[rowNum])) | |
if rowNum == 0: | |
row = [Fraction(2), Fraction(1)] + [Fraction(0)] * (len(pegs) - 3) | |
matrix.append(row) | |
elif rowNum == len(pegs) - 2: | |
row = [Fraction(1)] + [Fraction(0)] * (len(pegs) - 3) + [Fraction(1)] | |
matrix.append(row) | |
break | |
else: | |
row = [Fraction(0)] * rowNum + [Fraction(1), Fraction(1)] + [Fraction(0)] * (len(pegs) - rowNum - 3) | |
matrix.append(row) | |
rowNum = rowNum + 1 | |
inverse = invert(matrix) | |
if not(inverse): | |
return [-1, -1] | |
# Validate all gears | |
for i in range(1, len(pegs)-1): | |
y = Fraction(0) | |
for j in range(len(pegs)-1): | |
y = y + inverse[i][j] * deltas[j] | |
if (y.numerator < 1) or (y.numerator < y.denominator): | |
return [-1, -1] | |
x = Fraction(0) | |
for i in range(len(pegs)-1): | |
x = x + inverse[0][i] * deltas[i] | |
x = x * Fraction(2) | |
if (x.numerator < 1) or (x.numerator < x.denominator): | |
return [-1, -1] | |
return [x.numerator, x.denominator] | |
print(answer([1, 2])) |
I am struggling to understand the final part of the question.
it says:
returns a list of two positive integers a and b representing the numerator and denominator of the first gear's radius in its simplest form
what does it mean?
suppose I have the full list of gears radius like :
p = [4, 30, 50] and r = [12, 16, 6]
how do I get a
and b
?
I am struggling to understand the final part of the question.
it says:
returns a list of two positive integers a and b representing the numerator and denominator of the first gear's radius in its simplest form
what does it mean?
suppose I have the full list of gears radius like :
p = [4, 30, 50] and r = [12, 16, 6]
how do I geta
andb
?
here r = [12,14,6] .
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I came up with a very efficient solution for this problem. I also have an image there to explain if you are interested to see. Please upvote my answer if you like it. Thanks
https://stackoverflow.com/a/45626410/8447619