Skip to content

Instantly share code, notes, and snippets.

@pitt500
Created May 29, 2020 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pitt500/6d96964a01c7cd01ebbef95b7149b2b3 to your computer and use it in GitHub Desktop.
Save pitt500/6d96964a01c7cd01ebbef95b7149b2b3 to your computer and use it in GitHub Desktop.
Given a clock time in hh:mm format, determine, to the nearest degree, the angle between the hour and the minute hands.
func getNearestDegree(time: String) -> Int {
let array = time.split(separator: ":")
let hours = Int(array[0])!
let minutes = Int(array[1])!
let hourPosition = hours*60 + minutes
let minutesPosition = 12*minutes
let actualPosition = hourPosition - minutesPosition
let degrees = actualPosition/2
if degrees > 180 {
return abs(360 - degrees)
}
return degrees
}
let sol = getNearestDegree(time: "9:14")
print("\(sol) degrees")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment