Skip to content

Instantly share code, notes, and snippets.

@no1xsyzy
Last active April 7, 2019 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save no1xsyzy/9f10ccc3f90388e93b3317027be5f44c to your computer and use it in GitHub Desktop.
Save no1xsyzy/9f10ccc3f90388e93b3317027be5f44c to your computer and use it in GitHub Desktop.
#lang racket
(define (remove-palindrome s)
(define f
(cachedproc
(lambda (i j)
(cond
[(equal? i j) 0]
[(equal? (+ i 1) j) 1]
[(and (equal? (+ i 2) j) (equal? (list-ref s i) (list-ref s (- j 1)))) 1]
[(equal? (list-ref s i) (list-ref s (- j 1))) (f (+ i 1) (- j 1))]
[else (apply min (map (lambda (k) (+ (f i k) (f k j))) (range (+ i 1) j)))]))))
(f 0 (length s)))
(define (cachedproc func)
(let ([h (make-hash)])
(define (wrapped . w)
(hash-ref! h w (lambda () (apply func w))))
wrapped))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment