Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (define (blank? x)
- (eq? x #\.))
- (define (xcar x)
- (if (cons? x)
- (car x)
- x))
- (define (xcdr x)
- (if (cons? x)
- (cdr x)
- x))
- (define (contiguous-count x top)
- (local [(define (loop l r n seen)
- (cond ((null? l)
- n)
- ((blank? (car l))
- (if (blank? (xcar r))
- (loop (cdr l)
- null
- (+ n 1 (contiguous-count (cdr x) #f))
- #t)
- (loop (cdr l) (xcdr r) (+ n 1) #t)))
- ((and seen top (null? (cdr x)))
- (loop (cdr l) (xcdr r) 0 #f))
- (else
- (loop (cdr l) (xcdr r) n seen))))]
- (loop (car x) (xcar (cdr x)) 0 #f)))
- (define (contiguous? x (num 5))
- (>= (contiguous-count x #t) num))
- (check-expect (contiguous? '((#\. #\. #\. #\. #\.)))
- #t)
- (check-expect (contiguous? '((#\. #\T #\. #\.) (#\. #\. #\. #\T) (#\T #\T #\T #\T)))
- #t)
- (check-expect (contiguous? '((#\T #\T #\. #\.) (#\T #\T #\. #\.) (#\T #\T #\T #\.)))
- #t)
- (check-expect (contiguous? '((#\. #\. #\. #\.) (#\T #\T #\. #\T) (#\T #\T #\T #\T)))
- #t)
- (check-expect (contiguous? '((#\. #\. #\T #\. #\. #\.)))
- #f)
- (check-expect (contiguous? '((#\T #\T #\. #\.) (#\. #\. #\T #\T) (#\T #\T #\T #\.)))
- #f)
- (check-expect (contiguous? '((#\. #\. #\. #\.) (#\T #\T #\T #\T) (#\T #\T #\T #\.)))
- #f)
- (check-expect (contiguous-count '((#\. #\. #\. #\. #\.)) #t)
- 5)
- (check-expect (contiguous-count '((#\. #\T #\. #\.) (#\. #\. #\. #\T) (#\T #\T #\T #\T)) #t)
- 6)
- (check-expect (contiguous-count '((#\T #\T #\. #\.) (#\T #\T #\. #\.) (#\T #\T #\T #\.)) #t)
- 5)
- (check-expect (contiguous-count '((#\. #\. #\. #\.) (#\T #\T #\. #\T) (#\T #\T #\T #\T)) #t)
- 5)
- (check-expect (contiguous-count '((#\. #\. #\T #\. #\. #\.)) #t)
- 3)
- (check-expect (contiguous-count '((#\T #\T #\. #\.) (#\. #\. #\T #\T) (#\T #\T #\T #\.)) #t)
- 2)
- (check-expect (contiguous-count '((#\. #\. #\. #\.) (#\T #\T #\T #\T) (#\T #\T #\T #\.)) #t)
- 4)
Advertisement
Add Comment
Please, Sign In to add comment