DEV Community

Discussion on: Daily Challenge #232 - Regex Pattern

Collapse
 
quoll profile image
Paula Gearon

Assuming the above is OK, then here's my Clojure solution:

(require '[clojure.string :as s])
(defn permute [a]
  (or (seq (mapcat #(map (partial cons %) (permute (remove #{%} a))) a)) [[]]))

(defn regex-contains-all [input]
  (str "(" (s/join ")|(" (map (partial s/join ".*") (permute input))) ")"))

Apologies for writing code that looks like line noise.

Given an input of "abc" then it will generate:

(regex-contains-all "abc")
"(a.*b.*c)|(a.*c.*b)|(b.*a.*c)|(b.*c.*a)|(c.*a.*b)|(c.*b.*a)"

This gives the test results that I describe in the previous comment.