practicalli / practicalli/clojure

kata - condensing characters

Open
#12 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

kata
Dominant language
Makefile
Stars
117
Forks
36
PR merge metrics
No merged PRs in 30d

Description

Convert
("WWWWW" "BB" "BBBB" "W")
into
("5W" "2B" "4B" "W")

A possibly overcomplicated way to do this would be

(defn foo [things]
  (mapcat
    (fn [[k vs]]
      (map
        #(str (if (= k 1) "" k) (first %))
        vs))
    (group-by count things)))

(foo '("WWWWW" "BB" "BBBB" "W" "AA" "BB" "CC"))
("5W" "2B" "2A" "2B" "2C" "4B" "W")

A simple way without a transducer (not as efficient)
(def data '("WWWWW" "BB" "BBBB" "W"))
(->> data (map (juxt count first)) (map #(apply str %)))
("5W" "2B" "4B" "1W")

(def data ["WW" "BB" "CC"])
(->> a (map (juxt count first)) (map #(apply str %)))
("2W" "2B" "2C")

Using a transducer

(into [] (comp (map (juxt count first)) (map #(apply str %))) data)
["2W" "2B" "2C"]
(def data '("WWWWW" "BB" "BBBB" "W"))
(into [] (comp (map (juxt count first)) (map #(apply str %))) data)
["5W" "2B" "4B" "1W"]

Needs a conditional to return just W instead of 1W - or use another map in the transducer version

(map (fn [[n c :as pair]] (if (= n 1) [c] pair)))

Putting that map function in the transducer version of the solution:

"W" for the single letter case so I'd need an extra little transformation in my suggestion

(into [] (comp (map (juxt count first)) 
                      (map (fn [[n c :as pair]] (if (= 1 n) [c] pair))) 
                      (map #(apply str %))) a)

["5W" "2B" "4B" "W"]

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file or test is named in the issue. Start from the input/output examples and locate the corresponding kata entry point in the repository; verify the transformation for repeated characters and the singleton case, where the count is omitted.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure
Domain
developer-experience
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.