Skip to content

Instantly share code, notes, and snippets.

@spariev
Created May 17, 2011 06:24
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 spariev/976040 to your computer and use it in GitHub Desktop.
Save spariev/976040 to your computer and use it in GitHub Desktop.
(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