Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
18
Task/Gray-code/Racket/gray-code.rkt
Normal file
18
Task/Gray-code/Racket/gray-code.rkt
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#lang racket
|
||||
|
||||
(define (gray-encode n) (bitwise-xor n (arithmetic-shift n -1)))
|
||||
|
||||
(define (gray-decode n)
|
||||
(letrec ([loop (lambda(g bits)
|
||||
(if (> bits 0)
|
||||
(loop (bitwise-xor g bits) (arithmetic-shift bits -1))
|
||||
g))])
|
||||
(loop 0 n)))
|
||||
|
||||
(define (to-bin n) (format "~b" n))
|
||||
(define (show-table)
|
||||
(for ([i (in-range 1 32)])
|
||||
(printf "~a | ~a | ~a ~n"
|
||||
(~r i #:min-width 2 #:pad-string "0")
|
||||
(~a (to-bin(gray-encode i)) #:width 5 #:align 'right #:pad-string "0")
|
||||
(~a (to-bin (gray-decode(gray-encode i))) #:width 5 #:align 'right #:pad-string "0"))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue