Skip to content

Instantly share code, notes, and snippets.

@kolharsam
Last active April 1, 2020 13:13
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 kolharsam/808ba20e7284d7af1765c25665d9c008 to your computer and use it in GitHub Desktop.
Save kolharsam/808ba20e7284d7af1765c25665d9c008 to your computer and use it in GitHub Desktop.
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

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

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