Created
October 24, 2014 22:30
-
-
Save nickmain/0f33d1effe5bf3c9ee4a to your computer and use it in GitHub Desktop.
Racket solution for http://java.dzone.com/articles/my-favorite-coding-interview
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
#lang racket | |
(define (run-length n) | |
(let ([run-char #f] | |
[run-len 0]) | |
(λ(c) | |
(if (equal? c run-char) | |
(set! run-len (+ run-len 1)) | |
(begin (set! run-len 1) | |
(set! run-char c))) | |
(<= run-len n)))) | |
(define (remove-extra-consecutive s i) | |
(list->string | |
(filter (run-length i) | |
(string->list s)))) | |
(remove-extra-consecutive "aaab" 2) | |
(remove-extra-consecutive "aabb" 1) | |
(remove-extra-consecutive "aabbaa" 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment