Determine if a string is collapsible: Difference between revisions

Line 504:
original string: <<< --- Harry S Truman >>>, length = 72
result string: <<< - Hary S Truman >>>, length = 17
</pre>
 
 
=={{header|Clojure}}==
<lang Clojure>
(defn collapse [s]
(let [runs (partition-by identity s)]
(apply str (map first runs))))
 
(defn run-test [s]
(let [out (collapse s)]
(str (format "Input: <<<%s>>> (len %d)\n" s (count s))
(format "becomes: <<<%s>>> (len %d)\n" out (count out)))))
</lang>
 
{{out}}
<pre>
Input: <<<>>> (len 0)
becomes: <<<>>> (len 0)
Input: <<<"If I were two-faced, would I be wearing this one?" --- Abraham Lincoln >>> (len 72)
becomes: <<<"If I were two-faced, would I be wearing this one?" - Abraham Lincoln >>> (len 70)
Input: <<<..1111111111111111111111111111111111111111111111111111111111111117777888>>> (len 72)
becomes: <<<.178>>> (len 4)
Input: <<<I never give 'em hell, I just tell the truth, and they think it's hell. >>> (len 72)
becomes: <<<I never give 'em hel, I just tel the truth, and they think it's hel. >>> (len 69)
Input: <<< --- Harry S Truman >>> (len 72)
becomes: <<< - Hary S Truman >>> (len 17)
</pre>
 
Anonymous user