Teaching Kids Programming – Hour and Minute Angle on a Clock


Teaching Kids Programming: Videos on Data Structures and Algorithms

Given a clock time with hour and integer minutes, determine the smaller angle between the hour and the minute hands and floor it to the nearest integer. Note that the hour hand moves too. Bonus: When, during the course of a day, will the angle be zero?

Constraints
0 ≤ hour ≤ 23
0 ≤ minutes ≤ 59
Example 1
Input
hour = 12
minutes = 22
Output
121
Remember to take the angle displaced by the hour hand due to movement by minute hand (Try to make floating type calculations).
if the angle is > 180 , return 360-angle

Compute the Angle between Hour and Minute on a Clock

The key part here is that we need to consider the movement of hour while minute hand moves. Each hour angle is 360/12=30 degree and each minute angle is 360/60=6 degree. Compute the absolute difference value between hour-angle and minute-angle.

1
2
3
4
5
6
7
class Solution:
    def angleBetweenHourAndMinuteClock(self, hour, minutes):
        hour %= 12
        hh = (hour + minute/60)*30
        mm = minutes * 6
        ang = abs(hh - mm)
        return floor(min(ang, 360-ang))
class Solution:
    def angleBetweenHourAndMinuteClock(self, hour, minutes):
        hour %= 12
        hh = (hour + minute/60)*30
        mm = minutes * 6
        ang = abs(hh - mm)
        return floor(min(ang, 360-ang))

The time complexity and space complexity is O(1) constant as pure math calculations are performed.

See also: Compute the Angle of the Hour and Minute Hand on a Clock

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
340 words
Last Post: Alarming on High Disk Usage using BASH + AWK + Crontab Job
Next Post: Sum of Digits in Base K

The Permanent URL is: Teaching Kids Programming – Hour and Minute Angle on a Clock

Leave a Reply