Skip to content

Commit

Permalink
Add unfinished solution
Browse files Browse the repository at this point in the history
  • Loading branch information
deniscostadsc committed Nov 20, 2024
1 parent c5080d0 commit cbc6616
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
51 changes: 51 additions & 0 deletions solutions/beecrowd/1077/1077.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
(ns main)

(defn operator-precedence [term]
(cond
(contains? ["+" "-"] term)
1
(contains? ["*" "/"] term)
2
(= term "^")
3
:else
0))

(defn operator? [term]
(contains? ["+" "-" "*" "/" "^"] term))

(defn opening-parenthesis? [term]
(= term "("))

(defn closing-parenthesis? [term]
(= term ")"))

(defn parenthesis? [term]
(or (opening-parenthesis? term) (closing-parenthesis? term)))

(defn operand? [term]
(and (not (operator? term)) (not (parenthesis? term))))

(defn infix-to-postfix [line]
(loop [[term & remaining-terms] line
terms-stack []
postfix-expression []]
(when (opening-parenthesis? term)
(recur remaining-terms
(conj terms-stack term)
postfix-expression))
(when (opening-parenthesis? term)
(loop ))
(when (operand? term)
(recur remaining-terms
terms-stack
(conj postfix-expression term)))
(when (operator? term))))

(defn main []
(loop [n (Integer/parseInt (read-line))]
(when (> n 0)
(println (infix-to-postfix (read-line)))
(recur (dec n)))))

(main)
1 change: 1 addition & 0 deletions solutions/beecrowd/1077/WRONG
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
clj

0 comments on commit cbc6616

Please sign in to comment.