Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cassidy's Interview Question - 30/03
(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)
@stkent
Copy link

stkent commented Mar 31, 2020

I think the solution fails for start = 1, goal = 3; the sequence of starts this code would iterate through looks like it would be infinite: 1 -> 2 -> 1 -> 2 -> ...? solution updated!

@kolharsam
Copy link
Author

kolharsam commented Mar 31, 2020

Thank you for pointing this out.
I guess, back to drawing board for this one. Will correct it soon!

@stkent
Copy link

stkent commented Mar 31, 2020

No worries; I've been running into similar edge cases!

@kolharsam
Copy link
Author

kolharsam commented Apr 1, 2020

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