Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
34
Task/Align-columns/Racket/align-columns.rkt
Normal file
34
Task/Align-columns/Racket/align-columns.rkt
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#lang racket
|
||||
|
||||
(define (display-aligned text #:justify [justify 'left])
|
||||
(define lines
|
||||
(for/list ([line (regexp-split #rx"\n" text)])
|
||||
(regexp-split #rx"\\$" line)))
|
||||
(define width
|
||||
(add1 (for*/fold ([m 0]) ([line lines] [word line])
|
||||
(max m (string-length word)))))
|
||||
(define spaces (make-string width #\space))
|
||||
(for ([line lines])
|
||||
(for* ([word line]
|
||||
[strs (let ([spc (substring spaces (string-length word))])
|
||||
(case justify
|
||||
[(left) (list word spc)]
|
||||
[(right) (list spc word)]
|
||||
[(center) (let ([i (quotient (string-length spc) 2)])
|
||||
(list (substring spc i)
|
||||
word
|
||||
(substring spc 0 i)))]))])
|
||||
(display strs))
|
||||
(newline)))
|
||||
|
||||
(define text
|
||||
"Given$a$text$file$of$many$lines,$where$fields$within$a$line$
|
||||
are$delineated$by$a$single$'dollar'$character,$write$a$program
|
||||
that$aligns$each$column$of$fields$by$ensuring$that$words$in$each$
|
||||
column$are$separated$by$at$least$one$space.
|
||||
Further,$allow$for$each$word$in$a$column$to$be$either$left$
|
||||
justified,$right$justified,$or$center$justified$within$its$column.")
|
||||
|
||||
(display-aligned text)
|
||||
(display-aligned #:justify 'right text)
|
||||
(display-aligned #:justify 'center text)
|
||||
Loading…
Add table
Add a link
Reference in a new issue