Skip to content

Instantly share code, notes, and snippets.

@ray1729
Created May 15, 2014 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ray1729/68f4feb328c4d68b7b1e to your computer and use it in GitHub Desktop.
Save ray1729/68f4feb328c4d68b7b1e to your computer and use it in GitHub Desktop.
Explode string containing ambiguous characters
(ns playground.explode
(:require [clojure.math.combinatorics :refer [cartesian-product]]
[clojure.string :as str]))
(def ambiguous-chars [#{\O \0} #{\I \L \1}])
(defn ambiguate
[c]
(or (first (filter #(contains? % c) ambiguous-chars))
[c]))
(defn explode
[s]
(map str/join (apply cartesian-product (map ambiguate s))))
(explode "PHONIC")
;;=> ("PHONIC" "PHONLC" "PHON1C" "PH0NIC" "PH0NLC" "PH0N1C")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment