Skip to content

Instantly share code, notes, and snippets.

@ericnormand
Created February 13, 2021 14:20
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 ericnormand/9cc342aa0f250c18903c6033a335a76e to your computer and use it in GitHub Desktop.
Save ericnormand/9cc342aa0f250c18903c6033a335a76e to your computer and use it in GitHub Desktop.
414 PurelyFunctional.tv Newsletter

Decimal to binary

Write a function that takes a string and replaces all occurrences of numbers with their decimal form.

Examples

(d->b "I have 2 arms.") ;=> "I have 10 arms."
(d->b "That costs 3 dollars. But I only have 1 dollar.") ;=> "That costs 11 dollars. But I only have 1 dollar."
(d->b "I was born in 1981.") ;=> "I was born in 11110111101."

Any contiguous string of digits is a number. That means that spaces, periods, and commas separate numbers instead of indicating differnt parts. For instance, 2,000 is the number 2 followed by the number 000.

Thanks to this site for the challenge idea where it is considered Expert in JavaScript. The problem has been modified from the original.

Please submit your solutions as comments on this gist.

@dfornika
Copy link

dfornika commented Feb 15, 2021

(defn try-parse-int [s]
  (try (Integer/parseInt s)
    (catch Exception e s)))

(defn int-to-binary [i]
  (if (number? i)
    (Integer/toBinaryString i)
    i))

(defn d->b [s]
  (let [split-s (re-seq #"[^\d]+|\d+" s)]
    (->> split-s
         (map try-parse-int)
         (map int-to-binary)
         (str/join ""))))

@segudev
Copy link

segudev commented Feb 19, 2021

(defn d->b [string]
  (let [regex #"\d+"
        converter #(Integer/toBinaryString (Integer/parseInt %))
        binary-digits (map converter (re-seq regex string))
        split-string (str/split string regex)]
    (str/join (cons (first split-string)
              (interleave binary-digits (rest split-string))))))

@prairie-guy
Copy link

prairie-guy commented Apr 5, 2021

(defn d->b [s]                                                                                                                                        
  (string/replace s #"[0-9]+" #(Integer/toString (read-string %1) 2)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment