Chiao-Lun Cheng | 25 Jul 03:34
Picon

[QUIZ 5b & 5e] Date differences, and betting (Larry Clapp) [spoiler]

My solution to the betting problem:

(defun bet (total maxgames score1 score2 &optional err-check-off)
  (let ((thresh (floor (/ maxgames 2))))
    (unless err-check-off
      (cond ((evenp maxgames)
             (error "Only odd numbers of total games allowed"))
            ((or (> score1 thresh)
                 (> score2 thresh))
             (error "Scores must be below half the total number of games"))
            ((or (< maxgames 0)
                 (< score1 0)
                 (< score2 0))
             (error "maxgame, score1 and score2 must not be negative"))))
    (cond ((or (= score1 (1+ thresh))
               (= score2 (1+ thresh)))
           0)
          ((and (= score1 thresh)
                (= score2 thresh))
           total)
          (t (/ (+ (bet total maxgames (1+ score1) score2 t)
                   (bet total maxgames score1 (1+ score2) t))
                2)))))

Given the you bet $a this round, and $b / $c next round, then a
win-loss or a loss-win must put you back at the same value, meaning
that

a - b = -a +c

(Continue reading)

Larry Clapp | 20 Jul 01:36

[QUIZ 5b & 5e] Date differences, and betting

Beginner:

