CheckiO Weekly #0 ― Clock Angle Review
Hello, Checkio Friends!
In this weekly Checkio Mission Review (I hope it will be weekly) we look at various missions and the interesting (or just random) solutions for them. As before, any ideas, comments or questions are welcome and can be left on this post.
Today we will examine the "Clock Angle" mission. You are given a time in 24-hour format and you should calculate a lesser angle between the hour and minute hands in degrees for analog clock. Simple mathematical model. But you can solve this even if you don't have doctor degree in Mathematics. Just watch at an analog clock. Old grandmother cuckoo-clock will be nice or find some video with analog clock (yeah it's sound weird).
The time is given as as a string in "HH:MM" format. So first we need to get some numbers from this. You can use regexp if you a fan of perl for example, or just split by ":" and convert to int for each part. Don't forget to convert from the 24 to 12 hour intervals. The modulo operation is enough for this. From here we can find the angles for each hand and the "zero-value" (12 hours or 0 minutes). For the minute hand it's simple; we know a full circle is 360 degrees, this makes each minute 6 degrees. Thus if we multiply minutes by 6 and we get the minute angle. With the hour hand things are a little trickier, because this hand moves with each minute too. First, for each hour we have 360 / 12 = 30 degrees. Second, for each minute the hour hand moves at 30 / 60 = 0.5 degrees – 30 is degrees for one hour and 60 is number of minutes in each hour. So the hour-angle will be received as a sum of two numbers -- hours by 30 and minutes by 0.5. Now we have two angles, find the absolute value of the difference and you get the results. Gotcha. Yeeeaah, I did forget one liiiiitle detail, but I think you can find it out yourself.
def clock_angle(time): hour, minutes = map(int, time.split(':')) angle = abs(30 * (hour % 12) - 5.5 * minutes) return min(angle, 360 - angle)
Sim0000 merged the minute angle from the hour and minute hand and got 5.5. Firstly, the "map" may be looked at as "overhead", but this allows them to easily expand function for second hand and more DRY. For python newbies – "int" here is a function to convert strings to integers and strip forwarding zeroes.
hour = hour if hour < 12 else hour - 12 ... angle = angle if angle <= 180 else 360 - angle
180 - abs(180 - (h * 60 + m) % (720 / 11) * 11 / 2)
From his comment "I’ve learned basic Signal Processing, so I would search and formulate a periodicity in the outputs after solving.". Mmmmm, pure math (wink)
min(abs(i - abs(30*((h, h-12)[h>12]) - 5.5*m)) for i in (360, 0))
"for"?!? Yes, I think Hatrik showed how some code which can "hide" bad constructions. You do code reviews for you collegue while you drink your coffee and watch twitter (Our link) with one eye aaaand just easily skip that construction.
And from this mission in the "Scary" category for solutions. At first place in top, we have Veky with Scary math solution (that's why we did not see he in "Creative" category). This solution is the real Nightmare for people who turn white with mentions of TRIGONOMETRY! Early we saw pure math, now look at complex math (I can not stand and don't post it right here):
from cmath import rect, phase from math import radians, degrees h, m = map(int, time.split(":")) return degrees(abs(phase(rect(1, radians(30 * h - 5.5 * m)))))
And one more solution from Hanpari – "Awful story". This a simple solution, but it can be told near campfire in dark forest when you will gather with your developer friends. Take some time, you really need to read this.
That's all folks. We are going to do these reviews weekly, so this is just the first pitch. Please tell us your thoughts in the comments below. Maybe we can take a look at random "bad" solutions to some favorite missions, or take a look at more solutions per mission. Let me know if you have any ideas!
Welcome to CheckiO - games for coders where you can improve your codings skills.
The main idea behind these games is to give you the opportunity to learn by exchanging experience with the rest of the community. Every day we are trying to find interesting solutions for you to help you become a better coder.Join the Game