删除clojure中多项式项的o系数及其幂和变量

删除clojure中多项式项的o系数及其幂和变量,clojure,Clojure,我创建了一个多项式函数,该函数返回相加的多项式项的基于字符串的表示形式,但是我很难从字符串中删除包含0系数的项,并且它给了我不同的输出 以下是我的职责: (defn format-poly [poly] (clojure.string/join "" (map-indexed (fn [index item] (cond (= index 0) item

我创建了一个多项式函数,该函数返回相加的多项式项的基于字符串的表示形式,但是我很难从字符串中删除包含0系数的项,并且它给了我不同的输出

以下是我的职责:

(defn format-poly [poly]
    (clojure.string/join ""
    (map-indexed (fn [index item] (cond 
                                   (= index 0) item
                                   (= (get item 0) \-) item
                                   :default (str "+" item)))
    (reverse (for [[pow coeff] (map-indexed vector (rseq (:coefficients poly))) ; remove whatever is causing a negative coeff to be ignored
       ]
    (cond 
       (= coeff 0) (remove zero? [pow coeff])
        (= coeff 1) (format "%s^%d",(:variable poly) pow)
        (= pow 0) (format "%s",coeff)
        :else
    (format "%d%s^%d" coeff (:variable poly) pow)))))))
样本输入:

(format-poly {:variable "x"
              :coefficients [1 0 0 2 3 4]}) 
预期产出:
“x^6-2x^5+3x^4-4x^3+5x^2-6x^1+7”

我建议将所有polynom打印部分全部收集到一个平面集合中,然后将它们全部打印到字符串中。可能是这样的:

(defn format-poly [v coeffs]
  (let [fmt-parts (mapcat (fn [coeff pow]
                            (when-not (zero? coeff)
                              [(if (neg? coeff) "-" "+")
                               (let [coeff (Math/abs coeff)]
                                 (if (== 1 coeff) "" coeff))
                               (cond (zero? pow) ""
                                     (== 1 pow) v
                                     :else (str v "^" pow))]))
                          coeffs
                          (range (dec (count coeffs)) -1 -1))]
    (apply str (if (= "+" (first fmt-parts))
                 (rest fmt-parts)
                 fmt-parts))))

user> (format-poly "x" [-1 -2 3 0 4 5])
;; "-x^5-2x^4+3x^3+4x+5"

user> (format-poly "x" [1 -2 3 0 4 5])
;; "x^5-2x^4+3x^3+4x+5"

user> (format-poly "x" [1 2 3 0 4 5])
;; "x^5+2x^4+3x^3+4x+5"

我建议将所有polynom打印部件全部收集到一个平面集合中,然后将它们全部打印到字符串中。可能是这样的:

(defn format-poly [v coeffs]
  (let [fmt-parts (mapcat (fn [coeff pow]
                            (when-not (zero? coeff)
                              [(if (neg? coeff) "-" "+")
                               (let [coeff (Math/abs coeff)]
                                 (if (== 1 coeff) "" coeff))
                               (cond (zero? pow) ""
                                     (== 1 pow) v
                                     :else (str v "^" pow))]))
                          coeffs
                          (range (dec (count coeffs)) -1 -1))]
    (apply str (if (= "+" (first fmt-parts))
                 (rest fmt-parts)
                 fmt-parts))))

user> (format-poly "x" [-1 -2 3 0 4 5])
;; "-x^5-2x^4+3x^3+4x+5"

user> (format-poly "x" [1 -2 3 0 4 5])
;; "x^5-2x^4+3x^3+4x+5"

user> (format-poly "x" [1 2 3 0 4 5])
;; "x^5+2x^4+3x^3+4x+5"

我不知道如何从示例输入中获得所显示的预期输出。在我看来,你应该期望“x^5+2x^2+3x+4”???我不知道如何从示例输入中获得所显示的预期输出。在我看来,你应该期望“x^5+2x^2+3x+4”???