-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab_18_200327.scm
459 lines (438 loc) · 9.62 KB
/
lab_18_200327.scm
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
#lang scheme
; Lab_18
; by Melnikov Sergei, MEH-190102
; task 1
(define (qsort lst [partial-order-relation <=])
; linear iteration, that divides list to three sublists according to given partial order relation: LOwer, EQual, HIgher than/to chosen element
(define (iter s lst lo eq hi partial-order-relation)
(if (empty? lst) (list lo eq hi)
(cond
[ (equal? s (car lst)) (iter s (cdr lst) lo (cons (car lst) eq) hi ) ]
[ (partial-order-relation (car lst) s) (iter s (cdr lst) (cons (car lst) lo ) eq hi ) ]
[ else (iter s (cdr lst) lo eq (cons (car lst) hi))]
)
)
)
(if (empty? lst) null ; do not handle null lists, bc they cannot be car'ed
(let ((d (iter (car lst) (cdr lst) null (list (car lst)) null partial-order-relation ) )) ; run our linear list division subfunc, using first element as division sample
(append
(qsort (car d) partial-order-relation)
(cadr d)
(qsort (caddr d) partial-order-relation)
)
)
)
)
; task 2
(define (inssetion-sort-by-nums-sum lst)
(define (nums-sum-iter n t) ; subfunc task is obvious
(if (= 0 n) t
(nums-sum-iter (quotient n 10) (+ t (remainder n 10)))
)
)
(define (insert n lst)
(if (empty? lst) (list n)
(let ((sn (nums-sum-iter n 0)))
(if (>= sn (nums-sum-iter (car lst) 0)) (cons n lst)
(cons (car lst) (insert n (cdr lst)))
)
)
)
)
(if (empty? lst) null
(insert (car lst) (sort (cdr lst)))
)
)
; task 3
(define (process-ints)
; filenames definition
(define input_file "in.txt")
(define output_file "out.txt")
; opening files IO flows/ports
(define in (open-input-file input_file) )
(define out (open-output-file output_file #:exists 'replace))
(define (iter state n sum quantity)
(define new_char (read-char in))
(if (equal? new_char eof)
(begin
(if (= state 1) (writeln n out) null)
(writeln sum out)
(writeln quantity out)
(close-input-port in)
(close-output-port out)
)
(let ((t (char->integer new_char)))
(cond
[ (= state 0) ; state 0: previously we've read nothing that could be part of integer
(cond
[ (and (<= 48 t) (<= t 57) ) (iter 1 (- t 48) sum quantity) ] ; to state 1 with non-negaitive n
[ (equal? new_char #\-) (iter 2 0 sum quantity)]
[ else (iter 0 0 sum quantity)] ; otherwise do nothing
)
]
[ (= state 1) ; state 1: previously we've read number character
(if (and (<= 48 t) (<= t 57) ) ; let's assume, that integers in file we're dealing with is not contain non-significant zeros, otherwise, negative numbers with insignigicant zeros will be interpreted as positive ones
(iter 1 ( (if (>= n 0) + -) (* n 10) t -48 ) sum quantity )
(begin
(writeln n out)
(iter (if (equal? new_char #\-) 2 0) 0 (+ sum n) (+ quantity 1))
)
)
]
[ (= state 2) ; state 2: previously we've read dash character
(cond
[ (and (<= 48 t) (<= t 57) ) (iter 1 (- 48 t) sum quantity) ] ; to state 1 with negative n
[ (equal? new_char #\-) (iter 2 0 sum quantity)]
[else (iter 0 0 sum quantity)] ; otherwise do nothing
)
]
)
)
)
)
(iter 0 0 0 0)
)
; task 4
(define (remove-hyphenations)
; filenames definition
(define input_file "in.txt")
(define output_file "out.txt")
; opening files IO flows/ports
(define in (open-input-file input_file) )
(define out (open-output-file output_file #:exists 'replace))
(define (iter state)
(define new_char (read-char in))
(cond
[ (= state 0) ; state 0: previous symbol belongs to set PLAINTEXT \ { #\space #\return #\newline #\- }
(cond
[ (equal? new_char eof)
(begin
(close-input-port in)
(close-output-port out)
)
]
[ (equal? new_char #\space)
(begin
(display new_char out)
(iter 1)
)
]
[ (equal? new_char #\return)
(begin
(display new_char out)
(iter 2)
)
]
[ (equal? new_char #\newline)
(begin
(display new_char out)
(iter 3)
)
]
[ (equal? new_char #\-) (iter 5) ]
[ else
(begin
(display new_char out)
(iter 0)
)
]
)]
[ (= state 1) ; state 1: previous symbol was space
(cond
[ (equal? new_char eof)
(begin
(close-input-port in)
(close-output-port out)
)
]
[ (equal? new_char #\space)
(begin
(display new_char out)
(iter 1)
)
]
[ (equal? new_char #\return)
(begin
(display new_char out)
(iter 2)
)
]
[ (equal? new_char #\newline)
(begin
(display new_char out)
(iter 3)
)
]
[ (equal? new_char #\-)
(begin
(display new_char out)
(iter 4)
)
]
[ else
(begin
(display new_char out)
(iter 0)
)
]
)]
[ (= state 2) ; state 2: previous symbol was return
(cond
[ (equal? new_char eof)
(begin
(close-input-port in)
(close-output-port out)
)
]
[ (equal? new_char #\space)
(begin
(display new_char out)
(iter 1)
)
]
[ (equal? new_char #\return)
(begin
(display new_char out)
(iter 2)
)
]
[ (equal? new_char #\newline)
(begin
(display new_char out)
(iter 3)
)
]
[ (equal? new_char #\-)
(begin
(display new_char out)
(iter 4)
)
]
[ else
(begin
(display new_char out)
(iter 0)
)
]
)]
[ (= state 3) ; state 3: previous symbols was newline
(cond
[ (equal? new_char eof)
(begin
(close-input-port in)
(close-output-port out)
)
]
[ (equal? new_char #\space)
(begin
(display new_char out)
(iter 1)
)
]
[ (equal? new_char #\return)
(begin
(display new_char out)
(iter 2)
)
]
[ (equal? new_char #\newline)
(begin
(display new_char out)
(iter 3)
)
]
[ (equal? new_char #\-)
(begin
(display new_char out)
(iter 4)
)
]
[ else
(begin
(display new_char out)
(iter 0)
)
]
)]
[ (= state 4) ; state 4: previous symbols sequence " -" seems like dash
(cond
[ (equal? new_char eof)
(begin
(close-input-port in)
(close-output-port out)
)
]
[ (equal? new_char #\space)
(begin
(display new_char out)
(iter 1)
)
]
[ (equal? new_char #\return)
(begin
(display new_char out)
(iter 2)
)
]
[ (equal? new_char #\newline)
(begin
(display new_char out)
(iter 3)
)
]
[ (equal? new_char #\-)
(begin
(display new_char out)
(iter 4)
)
]
[ else
(begin
(display new_char out)
(iter 0)
)
]
)]
[ (= state 5) ; state 5: previous symbols sequence "-" seems like hyphen
(cond
[ (equal? new_char eof)
(begin
(display #\- out)
(close-input-port in)
(close-output-port out)
)
]
[ (equal? new_char #\space)
(begin
(display #\- out)
(display new_char out)
(iter 1)
)
]
[ (equal? new_char #\return) (iter 6) ]
[ (equal? new_char #\newline) (iter 6) ]
[ (equal? new_char #\-)
(begin
(display new_char out)
(iter 4)
)
]
[ else
(begin
(display #\- out)
(display new_char out)
(iter 0)
)
]
)]
[ (= state 6) ; state 6: previous symbol is a hyphenation sign, just ignoring one newline symbols sequence, and then moving to state 7
(cond
[ (equal? new_char eof)
(begin
(display #\- out)
(close-input-port in)
(close-output-port out)
)
]
[ (equal? new_char #\space)
(begin
(display #\return out)
(display #\newline out)
(iter 1)
)
]
[ (equal? new_char #\return)
(begin
(iter 6)
)
]
[ (equal? new_char #\newline)
(begin
(iter 7)
)
]
[ (equal? new_char #\-)
(begin
(iter 5)
)
]
[ else
(begin
(display new_char out)
(iter 6)
)
]
)]
[ (= state 7) ; state 7: previous symbol is a part of hyphenated word's part
(cond
[ (equal? new_char eof)
(begin
(close-input-port in)
(close-output-port out)
)
]
[ (equal? new_char #\space)
(begin
(display #\return out)
(display #\newline out)
(iter 1)
)
]
[ (equal? new_char #\return)
(begin
(display new_char out)
(iter 2)
)
]
[ (equal? new_char #\newline)
(begin
(display new_char out)
(iter 3)
)
]
[ (equal? new_char #\-)
(begin
(iter 5)
)
]
[ else
(begin
(display new_char out)
(iter 7)
)
]
)]
)
)
(iter 0)
)
; task 5
(define (find&replace-translate)
; filenames definition
(define input_file "in.txt")
(define dictionary_file "dict.txt")
(define output_file "out.txt")
; opening files IO flows/ports
(define in (open-input-file input_file) )
(define out (open-output-file output_file #:exists 'replace))
; even if this method rereads dictionary file as many times as we have words in input file, this solution does not load whole dictionary file into RAM so can be executed even if free RAM is not enough
(define (find sub dictf)
(define word (read dictf))
(cond
[ (equal? word eof) (begin (close-input-port dictf) sub) ]
[ (equal? word sub) (read dictf) ]
[ else (find sub dictf) ]
)
)
(define (iter)
(define sub (read in))
(if (equal? sub eof) (close-output-port out)
(begin
(display (find sub (open-input-file dictionary_file)) out)
(display " " out)
(iter)
)
)
)
(iter)
)