Created
May 17, 2011 06:24
-
-
Save spariev/976040 to your computer and use it in GitHub Desktop.
This file contains 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 cc-rev | |
[s] | |
(let [paren? (fn [ch] (or (= ch \() (= ch \))))] | |
(apply str | |
(concat | |
(flatten | |
(->> s | |
(partition-by paren?) | |
(remove #(paren? (first %))) | |
(map-indexed (fn [idx ss] (if (odd? idx) (reverse ss) ss))))))))) | |
user=> (cc-rev "asc(vbn)ert(qwerty)uiop") | |
"ascnbvertytrewquiop" | |
def ccrev(str) | |
substrings = str.split(/\(|\)/) | |
result = '' | |
substrings.each_with_index{|ss,idx| result += (if (idx % 2) == 1 then ss.reverse else ss end)} | |
result | |
end | |
irb(main):028:0> ccrev("asc(vbn)ert(qwerty)uiop") | |
=> "ascnbvertytrewquiop" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment