-
Notifications
You must be signed in to change notification settings - Fork 185
/
Copy pathsimplify-the-algebraic-expressions.rkt
242 lines (217 loc) · 7.48 KB
/
simplify-the-algebraic-expressions.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
;; https://www.hackerrank.com/contests/lambda-calculi-march-2016/challenges/simplify-the-algebraic-expressions
#lang racket
(require parser-tools/lex
(prefix-in re- parser-tools/lex-sre)
parser-tools/yacc)
(define-tokens a (NUM))
(define-empty-tokens b (LPAR RPAR x + - * / NEG NONE ^ EOF))
(define-lex-trans number
(syntax-rules ()
((_ digit) (re-+ digit))))
(define-lex-abbrevs
(digit10 (char-range "0" "9"))
(number10 (number digit10)))
(define alg-lexer
(lexer
("(" (token-LPAR))
(")" (token-RPAR))
("^" (token-^))
("*" (token-*))
("/" (token-/))
("-" (token--))
("+" (token-+))
("x" (token-x))
((re-+ number10) (token-NUM (string->number lexeme)))
(whitespace (alg-lexer input-port))
((eof) (token-EOF))))
(define-struct paren-exp (e))
(define-struct arith-exp (op e1 e2))
(define-struct neg-exp (e))
(define-struct x-exp (rank value))
(define alg-parser
(parser
(start exp)
(end EOF)
(error void)
(tokens a b)
(precs
(left NONE)
(left x NUM LPAR RPAR)
(left + -)
(left * /)
(left NEG)
(right ^)
)
(grammar
(exp
((NUM) (make-x-exp 0 $1))
((x) (make-x-exp 1 1))
((exp + exp) (make-arith-exp 'ADD $1 $3))
((exp - exp) (make-arith-exp 'SUB $1 $3))
((exp * exp) (make-arith-exp 'MUL $1 $3))
((exp / exp) (make-arith-exp 'DIV $1 $3))
((+ exp) $2)
((- exp) (prec NEG) (make-neg-exp $2))
((exp ^ exp) (make-arith-exp 'EXP $1 $3))
((LPAR exp RPAR) (prec LPAR) $2)))))
(define (neg xs)
(map (lambda (x)
(make-x-exp (x-exp-rank x)
(- (x-exp-value x))))
xs))
(define (add xs ys)
(match (cons xs ys)
((cons (list) (list)) (list))
((cons (list) _) ys)
((cons _ (list)) xs)
((cons (cons x xs2) (cons y ys2))
(cond ((> (x-exp-rank x) (x-exp-rank y))
(cons x (add xs2 ys)))
((< (x-exp-rank x) (x-exp-rank y))
(cons y (add xs ys2)))
(else
(cons (make-x-exp (x-exp-rank x)
(+ (x-exp-value x)
(x-exp-value y)))
(add xs2 ys2)))))))
(define (sub xs ys)
(match (cons xs ys)
((cons (list) (list)) (list))
((cons (list) ys) (map (lambda (y) (make-x-exp (x-exp-rank y)
(- (x-exp-value y))))
ys))
((cons xs (list)) xs)
((cons (cons x xs2) (cons y ys2))
(cond ((> (x-exp-rank x) (x-exp-rank y))
(cons x (sub xs2 ys)))
((< (x-exp-rank x) (x-exp-rank y))
(cons (make-x-exp (x-exp-rank y)
(- (x-exp-value y)))
(sub xs ys2)))
(else
(cons (make-x-exp (x-exp-rank x)
(- (x-exp-value x)
(x-exp-value y)))
(sub xs2 ys2)))))))
(define (merge xs)
(match xs
((list) (list))
((list a) xs)
((list a b)
(if (= (x-exp-rank a)
(x-exp-rank b))
(list (make-x-exp (x-exp-rank a)
(+ (x-exp-value a)
(x-exp-value b))))
xs))
((list-rest a b c)
(if (= (x-exp-rank a)
(x-exp-rank b))
(merge (cons (make-x-exp (x-exp-rank a)
(+ (x-exp-value a)
(x-exp-value b)))
c))
(cons a (merge (cons b c)))))))
(define (simplify xs)
(let ((sorted (sort xs #:key x-exp-rank >)))
(merge sorted)))
(define (mul xs ys)
(simplify (apply append (map (lambda (x)
(map (lambda (y)
(make-x-exp (+ (x-exp-rank x)
(x-exp-rank y))
(* (x-exp-value x)
(x-exp-value y))))
ys))
xs))))
(define (div xs ys)
(map (lambda (x)
(make-x-exp (x-exp-rank x)
(/ (x-exp-value x)
(x-exp-value (car ys)))))
xs))
(define (pow-iter i x acc)
(if (= i 0)
acc
(pow-iter (- i 1) x (mul x acc))))
(define (pow xs ys)
(let ((k (x-exp-value (car ys))))
(pow-iter (- k 1) xs xs)))
(define (eval parsed-exp)
(match parsed-exp
((neg-exp e) (neg (eval e)))
((arith-exp op e1 e2)
(case op
('ADD (add (eval e1) (eval e2)))
('SUB (sub (eval e1) (eval e2)))
('MUL (mul (eval e1) (eval e2)))
('DIV (div (eval e1) (eval e2)))
('EXP (pow (eval e1) (eval e2)))))
((paren-exp e) (eval e))
((x-exp rank value) (list (x-exp rank value)))))
(define (print-exp-inner es)
(if (null? es)
(list)
(let* ((rank (x-exp-rank (car es)))
(value (x-exp-value (car es)))
(s (cond ((= value 0) "")
((and (= rank 0) (< value 0)) (format " - ~a" (abs value)))
((= rank 0) (format " + ~a" value))
((and (= rank 1) (= value -1)) (format " - x"))
((and (= rank 1) (= value 1)) (format " + x"))
((and (= rank 1) (< value 0)) (format " - ~ax" (abs value)))
((= rank 1) (format " + ~ax" value))
((= value -1) (format " - x^~a" rank))
((= value 1) (format " + x^~a" rank))
((< value 0) (format " - ~ax^~a" (abs value) rank))
(else (format " + ~ax^~a" value rank)))))
(cons s (print-exp-inner (cdr es))))))
(define (print-exp es)
(if (null? es)
(list "0")
(let* ((rank (x-exp-rank (car es)))
(value (x-exp-value (car es)))
(s (cond ((= value 0) (if (null? (cdr es)) "0" ""))
((and (= rank 0) (< value 0)) (format "-~a" (abs value)))
((= rank 0) (format "~a" value))
((and (= rank 1) (= value -1)) (format "-x"))
((and (= rank 1) (= value 1)) (format "x"))
((and (= rank 1) (< value 0)) (format "-~ax" (abs value)))
((= rank 1) (format "~ax" value))
((= value -1) (format "-x^~a" rank))
((= value 1) (format "x^~a" rank))
((< value 0) (format "-~ax^~a" (abs value) rank))
(else (format "~ax^~a" value rank)))))
(cons s (print-exp-inner (cdr es))))))
(define (clean es)
(filter (lambda (x) (not (zero? (x-exp-value x)))) es))
(define (pr es)
(displayln (string-join (print-exp (clean es)) "")))
(define (lex-this lexer input) (lambda () (lexer input)))
(define (eval-string str)
;;(displayln str)
(let ((input (open-input-string str)))
(pr (eval (alg-parser (lex-this alg-lexer input))))))
(define (loop i)
(when (> i 0)
(eval-string
;; Ugly hack to avoid implicit multiplications which were
;; causing problems with operator precedence
(regexp-replace*
#px"[)]x"
(regexp-replace*
#px"[)]([\\d]+)"
(regexp-replace*
#px"[)][(]"
(regexp-replace*
#px"x[(]"
(regexp-replace*
#px"([\\d]+)[(]"
(regexp-replace* #px"([\\d]+)x" (read-line) "\\1*x")
"\\1*(")
"x*(")
")*(")
")*\\1")
")*x"))
(loop (- i 1))))
(loop (string->number (read-line)))