Skip to content

Instantly share code, notes, and snippets.

@joshuashaffer
Created October 15, 2017 16:21
Show Gist options
  • Save joshuashaffer/24e9d3de5f41aaca1fbb605f762fb115 to your computer and use it in GitHub Desktop.
Save joshuashaffer/24e9d3de5f41aaca1fbb605f762fb115 to your computer and use it in GitHub Desktop.
Given a time, your mission is to calculate the angle between the hour hand and the minute hand on an analog clock and return it in radians in lowest terms.
import fractions as f
def clockRadian(time):
   h, m = tuple(map(int, time.split(':')))
   t = abs((h % 12) * 60 - m * 12 + m)
   t = 720 - t if t > 360 else t
   if t == 0:
       return "0"
   a = f.Fraction(t, 360)
   b = a.numerator
   c = a.denominator
   return "{}pi/{} ".format(b if b > 1 else '', c)[0:-1 if c > 1 else -3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment