Skip to content

Instantly share code, notes, and snippets.

@abenezerangelos
Created May 29, 2023 01:01
Show Gist options
  • Save abenezerangelos/afd947878d37c5062d63ee27434ac22e to your computer and use it in GitHub Desktop.
Save abenezerangelos/afd947878d37c5062d63ee27434ac22e to your computer and use it in GitHub Desktop.
import time
from fractions import *
from decimal import *
from collections import *
input=[[4, 17, 50],
[4, 30, 50],
[4,36,67],
[4,36,67,69],
[4,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,54],
[0,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27],
[2,10,12,14,16,19],
[10,22,25,28,31],
[1,4,6],
[14,18,21,25,28,32]]
input1=[ [4, 30, 50],
[1, 5, 10],
[2, 5, 8, 12],
[1, 3, 5, 7, 9],
[4, 7, 9, 12, 15],
[2, 6, 12, 20, 30],
[1, 5, 9, 14, 20],
[10, 20, 30, 40, 50],
[1, 3, 7, 15, 31],
[1, 4, 9, 16, 25]]
pegs=input1[1]
def solution(pegs):
da=[]
distance = pegs[-1]-pegs[0]
for i in range(len(pegs) - 1):
da.append(pegs[i+1] - pegs[i])
dictionary=OrderedDict()
dictionary[0]=[0]
summation = 0
for i in range(len(pegs)):
if i==0 or i ==len(pegs)-1:
dictionary.setdefault(pegs[i],[1])
else:
dictionary.setdefault(pegs[i],[1,1])
keys = list(dictionary.keys())[1:]
increment = Decimal('0.1')
number = 0 + increment
dictionary_values = list(dictionary.values())[1:]
while summation!=distance and number<da[0]-increment:
i=0
summation = 0
for k in range(len(dictionary_values)):
if i==0 or i==len(keys)-1:
dictionary[keys[0]][0]=number*2
dictionary[keys[len(keys)-1]][0]=number
elif i>len(keys)-1:
pass
else:
replacement=da[i-1]-dictionary[keys[i-1]][0]
if replacement>0:
dictionary[keys[i]][0]=replacement;dictionary[keys[i]][1]=replacement
i+=1
summation += sum(dictionary_values[k])
if summation==distance:
return[int(Fraction(number*2).limit_denominator().numerator),int(Fraction(number*2).limit_denominator().denominator)]
number+=increment
return [-1,-1]
def solution2(pegs):
n = len(pegs)
if n < 2:
return [-1, -1]
gear_sizes = [0] * n
gear_sizes[0] = 2
for i in range(1, n):
gear_sizes[i] = pegs[i] - pegs[i-1] - gear_sizes[i-1]
# Check if the gear size becomes negative or zero
if gear_sizes[i] <= 0:
return [-1, -1]
# Calculate the last gear size
last_gear_size = gear_sizes[n-1]
for i in range(n-2, 0, -1):
last_gear_size -= gear_sizes[i]
# Check if the last gear size becomes negative
if last_gear_size <= 0:
return [-1, -1]
# Check if the last gear size is twice the first gear size
if last_gear_size != 2 * gear_sizes[0]:
return [-1, -1]
# Convert the gear sizes to the numerator and denominator of the first gear's radius
radius = Fraction(gear_sizes[0], 1)
return [radius.numerator, radius.denominator]
start=time.time()
for i in input1:
result=solution(i)
print("RESULT",result)
end=time.time()
print(end-start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment