pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

teenlinux private pastebin - collaborative debugging tool What's a private pastebin?


Posted by free-zombie on Wed 22 Oct 22:07
report abuse | download | new post

  1. ;; Solution to the TeenLinux programming challenge 2 (Common Lisp)
  2. ;; Copyright (c) 2008 Thomas Jollans
  3. ;;; You may use this code under the terms of the WTF Public License version 2
  4. ;;; if you recognize that it's not my fault if it harms you in any way.
  5.  
  6. (defun digitsof (numb)
  7.   (if (> numb 0)
  8.       (cons (mod numb 10) (digitsof (floor (/ numb 10))))
  9.       ()))
  10.  
  11. (defun unique-p (lst &optional (bad ()))
  12.   (cond ((eq lst ()) t)
  13.         ((member (car lst) bad) nil)
  14.         (t (unique-p (cdr lst) (cons (car lst) bad)))))
  15.  
  16. ;;; this version is nice and short, but fails at large numbers
  17. ;;; due to recursion (stack overflows), and lacks some features
  18. ;(defun with-unique-digits (from to)
  19. ;  (cond ((> from to) ())
  20. ;       ((unique-p (digitsof from))
  21. ;        (cons from (with-unique-digits (1+ from) to)))
  22. ;       (t
  23. ;        (with-unique-digits (1+ from) to))))
  24.  
  25.  
  26. ;;; This version works with large numbers, and provides additional
  27. ;;; information. It doesn't work on all common lisp implementations
  28. ;;; due to non-standard syntax.
  29. (defun with-unique-digits (start end)
  30.   (loop
  31.      for i from start to end
  32.      with lastval = (1- start)
  33.      with use = nil
  34.        
  35.      maximizing (- i lastval) into maxjump
  36.      do (if (setf use (unique-p (digitsof i)))
  37.             (setf lastval i))
  38.      append (and use (list i)) into lnums
  39.      count use into nnums
  40.      ; The line below is non-standard code, specified in
  41.      ;                                Common Lisp the Language, 2nd Edition
  42.      finally return (values lnums nnums maxjump)))
  43.  
  44.  
  45. (defun unique-digits-info (start end)
  46.   (multiple-value-bind (lnums nnums maxjump) (with-unique-digits start end)
  47.     (format t "Numbers without repeating digits between ~d and ~d incl.:~%"
  48.             start end)
  49.     (format t "Total number of values: ~d~%" nnums)
  50.     (format t "Largest jump: ~d~%" maxjump)
  51.     (values nil lnums)))
  52.  
  53.  
  54. ; tips for testers:
  55. ; * compile. (with CLISP: clisp -c foo.lisp), then just load with (load "foo")
  56. ; * Avoid seeing the whole value list returned, do
  57. ;   (progn (unique-digits-info n m) nil)
  58. ; * Time (if you must) with
  59. ;   (time (progn (unique-digits-info 1 1000000) nil))

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post