patternMinor
Clojure reverse multiple times
Viewed 0 times
multipletimesclojurereverse
Problem
I have a piece of Clojure code that reverses a string recursively.
It is working but too slow (because I got the problem from
The algorithm is :
and so on..
First iteration :
However it was too slow. I pass about 31 tests with this one before the timeout. Here is the fastest I have for now :
This code passes about 72 tests before there is a timeout.
How can I make this code faster ?
It is working but too slow (because I got the problem from
Codewars and I cannot validate it before it timeouts).The algorithm is :
- take a string ex. "abc" and reverse it (r "abc") -> "cba"
- take the first position and reverse from there "c" + (r "ba") -> "cab"
- take the second position and reverse from there "ca" + (r "b") -> "cab"
and so on..
First iteration :
(defn reverse-fun [s]
(loop [r s p 0]
(if (= p (count s))
r
(let [[b e] (split-at p r)
e (reverse e)]
(recur (apply str (concat b e)) (inc p))))))However it was too slow. I pass about 31 tests with this one before the timeout. Here is the fastest I have for now :
(defn reverse-fun [s]
(apply str (loop [r (vec s) p 0]
(if (= p (count r))
r
(let [b (subvec r 0 p)
e (rseq (subvec r p (count r)))]
(recur (into [] (concat b e)) (inc p)))))))This code passes about 72 tests before there is a timeout.
How can I make this code faster ?
Solution
The intermediate reversals seem like a bit of a trick question and are not really necessary to solve this problem. They are a complicated way to describe reordering a sequence indexed
alternating from which end you take each time.
If you abandon the idea of creating the intermediate strings described it becomes more about how to select the right character from the input string for each position :
0 ... n by taking items n, 0, n-1, 1, n-2, 2 ...alternating from which end you take each time.
If you abandon the idea of creating the intermediate strings described it becomes more about how to select the right character from the input string for each position :
(ns reversefun.core)
(defn reverse-fun [s]
(->> (interleave (reverse s) s)
(take (count s))
(apply str)))Code Snippets
(ns reversefun.core)
(defn reverse-fun [s]
(->> (interleave (reverse s) s)
(take (count s))
(apply str)))Context
StackExchange Code Review Q#123312, answer score: 2
Revisions (0)
No revisions yet.