Adapted from Perl Quiz #2:
[ http://perl.plover.com/~alias/list.cgi?mss:11:200210:ccbcblpijjjcnhnkabep ]

Write a function, days-diff, to compute the time difference, in days,
between two dates.  The dates will be strings in the format

        Wed Oct 16 2002

For example:

        (days-diff "Wed Oct 16 2002" "Wed Oct 23 2002")
	=> 7

        (days-diff "Wed Oct 16 2002" "Tue Oct 16 2001")
	=> -365

Error checking: at minimum, signal an error if either value is
unparsable or internally inconsistent (e.g. if 10/16/2002 is not, in
fact, a Wednesday).  More advanced users can establish useful
restarts.

Dependencies on your Lisp's time functions are okay.

Expert:

Adapted from Expert Perl Quiz #2:
[ http://perl.plover.com/~alias/list.cgi?mss:12:200210:mflioldnngfgmfnlfljf ]

(Continue reading)

Larry Clapp | 12 Jul 00:36

call for quizzes, and [QUIZ 4] centering

I think there's interest in this list, I just think the guy that
started it (Marco Baringer) ran out of or overestimated his free time.

If you have a puzzle or quiz, feel free to post it to the list.  Heck,
if you have some (short) Lisp code that's just not working and you
don't know why, post it to the list.

[ /me Googles ... ]

This list started as an imitation of the Perl quiz of the week, so
I'll shamelessly copy their first quiz (see below).

I'd also like to ask everyone to take a moment and re-read the rules
of the game as posted here[1].  In particular: no solutions for 48
hours; if you post one after that, include "[SPOILER]" in the subject.
I'll add to that: If you reply to a SPOILER thread and cut out the
solution, you can remove SPOILER from the subject (or maybe change it
to UNSPOILED just for fun :).  Also, the Perl quiz site has the
following Q&A (paraphrased): Q: Where to I send my answer?  A: Don't
feel obligated to send it anywhere.  The point of solving the puzzle
is to have solved it.

I'm not Marco, of course, so feel free to ignore me.  :)

Anyway, the quiz:

Write a function, 'center', whose argument is a list of strings, which
will be lines of text.  'center' should insert spaces at the beginning
of the lines of text so that if they were printed, the text would be
centered, and return the modified lines.
(Continue reading)

Chiao-Lun Cheng | 11 Jul 19:38
Picon
Favicon

XKCD Appetizers Solution

I've pasted a brute force solution that uses Peter Norvig's
tree-search function at

http://paste.lisp.org/display/44376

--

-- 
*******************************************
 Chiao-Lun Cheng
 Van Voorhis Group
 Department of Chemistry, Room 6-234
 Massachusetts Institute of Technology
 77 Massachusetts Avenue
 Cambridge, MA 02139-4307

 Email  : chiao@...
 Tel    : +1 (617) 253-1539
 Website    : http://zeno.unixboxen.net
*******************************************
Larry Clapp | 10 Jul 17:03

[QUIZ 3] reader macro

Okay, here's my 2 cent quiz:

Define a reader macro that reads strings but treats backslashes as
normal characters, except if you escape the string terminator.  This
should work sort of like Perl's single quote string syntax:

Perl:

  $pattern = '\w \s';		# five characters

Lisp:

  (let ((pattern #"\w \s"))	; five characters
    ...)

  as opposed to

  (let ((pattern "\\w \\s"))	; five characters
    ...)

  but this works:

  (let ((pattern #"\w \" \s"))	; seven characters
    ...)

The #" syntax is just to give you an idea of what I want; you can use
whatever macro character(s) you like.

Send answers to the list, preferably with at least one working test
case displayed, e.g. something like
(Continue reading)

Ivan Salazar | 7 Jul 03:38
Picon

Anybody there?

It sounds interesting. I'd like to join in, even if I'm a real newbie.
Have you got any ideas?

_______________________________________________
quiz mailing list
quiz@...
http://common-lisp.net/cgi-bin/mailman/listinfo/quiz
Carl | 7 Jul 01:46
Picon
Gravatar

Anybody there?

Is the CL-Quiz/mailing list still active? The archive doesn't have any
articles more recent than November '06.

I'm just getting started in CL and thought this might be a good
resource for practice and learning.

Carl.
Laurent PETIT | 28 Nov 14:26
Favicon

Enhanced Strings in CL ?

Hello Quizzers,

I'm a newbie in Lisp (have already finished the ANSI Common Lisp book, made some tries with programming as well, and I'm searching the answer to this question I have : could it be possible to "enhance" the reader in order to have in lisp "enhanced strings".

That is : by just doing something once at the beginning of a lisp file (some call, ...), being able to have all the strings replaced by another string (or another form).

Something like this :

;;; beginning of file

(enhanced-strings:start)

...
...

(defun some-function (x y)
   "This returned string is an enhanced string and the value of x and y will be replaced 'a la ruby' : x=${x}, y =${y}")

You see what I mean ....

This could be an interesting quiz, but first of all, will an ANSI compliant implementation let redefine the reader to alter the meaning of strings ?

I know I can add macro characters, play with macro character dispatching functions, ... but what about "overriding" the reader to intercept strings and change them, as for the above example, with :

(format t "This returned string is an enhanced string and the value of x and y will be replaced 'a la ruby' : x=~a, y =~a") x y)


thanks in advance for your answers, both on feasibility and/or complete answers to this "unofficial" quiz ;-)

And oh, if I definitely bothered you by sending this message on the wrong list, please let me know what would be the most natural list/newsgroup to post this message on ?


Thanks in advance,

A lisp beginner,

--
Laurent PETIT

_______________________________________________
quiz mailing list
quiz@...
http://common-lisp.net/cgi-bin/mailman/listinfo/quiz
Stuart Sierra | 11 May 21:10

quiz 2 without a stack?

I don't have a solution, but I'm curious: Is it possible to solve Quiz 
#2 in a purely functional way, without a temporary "stack" variable?
-Stuart
Pablo Barenbaum | 10 May 21:19
Picon

A (hopefully) working solution

A solution that seems to work =)

- it ignores blank lines
- it ignores comments starting with ;
- it expects the !# to be alone in a single line
- it gets screwed up with multiline strings and #| ... |# comments,
  and possibly with most extensions to the reader
- I keep overusing format and loop =b

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar *blanks* '(#\Space #\Tab #\Newline #\Return))
(defvar *tabs* '(#\Tab))
(defvar *comment-markers* `(#\;))
(defvar *tab-width* 8)

(defun width (char)
  (if (member char *tabs*)
    *tab-width*
    1))

(defun distance (str)
 (do ((col 0 (+ col (width (aref str col)))))
   ((not (member (aref str col) *blanks* :test #'char=))
    col)))

(defun trim-blanks (str)
  (string-trim *blanks* str))

(defun blank-line-p (str)
 (let ((trimmed (trim-blanks str)))
   (or (string= "" trimmed)
       (member (aref trimmed 0) *comment-markers* :test #'char=))))

(defun last-line-p (line)
 (or (eq line 'eof) (string= (trim-blanks line) "!#")))

(defun read-off-side-to-string (stream)
 (let ((prev-lines (list (list "PROGN")))
       (prev-dists (list -1))
       (prev-dist -1))
   (flet ((pop-expr ()
            (let* ((tail (pop prev-lines))
                   (head (pop (first prev-lines))))
              (push
                (format nil "(~A~%~{~A~%~})" head (nreverse tail))
                (first prev-lines))))
          (next-line ()
	   (loop for line = (read-line stream nil 'eof)
                   while (and (not (eq line 'eof)) (blank-line-p line))
                   finally (return line))))
     (with-output-to-string (s)
       (loop
         for line = (next-line)
         while (not (last-line-p line))
         do (let ((dist (distance line)))
              (when (> dist prev-dist)
                (push dist prev-dists)
                (push (list) prev-lines))
              (loop while (and (not (null (cdr prev-dists)))
                               (< dist (car prev-dists)))
                    do (pop prev-dists)
                    do (pop-expr))
              (push line (first prev-lines))
              (setf prev-dist dist)))

       (loop while (not (null (cdr prev-dists)))
             do (pop prev-dists)
             do (pop-expr))
       (format s "~{~A~%~}" (nreverse (pop prev-lines)))))))

(defun read-off-side (stream c n)
  (let ((r (nth-value 0 (read-from-string (read-off-side-to-string stream)))))
    (if (atom r)
      (list r)
      r)))
Ivan Boldyrev | 9 May 19:48
X-Face
Picon

[SPOILER] Quiz #2

I can't run test because FiveAM from Gentoo portage (1.2.3) has no
FOR-ALL (or is it from other package?).  But I tested it manually.

----------------------------------------------------------------------
;;; Copyright (C) 2006 Ivan Boldyrev
;;;    This code is freely redistributable.

(cl:defpackage #:quiz2
  (:use #:cl))

(cl:in-package #:quiz2)

(defvar *stack* nil
  "Stack for parsed objects")

(defun tab-ws (pos)
  #-dont-blame-tabspace(warn "Tabular is found at pos ~S" pos)
  (* 8 (floor (+ pos 8) 8)))

(defun count-ws (input-stream)
  (loop :for cnt := 0 :then (case char
                              ((#\Space)
                               (1+ cnt))
                              ((#\Tab)
                               (tab-ws cnt))
                              ((#\Return #\Linefeed)
                               ;; Space-only line!  Just ignore it
                               (return-from count-ws
                                 (count-ws input-stream)))
                              (otherwise
                               (unread-char char input-stream)
                               (return-from count-ws
                                 cnt)))
        :for char := (read-char input-stream)))

(defun combine-lines (first rest)
  (cons first (nreverse rest)))

(defun reduce-forms (offset)
  (let ((tail (cdr (pop *stack*))))
    (if (> offset (car (first *stack*)))
        (push (list offset (nreverse tail))
              *stack*)
        (let ((first (pop (cdr (first *stack*)))))
          (progn (push (combine-lines first tail)
                       (cdr (first *stack*)))
                 *stack*)))))

(defun read-the-line (input-stream)
  (let ((space-offset (count-ws input-stream))
        (data (read-preserving-whitespace input-stream)))
    ;; Perform all reductions
    (loop :while (and (rest *stack*)
                      (< space-offset (car (first *stack*))))
          :do (reduce-forms space-offset))
    (cond
      ((and (symbolp data)
            (string= (symbol-name data) "!#"))
       (throw 'thats-all-folks
         (cons 'progn (loop :while (rest *stack*)
                            :do (reduce-forms 0)
                            :finally
                            (return (nreverse (cdr (first *stack*))))))))
      ((and *stack*
            (= space-offset (car (first *stack*))))
       (push data (cdr (first *stack*)))
       *stack*)
      (t
       (push (cons space-offset (list data))
             *stack*)))))

(defun read-off-side (stream subchar parameter)
  (declare (ignore subchar parameter))
  (let ((*stack* nil))
    ;; Catch is duty hack here.  Do not repeat at home!
    (catch 'thats-all-folks
      (loop
       (read-the-line stream)))))
----------------------------------------------------------------------

--

-- 
Ivan Boldyrev

                      Ok people, move along, there's nothing to see here.

Gmane