Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,29 @@
|
|||
#lang racket
|
||||
|
||||
(struct *straddling (header main original)
|
||||
#:reflection-name 'straddling
|
||||
#:methods gen:custom-write
|
||||
[(define (write-proc board port mode)
|
||||
(write-string "#<straddling " port)
|
||||
(write (*straddling-original board) port)
|
||||
(write-string ">" port))])
|
||||
|
||||
(define string->vector (compose list->vector string->list))
|
||||
|
||||
(define (straddling . lines)
|
||||
(define header-tail (reverse
|
||||
(for/fold ([rev-ret '()])
|
||||
([char (in-list (string->list (car lines)))]
|
||||
[i (in-naturals)])
|
||||
(if (equal? char #\space)
|
||||
(cons (number->string i) rev-ret)
|
||||
rev-ret))))
|
||||
(define main (list->vector
|
||||
(map string->vector
|
||||
(cons "0123456789"
|
||||
(map string-upcase
|
||||
lines)))))
|
||||
(define temporal-board
|
||||
(*straddling (list->vector (list* "?" "" header-tail)) main lines))
|
||||
(define escape (straddling-encode-char #\/ temporal-board))
|
||||
(*straddling (list->vector (list* escape "" header-tail)) main lines))
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
(define (straddling-encode-char char board)
|
||||
(or (for/or ([head (in-vector (*straddling-header board))]
|
||||
[line (in-vector (*straddling-main board))])
|
||||
(let ([pos (vector-member char line)])
|
||||
(if pos
|
||||
(string-append head (number->string pos))
|
||||
#f)))
|
||||
""))
|
||||
|
||||
(define (straddle message board)
|
||||
(apply string-append
|
||||
(map (lambda (char)
|
||||
(if (or (equal? char #\space) (equal? char #\/))
|
||||
""
|
||||
(straddling-encode-char char board)))
|
||||
(string->list (string-upcase message)))))
|
||||
|
||||
|
||||
(define (unstraddle message board)
|
||||
(define char->string string)
|
||||
(define (straddling-decode-char str row)
|
||||
(vector-ref (vector-ref (*straddling-main board) row) (string->number str)))
|
||||
(list->string
|
||||
(reverse
|
||||
(let-values ([(row rev-ret)
|
||||
(for/fold ([row #f] ;row to read in multichar codes
|
||||
;#f means start of new code
|
||||
[rev-ret '()]) ;result
|
||||
([str (map char->string (string->list (string-upcase message)))])
|
||||
(if (not row)
|
||||
(let ([pos (vector-member str (*straddling-header board))])
|
||||
(if pos
|
||||
(values pos rev-ret)
|
||||
(values #f (cons (straddling-decode-char str 1) rev-ret))))
|
||||
(let ([decoded (straddling-decode-char str row)])
|
||||
(if (equal? decoded #\/)
|
||||
(values 0 rev-ret)
|
||||
(values #f (cons decoded rev-ret))))))])
|
||||
(unless row ;check that last number was not missing
|
||||
rev-ret)))))
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
(define (test-straddling message board)
|
||||
(let* ([encoded (straddle message board)]
|
||||
[decoded (unstraddle encoded board)])
|
||||
(displayln board)
|
||||
(displayln message)
|
||||
(displayln encoded)
|
||||
(displayln decoded)))
|
||||
|
||||
(test-straddling "One night-it was on the twentieth of March, 1888-I was returning"
|
||||
(straddling "HOL MES RT"
|
||||
"ABCDFGIJKN"
|
||||
"PQUVWXYZ./"))
|
||||
(newline)
|
||||
(test-straddling "One night-it was on the twentieth of March, 1888-I was returning"
|
||||
(straddling "ET AON RIS"
|
||||
"BCDFGHJKLM"
|
||||
"PQ/UVWXYZ."))
|
||||
Loading…
Add table
Add a link
Reference in a new issue