Skip to content

Instantly share code, notes, and snippets.

@jimfisher
Created March 7, 2011 21:36
Show Gist options
  • Save jimfisher/859294 to your computer and use it in GitHub Desktop.
Save jimfisher/859294 to your computer and use it in GitHub Desktop.
A simple program to calculate the difference in degrees between two hands on analog clock. This was a puzzle presented to me, so I thought I'd give it a shot in code.
#
# Author: Jim Fisher @jimfish
# Date: March 7th, 2011
# Description: A simple program to calculate the difference in degrees between two hands on analog clock. This was a puzzle presented to me, so I thought I'd give it a shot in code.
#
def difference_between_hands_on_an_analog_clock_in_degrees(time)
hours = time.strftime("%l").to_f
minutes = time.min.to_f
hratio = 1/12.0
mratio = 1/60.0
offset = minutes*mratio*hratio
(360 * offset + (360*hratio*hours - 360*mratio*minutes)).abs
end
puts "This program will calculate the degrees in difference between the hours and the minute hands on a clock. It includes the movement of the hour hand between the hours while the minute hand moves past the 12."
puts "Enter Hour (1-12)"
hour = gets
puts "Enter Minutes (0-59)"
minutes = gets
time = Time.parse("#{hour}:#{minutes}")
puts difference_between_hands_on_an_analog_clock_in_degrees(time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment