Created
May 29, 2020 18:21
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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