Repository: exercism/clojure
Pull request #194 (2017-10-26 20:01:10)

added run length encoding exercise

haus: @themistoklik docstrings come before the param list for functions. other than that it is looking great.
themistoklik: Hey @haus , thank you for the comments.

Checked the travis.yml in the travis linter, seems valid. Anything else to check on my end?

Patch:
diff --git a/config.json b/config.json
index 336751d..f462a44 100644
--- a/config.json
+++ b/config.json
@@ -25,6 +25,14 @@
       "unlocked_by": null,
       "uuid": "c2100dff-4d18-4fcb-9035-6c57eddd6d37"
     },
+    {
+      "core": false,
+      "difficulty": 1,
+      "slug": "run-length-encoding",
+      "topics": null,
+      "unlocked_by": null,
+      "uuid": "04a6c1d6-6cce-4c87-a34b-23fdd9baf70d"
+    },
     {
       "core": false,
       "difficulty": 1,
diff --git a/exercises/run-length-encoding/README.md b/exercises/run-length-encoding/README.md
new file mode 100644
index 0000000..b173f3f
--- /dev/null
+++ b/exercises/run-length-encoding/README.md
@@ -0,0 +1,36 @@
+# Run Length Encoding
+
+Implement run-length encoding and decoding.
+
+Run-length encoding (RLE) is a simple form of data compression, where runs
+(consecutive data elements) are replaced by just one data value and count.
+
+For example we can represent the original 53 characters with only 13.
+
+```text
+"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"  ->  "12WB12W3B24WB"
+```
+
+RLE allows the original data to be perfectly reconstructed from
+the compressed data, which makes it a lossless data compression.
+
+```text
+"AABCCCDEEEE"  ->  "2AB3CD4E"  ->  "AABCCCDEEEE"
+```
+
+For simplicity, you can assume that the unencoded string will only contain
+the letters A through Z (either lower or upper case) and whitespace. This way
+data to be encoded will never contain any numbers and numbers inside data to
+be decoded always represent the count for the following character.
+
+## General help
+
+For more detailed information about running tests, code style and linting,
+please see the [help page](http://exercism.io/languages/clojure).
+
+## Source
+
+Wikipedia [https://en.wikipedia.org/wiki/Run-length_encoding](https://en.wikipedia.org/wiki/Run-length_encoding)
+
+## Submitting Incomplete Solutions
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.
diff --git a/exercises/run-length-encoding/project.clj b/exercises/run-length-encoding/project.clj
new file mode 100644
index 0000000..f671d7d
--- /dev/null
+++ b/exercises/run-length-encoding/project.clj
@@ -0,0 +1,4 @@
+(defproject run-length-encoding "0.1.0-SNAPSHOT"
+  :description "run-length-encoding exercise."
+  :url "https://github.com/exercism/clojure/tree/master/exercises/run-length-encoding"
+  :dependencies [[org.clojure/clojure "1.8.0"]])
diff --git a/exercises/run-length-encoding/src/example.clj b/exercises/run-length-encoding/src/example.clj
new file mode 100644
index 0000000..e324be6
--- /dev/null
+++ b/exercises/run-length-encoding/src/example.clj
@@ -0,0 +1,20 @@
+(ns run-length-encoding)
+
+(defn run-length-encode 
+  "encodes a string with run-length-encoding"
+  [s]
+  (apply str
+         (for
+           [ x (partition-by identity s)]
+           (str
+             (when-not (= 1 (count x)) (count x)) (first x)))))
+
+(defn run-length-decode 
+  "decodes a run-length-encoded string"
+  [s]
+  (->> s
+       (re-seq #"[0-9]+.|[a-zA-Z\s]")
+       (map #(if (= (count %) 1) (str "1" %) %))
+       (map #(list (Integer. (reduce str "" (butlast %))) (str (last %))))
+       (map #(apply str (repeat (first %) (second %))))
+       (reduce str)))
\ No newline at end of file
diff --git a/exercises/run-length-encoding/src/run_length_encoding.clj b/exercises/run-length-encoding/src/run_length_encoding.clj
new file mode 100644
index 0000000..225f76f
--- /dev/null
+++ b/exercises/run-length-encoding/src/run_length_encoding.clj
@@ -0,0 +1,11 @@
+(ns run-length-encoding)
+
+(defn run-length-encode
+  "encodes a string with run-length-encoding"
+  [s]
+  )
+
+(defn run-length-decode
+  "decodes a run-length-encoded string"
+  [s]
+  )
\ No newline at end of file


Test patch:
diff --git a/exercises/run-length-encoding/test/run_length_encoding_test.clj b/exercises/run-length-encoding/test/run_length_encoding_test.clj
new file mode 100644
index 0000000..598b34e
--- /dev/null
+++ b/exercises/run-length-encoding/test/run_length_encoding_test.clj
@@ -0,0 +1,57 @@
+(ns run-length-encoding-test
+  (:require [clojure.test :refer :all]
+            [run-length-encoding :as rle]))
+
+;;Tests for run-length-encoding exercise
+
+(deftest encode-empty-string
+         (testing "encode an empty string"
+                  (is (= (rle/run-length-encode "") ""))))
+
+(deftest encode-single-characters-without-count
+         (testing "encode single characters without count"
+                  (is (= (rle/run-length-encode "XYZ") "XYZ"))))
+
+(deftest encode-string-with-no-single-characters
+         (testing "encode string with no single characters"
+                  (is (= (rle/run-length-encode "AABBBCCCC") "2A3B4C"))))
+
+(deftest encode-string-with-single-and-mixed-characters
+         (testing "encode string with single and mixed characters"
+                  (is (= (rle/run-length-encode "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB") "12WB12W3B24WB"))))
+
+(deftest encode-multiple-whitespace
+         (testing "encode string with whitespace characters mixed in it"
+                  (is (= (rle/run-length-encode "  hsqq qww  ") "2 hs2q q2w2 "))))
+
+(deftest encode-lowercase
+         (testing "encode string with lowercase characters"
+                  (is (= (rle/run-length-encode "aabbbcccc") "2a3b4c"))))
+
+(deftest decode-empty-string
+         (testing "decode empty string"
+                  (is (= (rle/run-length-decode "") ""))))
+
+(deftest decode-single-characters
+         (testing "decode string with single characters only"
+                  (is (= (rle/run-length-decode "XYZ") "XYZ"))))
+
+(deftest decode-no-single-characters
+         (testing "decode string with no single characters"
+                  (is (= (rle/run-length-decode "2A3B4C") "AABBBCCCC"))))
+
+(deftest decode-single-and-repeated-characters
+         (testing "decode string with single and repeated characters"
+                  (is (= (rle/run-length-decode "12WB12W3B24WB") "WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"))))
+
+(deftest decode-lowercase
+         (testing "decode string with lowercase characters"
+                  (is (= (rle/run-length-decode "2a3b4c") "aabbbcccc"))))
+
+(deftest decode-mixed-whitespace
+         (testing "decode string with mixed whitespace characters in it"
+                  (is (= (rle/run-length-decode "2 hs2q q2w2 ") "  hsqq qww  "))))
+
+(deftest consistency
+         (testing "Encode a string and then decode it. Should return the same one."
+                  (is (= (rle/run-length-decode (rle/run-length-encode "zzz ZZ  zZ")) "zzz ZZ  zZ"))))