Skip to content

Commit

Permalink
Refactor problem
Browse files Browse the repository at this point in the history
  • Loading branch information
deniscostadsc committed Sep 28, 2024
1 parent 15405a9 commit 4ae0c3d
Showing 1 changed file with 23 additions and 26 deletions.
49 changes: 23 additions & 26 deletions solutions/beecrowd/1022/1022.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,31 @@
a'
(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 "+")
(let [n (+ (* a d) (* c b))
d (* b d)]
[n d])
(= o "-")
(let [n (- (* a d) (* c b))
d (* b d)]
[n d])
(= o "*")
(let [n (* a c)
d (* b d)]
[n d])
:else
(let [n (* a d)
d (* b c)]
[n d]))))
(defn solve-expression [num-1 num-2 operator den-1 den-2]
(cond
(= operator "+")
[(+ (* num-1 den-2) (* den-1 num-2))
(* num-2 den-2)]
(= operator "-")
[(- (* num-1 den-2) (* den-1 num-2))
(* num-2 den-2)]
(= operator "*")
[(* num-1 den-1)
(* num-2 den-2)]
:else
[(* num-1 den-2)
(* num-2 den-1)]))

(defn main []
(let [c (-> (read-line)
(let [n (-> (read-line)
(Integer/parseInt))]
(loop [c' c]
(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)))))))
(loop [n' n]
(when (> n' 0)
(let [[num-1 _ num-2 operator den-1 _ den-2] (str/split (read-line) #" ")
[num-1' num-2' den-1' den-2'] (map #(Integer/parseInt %) [num-1 num-2 den-1 den-2])
[num den] (solve-expression num-1' num-2' operator den-1' den-2')
gcd (gcd num den)]
(println (format "%d/%d = %d/%d" num den (/ num gcd) (/ den gcd)))
(recur (- n' 1)))))))

(main)

0 comments on commit 4ae0c3d

Please sign in to comment.