From 15405a9e8724257b08c17b69c56ede714d0652c4 Mon Sep 17 00:00:00 2001 From: Denis Costa Date: Sat, 28 Sep 2024 11:51:42 -0300 Subject: [PATCH] Solve TDA Rational in clojure --- solutions/beecrowd/1022/1022.clj | 54 ++++++++++---------------------- solutions/beecrowd/1022/WRONG | 0 2 files changed, 17 insertions(+), 37 deletions(-) delete mode 100644 solutions/beecrowd/1022/WRONG diff --git a/solutions/beecrowd/1022/1022.clj b/solutions/beecrowd/1022/1022.clj index a407c7a4..0e9a33b8 100644 --- a/solutions/beecrowd/1022/1022.clj +++ b/solutions/beecrowd/1022/1022.clj @@ -1,61 +1,41 @@ (ns main (:require [clojure.string :as str])) -(defn debug [x] - (println x) - x) -(defn gimme-type [x] - (println (type x)) - x) - (defn gcd [a b] - (println (type a) " - " (type b)) (loop [a' a b' b] - (println (type a') " - " (type b')) (if (= 0 b') a' - (recur 'b (mod a' b'))))) + (recur b' (mod a' b'))))) (defn process-input [line] (let [[a' _ b' o c' _ d'] (str/split line #" ") [a b c d] (map #(Integer/parseInt %) [a' b' c' d'])] (cond (= o "+") - (-> (* b d) - (* a) - (/ b) - (+ (* b d)) - (* c) - (/ d) - (gcd (* b d))) + (let [n (+ (* a d) (* c b)) + d (* b d)] + [n d]) (= o "-") - (-> (* b d) - (* a) - (/ b) - (- (* b d)) - (* c) - (/ d) - (gcd (* b d))) + (let [n (- (* a d) (* c b)) + d (* b d)] + [n d]) (= o "*") - (-> a - (* c) - (gcd (* b d))) + (let [n (* a c) + d (* b d)] + [n d]) :else - (-> a - (debug) - (gimme-type) - (* d) - (debug) - (gimme-type) - (gcd (* b c)))))) + (let [n (* a d) + d (* b c)] + [n d])))) (defn main [] (let [c (-> (read-line) (Integer/parseInt))] (loop [c' c] - (process-input (read-line)) - (when c' - (recur (- c' 1)))))) + (when (> c' 0) + (let [[n d] (process-input (read-line))] + (println (format "%d/%d = %d/%d" n d (/ n (gcd n d)) (/ d (gcd n d)))) + (recur (- c' 1))))))) (main) diff --git a/solutions/beecrowd/1022/WRONG b/solutions/beecrowd/1022/WRONG deleted file mode 100644 index e69de29b..00000000