http://qiita.com/Nabetani/items/9d80de41903775296ca6 第2回 オフラインリアルタイムどう書くの参考問題
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; -*- coding:utf-8 -*- | |
(use srfi-60) ; integer->list | |
;; "3:5b8" -> ((#f #t #f) (#t #t #f) (#t #t #t)) | |
(define (parse input) | |
(rxmatch-let (#/^(\d+):([\da-fA-F]+)/ input) (_ ssize data) | |
(let1 size (x->integer ssize) | |
($ (cut take <> size) $ (cut slices <> size) | |
$ integer->list (string->number data 16) (* 4 (string-length data)))))) | |
;; #f #t #f #t #t #f | |
;; #t #t #f -> #t #t #t | |
;; #t #t #t #t #f #f | |
(define (rotate liss) (apply map list (reverse liss))) | |
;; ((#t #t #f) (#t #t #t) (#t #f #f)) -> "3:de0" | |
(define (unparse liss) | |
(let* ([size (length liss)] | |
[lis (apply append liss)] | |
[ndigits (ceiling (/ (length lis) 4))]) | |
(format "~d:~v,'0x" size ndigits | |
(ash (list->integer lis) (- (* ndigits 4) (length lis)))))) | |
(define doit ($ unparse $ rotate $ parse $)) | |
;; Test | |
(define *testdata* " | |
3:5b8 → 3:de0 | |
1:8 → 1:8 | |
2:8 → 2:4 | |
2:4 → 2:1 | |
2:1 → 2:2 | |
3:5d0 → 3:5d0 | |
4:1234 → 4:0865 | |
5:22a2a20 → 5:22a2a20 | |
5:1234567 → 5:25b0540 | |
6:123456789 → 6:09cc196a6 | |
7:123456789abcd → 7:f1a206734b258 | |
7:fffffffffffff → 7:ffffffffffff8 | |
7:fdfbf7efdfbf0 → 7:ffffffffffc00 | |
8:123456789abcdef1 → 8:f0ccaaff78665580 | |
9:112233445566778899aab → 9:b23da9011d22daf005d40") | |
(use gauche.test) | |
(test-start "rotate") | |
(dolist [x (string-split *testdata* "\n")] | |
(rxmatch-case x | |
[#/\s*([\w:]+)\s*→\s*([\w:]+)\s*/ (_ in out) (test* x out (doit in))] | |
[else #f])) | |
(test-end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment