I am trying to solve the 4clojure problem "Product Digits". The problem description is -
Write a function which multiplies two numbers and returns the result as a sequence of its digits.
(= (__ 1 1) [1])
(= (__ 99 9) [8 9 1])
(= (__ 999 99) [9 8 9 0 1])
Here is my solution -
#(map (fn [x] (Integer/valueOf x)) (clojure.string/split (str (* %1 %2)) #""))
This works perfectly fine in my local. I tested in both lein repl & emacs cider.
But the same solution throws an error in 4clojure site
java.lang.NumberFormatException: For input string: ""
Do they use a different repl? Or am I doing something wrong?
that is probably connected with the older version of clojure in 4clojure.
So clojure.string/split
leaves an empty string as an artefact.
There are some differences in that version of clojure with current ones (you would probably run into them in later tasks)
However, you don't even need split
here, because mapping internally calls seq
on string, making a char sequence of it. So you just need to do this:
#(map (fn [x] (Integer/valueOf (str x))) (str (* %1 %2)))