Skip to content

Instantly share code, notes, and snippets.

@youz
Created May 16, 2012 08:52
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 youz/2708823 to your computer and use it in GitHub Desktop.
Save youz/2708823 to your computer and use it in GitHub Desktop.
逆fizzbuzz in Common Lisp
;;; http://www.jasq.org/2/post/2012/05/inverse-fizzbuzz.html
;;; http://d.hatena.ne.jp/matarillo/20120515/p1
(ql:quickload :cl-ppcre)
(defun scan-all (re str &optional (start 0))
(multiple-value-bind (s e) (ppcre:scan re str :start start)
(when s (cons (list (1+ s) e) (scan-all re str (1+ s))))))
(defun inverse-fizzbuzz (fb)
(let ((re (format nil "~{~A~^_*?~}" (mapcar #'(lambda (s) (case s (fizz "f") (buzz "b") (t "z"))) fb)))
(pattern (format nil "~V@{__f_bf__fb_f__z~}" (+ 2 (floor (length fb) 7)) t)))
(car (sort (scan-all re pattern) #'> :key (lambda (p) (apply #'- p))))))
(format t "~{~&~A~}"
(mapcar #'inverse-fizzbuzz
`((fizz)
(buzz)
(fizz fizz buzz)
(fizz buzz)
(buzz fizz)
(fizz buzz fizz)
(fizz fizz)
(buzz buzz)
(fizzbuzz fizz buzz)
)))
; (3 3)
; (5 5)
; (6 10)
; (9 10)
; (5 6)
; (3 6)
; (6 9)
; NIL
; (15 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment