The follow code should correctly run the test cases at the bottom.
It works correctly. I just don’t feel like this is clearest way I could write it.
A lot of the complexity comes from having to return the middle two characters if the length of the string is even.
(defn convert-to-zero-based-list [index] (- index 1)) (defn convert-to-one-based-list [index] (+ index 1)) (defn get-middle-with-offset [s] (/ (convert-to-one-based-list (count s)) 2)) (defn get-start-of-middle [s] (convert-to-zero-based-list (Math/floor (get-middle-with-offset s)))) (defn get-end-of-middle [s] (Math/ceil (get-middle-with-offset s))) (defn get-middle [s] (subs s (get-start-of-middle s) (get-end-of-middle s))) (= (get-middle "a") "a") (= (get-middle "aa") "aa") (= (get-middle "aba") "b") (= (get-middle "abba") "bb") (= (get-middle "abcba") "c")