<commit_before>(ns run-length-encoding
  (:require [clojure.string :as string :only join]))

;; Pensez à faire une version avec reduce
;; Changer les noms des variables c ==> current-char
;; Variabiliser (peek (peek acc)) dans un let
(defn plain-text-to-vector
  "transform the plain text in a vector of vector of characters"
  [plain-text]
  (loop [acc [] char-seq plain-text]
    (let [current-char (first char-seq) current-vector (peek acc)]
      (if (nil? current-char) acc
          (cond
            (or (empty? acc) (not (= current-char (peek current-vector)))) (recur (conj acc (vector current-char)) (rest char-seq))
            (= current-char (peek current-vector)) (recur (conj (vec (butlast acc)) (conj current-vector current-char)) (rest char-seq)))))))

(defn vector-to-cipher-text
  "transform a vector of vector of characters in the run-length encoded string"
  [char-seq]
  (->> char-seq
       (map #(vector (let [number-of-chars (count %)]
                       (if (= 1 number-of-chars)
                         "" (str number-of-chars))) (str (peek %))))
       (map string/join)
       (string/join)))

(defn run-length-encode
  "encodes a string with run-length-encoding"
  [plain-text]
  (->> plain-text
       (plain-text-to-vector)
       (vector-to-cipher-text)))

(defn run-length-decode
  "decodes a run-length-encoded string"
  [cipher-text])<commit_msg>Une première version fonctionnelle de l'exercice "Run-length encoding" de Exercism dans la track Clojure<commit_after>(ns run-length-encoding
  (:require [clojure.string :as string :only join]))

;; Pensez à faire une version avec reduce
(defn plain-text-to-vector
  "transforms the plain text in a vector of vector of characters"
  [plain-text]
  (loop [acc [] char-seq plain-text]
    (let [current-char (first char-seq) current-vector (peek acc)]
      (if (nil? current-char) acc
          (cond
            (or (empty? acc) (not (= current-char (peek current-vector)))) (recur (conj acc (vector current-char)) (rest char-seq))
            (= current-char (peek current-vector)) (recur (conj (vec (butlast acc)) (conj current-vector current-char)) (rest char-seq)))))))

(defn vector-to-cipher-text
  "transforms a vector of vector of characters in the run-length encoded string"
  [char-seq]
  (->> char-seq
       (map #(vector (let [number-of-chars (count %)]
                       (if (= 1 number-of-chars)
                         "" (str number-of-chars))) (str (peek %))))
       (map string/join)
       (string/join)))

(defn run-length-encode
  "encodes a string with run-length-encoding"
  [plain-text]
  (->> plain-text
       (plain-text-to-vector)
       (vector-to-cipher-text)))

(defn run-length-decode
  "decodes a run-length-encoded string"
  [cipher-text]
  (loop [acc "" char-seq cipher-text stack []]
    (let [current-char (first char-seq)]
      (cond
        (empty? char-seq) acc
        (java.lang.Character/isDigit current-char) (recur acc (rest char-seq) (vector (if (empty? stack) (java.lang.Character/digit current-char 10) (+ (java.lang.Character/digit current-char 10) (* 10 (last stack))))))
        :else (recur (str acc (if (empty? stack) (str current-char) (string/join (repeat (last stack) (str current-char))))) (rest char-seq) [])))))
{'fraction_non_alphanumeric': 0.15064298836497245, 'fraction_numerical': 0.002449479485609308, 'mean_word_length': 2.96, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3139497', 'n_tokens_mistral': 1076, 'n_tokens_neox': 1050, 'n_words': 317}