Last active
May 2, 2019 00:44
-
-
Save seisvelas/43a62d8e5f392067e98895515e9d9438 to your computer and use it in GitHub Desktop.
Solution to Lorenzo's Clock Angle Problem
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
(defun h->m (h m) | |
(* 5 (+ (mod h 12) | |
(/ m 60)))) | |
(defun clock° (h m) | |
(let ((° (abs (* 6 (- m (h->m h m)))))) | |
(min ° (- 360 °)))) | |
(write (clock° 11 27)) |
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
#lang racket | |
(define (hour-position hour minute) | |
; if hour is 12, set to 0, otherwise leave unchanged | |
(+ (* 5 (modulo hour 12)) | |
; number (in minutes) ahead of hour the hour hand should be | |
(* 5 (/ minute 60)))) | |
(define-syntax-rule (time-angle time) | |
; convert 2:20 to the string "2:20". This is impossible in other languages | |
(let ((time (symbol->string 'time))) | |
(let* ((clock-hands (string-split time ":")) | |
(hour (string->number (first clock-hands))) | |
(minute (string->number (second clock-hands)))) | |
(string-append (number->string | |
; convert from 60 minute clock to 360° circle (60 * 6 = 360) | |
(abs (- (* 6 (hour-position hour minute)) | |
(* 6 minute)))) | |
; append degree symbol | |
"°")))) | |
(display (time-angle 2:20)) ; displays 50° |
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
defmodule ClockAngle do | |
def pos(hour, minute) do | |
5 * (rem(hour, 12) + minute / 60) | |
end | |
def angle(hour, minute, second) do | |
hour_pos = pos(hour, minute) | |
angle = abs(6 * (second - hour_pos)) | |
inner_angle = min(angle, 360 - angle) | |
inner_angle | |
end | |
end | |
IO.puts ClockAngle.angle(2, 20, 20) |
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
let hourPos = (h, m) => | |
5*((h % 12) + (m / 60)) | |
let clockAngle = (h, m) => { | |
let angle = Math.abs(6*(m-hourPos(h, m))) | |
return Math.min(angle, 360 - angle) | |
} |
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
def hourPos(hour, minute): | |
return 5 * ((hour % 12) + (minute / 60)) | |
def clockAngle(hour, minute, sec): | |
angle = abs(sec - hourPos(hour, minute)) | |
return 6 * min(angle, 360 - angle) | |
# h m s | |
clockAngle(3, 15, 15) |
The Scheme definitely looks messier, but consider also that I used a macro so you can say (time-angle 7:30)
, which in Python is outright impossible. Also, I used lots of dumb tricks like (apply - ...) instead of just subtracting the values.
You, dear reader, may not notice because I'm about to beautify it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In honor of Lorenzo (an MIT grad) I chose to solve this in Scheme (a language invented at MIT).
You rock, Lorenzo!