Created
May 29, 2023 01:03
-
-
Save abenezerangelos/d304c87272c6a32e46fbf78ea1054644 to your computer and use it in GitHub Desktop.
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
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,37.75], | |
[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() | |
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()) | |
increment = Decimal('0.1') | |
number = 0 + increment | |
dictionary_values = list(dictionary.values()) | |
while summation!=distance and number<da[0]-increment: | |
summation = 0 | |
for k in range(len(dictionary_values)): | |
if k==0 or k==len(keys)-1: | |
dictionary[keys[0]][0]=number | |
dictionary[keys[len(keys)-1]][0]=number/2 | |
elif k>len(keys)-1: | |
print("CAUTION,DANGER!!!") | |
exit("Fix code") | |
pass | |
else: | |
replacement=da[k-1]-dictionary[keys[k-1]][0] | |
if replacement>0: | |
dictionary[keys[k]][0]=replacement | |
dictionary[keys[k]][1]=replacement | |
# print(k) | |
print("Dictionary values:",dictionary_values, dictionary_values[k]) | |
summation += sum(dictionary_values[k]) | |
# print("Dictionary",dictionary) | |
print("DA",da) | |
print("Summation",summation) | |
if summation==distance: | |
return[int(Fraction(number).limit_denominator().numerator),int(Fraction(number).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 input: | |
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