Last active
April 1, 2020 13:13
-
-
Save kolharsam/808ba20e7284d7af1765c25665d9c008 to your computer and use it in GitHub Desktop.
Cassidy's Interview Question - 30/03
This file contains hidden or 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
(defn multiply-two [x] (* x 2)) | |
(defn sub-one [x] (dec x)) | |
(defn broken-calc | |
([start goal] (broken-calc start goal 0)) | |
([start goal steps] | |
(if | |
(= start goal) | |
steps | |
(if | |
(even? goal) | |
(if | |
(<= (multiply-two start) goal) | |
(broken-calc (multiply-two start) goal (inc steps)) | |
(broken-calc (sub-one start) goal (inc steps))) | |
(if | |
(> start goal) | |
(broken-calc (sub-one start) goal (inc steps)) | |
(broken-calc (multiply-two start) goal (inc steps))))))) | |
(broken-calc 3 10) |
Thank you for pointing this out.
I guess, back to drawing board for this one. Will correct it soon!
No worries; I've been running into similar edge cases!
I've changed the solution slightly. In the end, it was a very elementary mistake I'd made earlier.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think the solution fails forsolution updated!start = 1
,goal = 3
; the sequence of starts this code would iterate through looks like it would be infinite:1 -> 2 -> 1 -> 2 -> ...
?