You are viewing a single comment's thread. Return to all comments →
Clojure solution:
(require '[clojure.string :as str]) (defn split-to-double [input] (->> (str/split input #"\s") (map #(Double/parseDouble %)) (into []) ) ) (defn distance [p1 p2] (Math/sqrt (+ (Math/pow (- (first p1) (first p2)) 2) (Math/pow (- (second p1) (second p2)) 2))) ) (defn get-points-vector [number-of-points] (->> #(split-to-double (read-line)) (repeatedly number-of-points) (into []) ) ) (defn calc-perimeter [acc points-vector] (if (>= (count points-vector) 2) (recur (+ acc (distance (first points-vector) (second points-vector))) (rest points-vector)) acc ) ) (let [number-of-points (Integer/parseInt (read-line)) points-vector (get-points-vector number-of-points) ] (println (calc-perimeter 0 (conj points-vector (first points-vector)))) )
Seems like cookies are disabled on this browser, please enable them to open this website
Compute the Perimeter of a Polygon
You are viewing a single comment's thread. Return to all comments →
Clojure solution: