Back to General discussions forum
Ok. My code might be somehow mistaken, but I know I have put a lot of effort to come up with an algorithm good enough to be right with all exact input datas. Well, but only with exact input datas...
Here's my code (Python):
from math import sqrt
qtdHoras = int(input('Entre com a quantidade de horas para calcular as coordenadas: ').strip())
horarios = input('Entre com os horĂ¡rios fornecidos pelo CodeAbbey: ').strip().split()
result = []
for n in range(qtdHoras):
if len(horarios[n]) == 5:
hora = int(horarios[n][0]+horarios[n][1])
else:
hora = int(horarios[n][0])
minuto = int(horarios[n][-2]+horarios[n][-1])
hora %= 12
total = (hora*60)+minuto
if hora <= 6:
yHora = (-total + 480)/30
xHora = 10+(sqrt(36-(abs(10-yHora)**2)))
else:
yHora = 20-((-(total-360) + 480)/30)
xHora = 10-(sqrt(36-(abs(10-yHora)**2)))
if minuto <= 30:
yMinuto = ((-minuto * 18) + 570)/30
xMinuto = 10+(sqrt(81-(abs(10-yMinuto)**2)))
else:
yMinuto = 20-((-(minuto-30) * 18) + 570)/30
xMinuto = 10-(sqrt(81-(abs(10-yMinuto)**2)))
result.append(str(xHora)); result.append(str(yHora)); result.append(str(xMinuto)); result.append(str(yMinuto))
print('\n--- R E S U L T A D O ---\n'+' '.join(result))
Everything you have to do to try my code out is to copy-paste Problem 74's input data into your running interpreter and press Enter.
I have tried debugging my code by hand and 9:30's xHora variable returns (when doing by hand) 11.0. It's obvious, since 9:30 is right between 9:00 and 10:00, which return 10.0 and 12.0 respectively.
This problem's answer states that 9:30 should return 11.55291427.
I ask you then, what is wrong with my code?
Try to look at analog clock face and think how much hours hand y axis changes from 9:00
to 10:00, then from 10:00 to 11:00 and from 11:00 to 12:00.
You may notice that the change ratio keeps decreasing until it reaches 12:00.
Now take a look at this
You probably get the idea why coordinates of 9:30 is not in the midlle between 10 and 12,
so i won't spoil the solution futher. This is a basic trigonometry related problem and
you don't need to reinvent the wheel here, just find the right formulas.