解决 4clojure 难题时出现 NumberFormatException
NumberFormatException occurs while solving 4clojure puzzle
我正在尝试解决 4clojure 问题 "Product Digits"。问题描述为-
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])
这是我的解决方案 -
#(map (fn [x] (Integer/valueOf x)) (clojure.string/split (str (* %1 %2)) #""))
这在我的本地工作得很好。我在 lein repl 和 emacs cider 中都进行了测试。
但同样的解决方案在 4clojure 站点中抛出错误
java.lang.NumberFormatException: For input string: ""
他们使用不同的回复吗?还是我做错了什么?
这可能与 4clojure 中的旧版本 clojure 有关。
所以 clojure.string/split
留下一个空字符串作为人工制品。
该版本的 clojure 与当前版本存在一些差异(您可能 运行 在以后的任务中使用它们)
然而,你在这里甚至不需要 split
,因为映射在内部调用字符串 seq
,生成它的字符序列。所以你只需要这样做:
#(map (fn [x] (Integer/valueOf (str x))) (str (* %1 %2)))
我正在尝试解决 4clojure 问题 "Product Digits"。问题描述为-
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])
这是我的解决方案 -
#(map (fn [x] (Integer/valueOf x)) (clojure.string/split (str (* %1 %2)) #""))
这在我的本地工作得很好。我在 lein repl 和 emacs cider 中都进行了测试。
但同样的解决方案在 4clojure 站点中抛出错误
java.lang.NumberFormatException: For input string: ""
他们使用不同的回复吗?还是我做错了什么?
这可能与 4clojure 中的旧版本 clojure 有关。
所以 clojure.string/split
留下一个空字符串作为人工制品。
该版本的 clojure 与当前版本存在一些差异(您可能 运行 在以后的任务中使用它们)
然而,你在这里甚至不需要 split
,因为映射在内部调用字符串 seq
,生成它的字符序列。所以你只需要这样做:
#(map (fn [x] (Integer/valueOf (str x))) (str (* %1 %2)))