From c.groner at gmail.com Fri Jul 6 23:46:56 2007 From: c.groner at gmail.com (Carl) Date: Fri, 6 Jul 2007 16:46:56 -0700 Subject: [quiz] Anybody there? Message-ID: 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. From luis at geodynamics.org Sat Jul 7 01:34:30 2007 From: luis at geodynamics.org (Luis Armendariz) Date: Fri, 06 Jul 2007 18:34:30 -0700 Subject: [quiz] Anybody there? In-Reply-To: References: Message-ID: <468EEDA6.5090000@geodynamics.org> Carl wrote: > 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. I felt the same way, but I've received no other emails from this list either. Someone should revive it. Maybe it's up to us to come up with nice problems and send them to the list directly? :-) -Luis From ivan.salazarv at gmail.com Sat Jul 7 01:38:14 2007 From: ivan.salazarv at gmail.com (Ivan Salazar) Date: Fri, 6 Jul 2007 20:38:14 -0500 Subject: [quiz] Anybody there? Message-ID: <90bf67330707061838s195d5b2h3c88240558c75a82@mail.gmail.com> It sounds interesting. I'd like to join in, even if I'm a real newbie. Have you got any ideas? -------------- next part -------------- An HTML attachment was scrubbed... URL: From ryan at acceleration.net Mon Jul 9 14:27:44 2007 From: ryan at acceleration.net (Ryan Davis) Date: Mon, 09 Jul 2007 10:27:44 -0400 Subject: [quiz] Anybody there? In-Reply-To: <90bf67330707061838s195d5b2h3c88240558c75a82@mail.gmail.com> References: <90bf67330707061838s195d5b2h3c88240558c75a82@mail.gmail.com> Message-ID: <469245E0.4050801@acceleration.net> How about the problem in today's XKCD?: http://xkcd.com/c287.html Ryan Davis Acceleration.net Director of Programming Services 2831 NW 41st street, suite B Gainesville, FL 32606 Office: 352-335-6500 x 124 Fax: 352-335-6506 Ivan Salazar wrote: > 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 at common-lisp.net > http://common-lisp.net/cgi-bin/mailman/listinfo/quiz > -------------- next part -------------- An HTML attachment was scrubbed... URL: From larry at theclapp.org Tue Jul 10 15:03:18 2007 From: larry at theclapp.org (Larry Clapp) Date: Tue, 10 Jul 2007 11:03:18 -0400 Subject: [quiz] [QUIZ 3] reader macro Message-ID: <20070710150318.GH10251@santa.theclapp.org> 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 cl-user> #"\w \" \s" "\\w \" \\s" cl-user> -- L (Yes, this is similar to a previous quiz. Sue me.) From mtbp at rci.rutgers.edu Wed Jul 11 01:31:34 2007 From: mtbp at rci.rutgers.edu (Matt Pillsbury) Date: Tue, 10 Jul 2007 21:31:34 -0400 Subject: [quiz] [QUIZ 3] reader macro In-Reply-To: <20070710150318.GH10251@santa.theclapp.org> References: <20070710150318.GH10251@santa.theclapp.org> Message-ID: <43634257-C2A4-435D-B378-ADB21E4A8589@rci.rutgers.edu> On Jul 10, 2007, at 11:03 , Larry Clapp wrote: > Send answers to the list, preferably with at least one working test > case displayed, e.g. something like > cl-user> #"\w \" \s" > "\\w \" \\s" Well, I ended up doing something slightly different. (defun |#?-READER| (s c n) (declare (ignore c n)) (let ((term (read-char s))) (with-output-to-string (string) (loop :for char := (read-char s) :until (char= char term) :if (and (char= char #\\) (char= (peek-char nil s) term)) :do (write-char (read-char s) string) :else :do (write-char char string))))) (set-dispatch-macro-character #\# #\? #'|#?-READER|) This means that whatever character follows the #? becomes the delimeter, like so: CL-USER> #?"foo\"bar" "foo\"bar" CL-USER> #?%foo"bar% "foo\"bar" CL-USER> #?%foo\\bar% "foo\\\\bar" CL-USER> #?%foo\"bar% "foo\\\"bar" Cheers, Pillsy From ivan.salazarv at gmail.com Wed Jul 11 03:35:45 2007 From: ivan.salazarv at gmail.com (Ivan Salazar) Date: Tue, 10 Jul 2007 22:35:45 -0500 Subject: [quiz] Anybody there? In-Reply-To: <469245E0.4050801@acceleration.net> References: <90bf67330707061838s195d5b2h3c88240558c75a82@mail.gmail.com> <469245E0.4050801@acceleration.net> Message-ID: <90bf67330707102035w7878475at69fe85e34ac7a3c2@mail.gmail.com> 2007/7/9, Ryan Davis : > > How about the problem in today's XKCD?: http://xkcd.com/c287.html > > Ryan Davis > Acceleration.net > Director of Programming Services > 2831 NW 41st street, suite B > Gainesville, FL 32606 > > Office: 352-335-6500 x 124 > Fax: 352-335-6506 > > > > Ivan Salazar wrote: > > 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 at common-lisp.nethttp://common-lisp.net/cgi-bin/mailman/listinfo/quiz > > > _______________________________________________ > quiz mailing list > quiz at common-lisp.net > http://common-lisp.net/cgi-bin/mailman/listinfo/quiz > > OK... This is the second time I've heard of the knapsack problem. I've searched a little and I have some questions: - Are repetitions allowed (one kind of appetizer chosen more than once)? - Should we maximize or minimize the number of appetizers? If I were the waiter, it wouldn't matter if I chose an appetizer more than once but I'd try to carry less appetizers. If I were the costumer, I'd like a diverse mix (no repetitions) and more appetizers. I'm thinking about programming a more general algorithm, something like this: (defun serve-appetizers (menu amount &key (:with-reps nil) (:minimize t)) ;... lots of code I have to do more research obviously (maybe it isn't feasible to program such thing, I don't now). Have you got any particular idea or solution? Iv?n. -------------- next part -------------- An HTML attachment was scrubbed... URL: From larry at theclapp.org Wed Jul 11 12:38:17 2007 From: larry at theclapp.org (Larry Clapp) Date: Wed, 11 Jul 2007 08:38:17 -0400 Subject: [quiz] [QUIZ 3] reader macro In-Reply-To: <43634257-C2A4-435D-B378-ADB21E4A8589@rci.rutgers.edu> References: <20070710150318.GH10251@santa.theclapp.org> <43634257-C2A4-435D-B378-ADB21E4A8589@rci.rutgers.edu> Message-ID: <20070711123817.GI10251@santa.theclapp.org> On Tue, Jul 10, 2007 at 09:31:34PM -0400, Matt Pillsbury wrote: > On Jul 10, 2007, at 11:03 , Larry Clapp wrote: > >Send answers to the list, preferably with at least one working test > >case displayed, e.g. something like > > > cl-user> #"\w \" \s" > > "\\w \" \\s" > > Well, I ended up doing something slightly different. > > (defun |#?-READER| (s c n) [snip] > This means that whatever character follows the #? becomes the > delimeter, like so: > > CL-USER> #?"foo\"bar" > "foo\"bar" > > CL-USER> #?%foo"bar% > "foo\"bar" [snip] I like it. Works for me in Lispworks for Linux. This one struck me kind of funny, for no good reason: CL-USER 8 > #?(foo"bar \w \s \t( "foo\"bar \\w \\s \\t" That is, the use of #\( as a delimiter. It kind of makes me smile and cringe at the same time. ... So I fixed it: (defvar *matched-delimiters* '(( #\( . #\) ) ( #\{ . #\} ) ( #\[ . #\] ) ( #\< . #\> ))) (defun |#?-READER| (s c n) (declare (ignore c n)) (let* ((term (read-char s)) (match (assoc term *matched-delimiters*))) (when match (setf term (cdr match))) (with-output-to-string (string) (loop :for char := (read-char s) :until (char= char term) :if (and (char= char #\\) (char= (peek-char nil s) term)) :do (write-char (read-char s) string) :else :do (write-char char string))))) CL-USER 12 > #?(test) "test" CL-USER 13 > #?{foo"bar} "foo\"bar" CL-USER 14 > #?{foo\}bar} "foo}bar" This would make an amusing (and confusing and silly and misleading, etc) way to comment out some code: slap a #? in front of the leading #\(. (But see below.) Extra credit 1 for newbies: my first version of *matched-delimiters* looked like this: '(( \( . \) ) ( { . } ) ( [ . ] ) ( < . > )) This did not work; explain why. Extra credit 2 for newbies (really new programmers as well as new Lispers): My remark about commenting out code by slapping a #? in front of the leading #\(, above, wouldn't work either, at least not as the code stands. Explain why, or extend the code so that it would. Discussion: Do you like the idea of the #?(string) or hate it? How about #?(strings (with (nested (parentheses)))) ? You know, given all the strange and silly things you can do with read macros, I'm surprised that Lisp isn't the king of obfuscated code contests. But then again maybe it's just a matter of "what's the point?" "Lisp is easier than Perl to make unreadable if you diddle the readtable. Also, water is wet. Film at 11." :) -- Larry From larry at theclapp.org Wed Jul 11 12:59:30 2007 From: larry at theclapp.org (Larry Clapp) Date: Wed, 11 Jul 2007 08:59:30 -0400 Subject: [quiz] Anybody there? In-Reply-To: <90bf67330707102035w7878475at69fe85e34ac7a3c2@mail.gmail.com> References: <90bf67330707061838s195d5b2h3c88240558c75a82@mail.gmail.com> <469245E0.4050801@acceleration.net> <90bf67330707102035w7878475at69fe85e34ac7a3c2@mail.gmail.com> Message-ID: <20070711125929.GJ10251@santa.theclapp.org> On Tue, Jul 10, 2007 at 10:35:45PM -0500, Ivan Salazar wrote: > 2007/7/9, Ryan Davis : > > How about the problem in today's XKCD?: http://xkcd.com/c287.html > OK... This is the second time I've heard of the knapsack problem. > I've searched a little and I have some questions: > - Are repetitions allowed (one kind of appetizer chosen more than > once)? Yes. > - Should we maximize or minimize the number of appetizers? Either. Whatever fits. > If I were the waiter, it wouldn't matter if I chose an appetizer > more than once but I'd try to carry less appetizers. > If I were the costumer, I'd like a diverse mix (no repetitions) and > more appetizers. > > I'm thinking about programming a more general algorithm, something > like this: > > (defun serve-appetizers (menu amount &key (:with-reps nil) (:minimize t)) > ;... lots of code > > I have to do more research obviously (maybe it isn't feasible to > program such thing, I don't now). It's feasible to *program* it. It's frequently not feasible to *run* it. NP-complete problems grow in runtime very quickly. That is, you can solve one in a minute for n=5, an hour for n=6, a day for n=7, and a year for n=8 (pulling these numbers out of thin air just to illustrate the point). I *think* that factoring numbers is NP-complete. Note that the security of public-key cryptography hinges on the difficulty of factoring large numbers. > Have you got any particular idea or solution? Use very small data sets. :) ... I've said all this on the assumption that because you're unfamiliar with the knapsack problem, you're also unfamiliar with NP-complete problems in general (like the travelling salesman problem); if this is not true, I apologize. -- Larry From chiao at mit.edu Wed Jul 11 17:38:11 2007 From: chiao at mit.edu (Chiao-Lun Cheng) Date: Wed, 11 Jul 2007 13:38:11 -0400 Subject: [quiz] XKCD Appetizers Solution Message-ID: <208121240707111038h75111fb7s13162e80a1386bc8@mail.gmail.com> 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 at mit.edu Tel : +1 (617) 253-1539 Website : http://zeno.unixboxen.net ******************************************* From ivan.salazarv at gmail.com Wed Jul 11 21:07:53 2007 From: ivan.salazarv at gmail.com (Ivan Salazar) Date: Wed, 11 Jul 2007 16:07:53 -0500 Subject: [quiz] Anybody there? In-Reply-To: <20070711125929.GJ10251@santa.theclapp.org> References: <90bf67330707061838s195d5b2h3c88240558c75a82@mail.gmail.com> <469245E0.4050801@acceleration.net> <90bf67330707102035w7878475at69fe85e34ac7a3c2@mail.gmail.com> <20070711125929.GJ10251@santa.theclapp.org> Message-ID: <90bf67330707111407v71859532tf92d705b5b425a94@mail.gmail.com> 2007/7/11, Larry Clapp : > On Tue, Jul 10, 2007 at 10:35:45PM -0500, Ivan Salazar wrote: > > 2007/7/9, Ryan Davis : > > > How about the problem in today's XKCD?: http://xkcd.com/c287.html > > OK... This is the second time I've heard of the knapsack problem. > > I've searched a little and I have some questions: > > - Are repetitions allowed (one kind of appetizer chosen more than > > once)? > > Yes. > > > - Should we maximize or minimize the number of appetizers? > > Either. Whatever fits. > > > If I were the waiter, it wouldn't matter if I chose an appetizer > > more than once but I'd try to carry less appetizers. > > If I were the costumer, I'd like a diverse mix (no repetitions) and > > more appetizers. > > > > I'm thinking about programming a more general algorithm, something > > like this: > > > > (defun serve-appetizers (menu amount &key (:with-reps nil) (:minimize t)) > > ;... lots of code > > > > I have to do more research obviously (maybe it isn't feasible to > > program such thing, I don't now). > > It's feasible to *program* it. It's frequently not feasible to *run* > it. NP-complete problems grow in runtime very quickly. That is, you > can solve one in a minute for n=5, an hour for n=6, a day for n=7, and > a year for n=8 (pulling these numbers out of thin air just to > illustrate the point). > > I *think* that factoring numbers is NP-complete. Note that the > security of public-key cryptography hinges on the difficulty of > factoring large numbers. > > > Have you got any particular idea or solution? > > Use very small data sets. :) > > ... I've said all this on the assumption that because you're > unfamiliar with the knapsack problem, you're also unfamiliar with > NP-complete problems in general (like the travelling salesman > problem); if this is not true, I apologize. > > -- Larry > > _______________________________________________ > quiz mailing list > quiz at common-lisp.net > http://common-lisp.net/cgi-bin/mailman/listinfo/quiz > OK, was this quiz a joke then? u_u From larry at theclapp.org Wed Jul 11 22:08:15 2007 From: larry at theclapp.org (Larry Clapp) Date: Wed, 11 Jul 2007 18:08:15 -0400 Subject: [quiz] Anybody there? In-Reply-To: <90bf67330707111407v71859532tf92d705b5b425a94@mail.gmail.com> References: <90bf67330707061838s195d5b2h3c88240558c75a82@mail.gmail.com> <469245E0.4050801@acceleration.net> <90bf67330707102035w7878475at69fe85e34ac7a3c2@mail.gmail.com> <20070711125929.GJ10251@santa.theclapp.org> <90bf67330707111407v71859532tf92d705b5b425a94@mail.gmail.com> Message-ID: <20070711220815.GM10251@santa.theclapp.org> On Wed, Jul 11, 2007 at 04:07:53PM -0500, Ivan Salazar wrote: > OK, was this quiz a joke then? u_u Well, I didn't propose it. I don't know what the guy who suggested it had in mind, one way or the other. I initially thought it would be a hard problem (or at least take a long time to solve), thus my cautionary remarks, but the solution posted here earlier took ~0.001 seconds on my machine. I guess this dataset was sufficiently small. :) -- L From larry at theclapp.org Wed Jul 11 20:31:30 2007 From: larry at theclapp.org (Larry Clapp) Date: Wed, 11 Jul 2007 16:31:30 -0400 Subject: [quiz] XKCD Appetizers Solution In-Reply-To: <208121240707111038h75111fb7s13162e80a1386bc8@mail.gmail.com> References: <208121240707111038h75111fb7s13162e80a1386bc8@mail.gmail.com> Message-ID: <20070711203130.GL10251@santa.theclapp.org> On Wed, Jul 11, 2007 at 01:38:11PM -0400, Chiao-Lun Cheng wrote: > I've pasted a brute force solution that uses Peter Norvig's > tree-search function at > > http://paste.lisp.org/display/44376 Cool. I took the liberty of posting it to the xkcd forum. Hope that's okay. http://forums.xkcd.com/viewtopic.php?t=7532 -- Larry From larry at theclapp.org Wed Jul 11 22:36:18 2007 From: larry at theclapp.org (Larry Clapp) Date: Wed, 11 Jul 2007 18:36:18 -0400 Subject: [quiz] call for quizzes, and [QUIZ 4] centering Message-ID: <20070711223618.GO10251@santa.theclapp.org> 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. For example, (center "This" "is" "a test of the" "center function") should return the list: (" This" " is" " a test of the" "center function") because if these lines were printed, they would look like: This is a test of the center function -- Larry [1] http://common-lisp.net/project/quiz/ From peter at gigamonkeys.com Thu Jul 12 02:47:01 2007 From: peter at gigamonkeys.com (Peter Seibel) Date: Wed, 11 Jul 2007 19:47:01 -0700 Subject: SPOILER: Re: [quiz] call for quizzes, and [QUIZ 4] centering In-Reply-To: <20070711223618.GO10251@santa.theclapp.org> References: <20070711223618.GO10251@santa.theclapp.org> Message-ID: <46959625.1080800@gigamonkeys.com> Larry Clapp wrote: > 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 going to unilaterally change the rules (at least as they apply to me ;-)) and say that it's okay to send a solution whenever you want as long as it's got SPOILER in the subject. If I hack up a solution I don't want to have to remember to come back in two days and send it to the list. So here's my version of CENTER. (defun center (&rest list) (let ((max (reduce #'max list :key #'length))) (format t "~{~v,0t~a~%~}" (mapcan #'(lambda (x) (list (floor (- max (length x)) 2) x)) list)))) REPL> (center "This" "is" "a test of the" "center function") This is a test of the center function NIL -Peter -- Peter Seibel : peter at gigamonkeys.com A Billion Monkeys Can't be Wrong : http://www.gigamonkeys.com/blog/ Practical Common Lisp : http://www.gigamonkeys.com/book/ Coders at Work : http://www.codersatwork.com/ From rosssd at gmail.com Thu Jul 12 09:09:12 2007 From: rosssd at gmail.com (Sean) Date: Thu, 12 Jul 2007 10:09:12 +0100 Subject: SPOILER : Re: [quiz] call for quizzes, and [QUIZ 4] centering In-Reply-To: <20070711223618.GO10251@santa.theclapp.org> References: <20070711223618.GO10251@santa.theclapp.org> Message-ID: <1184231353.7262.2.camel@deepthought> A solution using the minpad argument to ~A (defun center (&rest list) (let ((max (reduce #'max vals :key #'length))) (mapcar #'(lambda (x) (format nil "~,,v at A" (floor (- max (length x)) 2) x)) list))) Sean. On Wed, 2007-07-11 at 18:36 -0400, Larry Clapp wrote: > 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. > > For example, > > (center "This" "is" "a test of the" "center function") > > should return the list: > > (" This" " is" " a test of the" "center function") > > because if these lines were printed, they would look like: > > This > is > a test of the > center function > > -- Larry > > > [1] http://common-lisp.net/project/quiz/ > > _______________________________________________ > quiz mailing list > quiz at common-lisp.net > http://common-lisp.net/cgi-bin/mailman/listinfo/quiz -- ...Please don't assume Lisp is only useful for Animation and Graphics, AI, Bioinformatics, B2B and E-Commerce, Data Mining, EDA/Semiconductor applications, Expert Systems, Finance, Intelligent Agents, Knowledge Management, Mechanical CAD, Modeling and Simulation, Natural Language, Optimization, Research, Risk Analysis, Scheduling, Telecom, and Web Authoring just because these are the only things they happened to list. Kent Pitman. From larry at theclapp.org Thu Jul 12 10:58:04 2007 From: larry at theclapp.org (Larry Clapp) Date: Thu, 12 Jul 2007 06:58:04 -0400 Subject: [quiz] call for quizzes, and [QUIZ 4] centering In-Reply-To: <46959625.1080800@gigamonkeys.com> References: <20070711223618.GO10251@santa.theclapp.org> <46959625.1080800@gigamonkeys.com> Message-ID: <20070712105804.GQ10251@santa.theclapp.org> On Wed, Jul 11, 2007 at 07:47:01PM -0700, Peter Seibel wrote: > Larry Clapp wrote: > >In particular: no solutions for 48 hours; if you post one after > >that, include "[SPOILER]" in the subject. > > I'm going to unilaterally change the rules (at least as they apply to me > ;-)) and say that it's okay to send a solution whenever you want as long > as it's got SPOILER in the subject. If I hack up a solution I don't want > to have to remember to come back in two days and send it to the list. Yeah, I can live with that. After I posted the puzzle, I hacked up a solution, and gnashed my teeth that by my own rules I couldn't send it back. :) > So here's my version of CENTER. [snip] Broken. (Hey, if you can change the rules about posting solutions, I can change the rules about judging! ;) Hint: re-read the problem specification. Broken in a common way, though (based on MJD's comments[1] on the solutions to the first Perl quiz), and in the same way as my last almost-correct solution. Your solution highlights some of the possible axes of judging, aka design trade-offs, kind of like Kent is always harping^W reminding us about in c.l.l. Specifically, your solution conses a little more than mine, but since you use reduce and mapcan and I use loop (twice), yours seems "Lispier". -- L [1] http://perl.plover.com/~alias/list.cgi?mss:8:200210:gjgjlgandhobgejokhic From larry at theclapp.org Thu Jul 12 11:15:47 2007 From: larry at theclapp.org (Larry Clapp) Date: Thu, 12 Jul 2007 07:15:47 -0400 Subject: [SPOILER] Re: [quiz] call for quizzes, and [QUIZ 4] centering In-Reply-To: <20070712105804.GQ10251@santa.theclapp.org> References: <20070711223618.GO10251@santa.theclapp.org> <46959625.1080800@gigamonkeys.com> <20070712105804.GQ10251@santa.theclapp.org> Message-ID: <20070712111547.GR10251@santa.theclapp.org> On Thu, Jul 12, 2007 at 06:58:04AM -0400, Larry Clapp wrote: > Your solution highlights some of the possible axes of judging, aka > design trade-offs, kind of like Kent is always harping^W reminding us > about in c.l.l. Specifically, your solution conses a little more than > mine, but since you use reduce and mapcan and I use loop (twice), > yours seems "Lispier". My solution: (defun center (&rest list) (let* ((max-length (loop for item in list maximize (length item))) (center (floor max-length 2))) (loop for item in list for length = (length item) collect (format nil "~v,0T~A" (- center (floor length 2)) item)))) Outputs: QUIZ 15 > (center "this" "is" "a test of the" "center function") (" this" " is" " a test of the" "center function") I realize now that I should simplify my math: (defun center2 (&rest list) (let ((max-length (loop for item in list maximize (length item)))) (loop for item in list collect (format nil "~v,0T~A" (floor (- max-length (length item)) 2) item)))) So far, I like Sean's solution the best. For reference, Sean wrote: (defun center (&rest list) (let ((max (reduce #'max vals :key #'length))) (mapcar #'(lambda (x) (format nil "~,,v at A" (floor (- max (length x)) 2) x)) list))) -- L From ryan at acceleration.net Thu Jul 12 14:41:43 2007 From: ryan at acceleration.net (Ryan Davis) Date: Thu, 12 Jul 2007 10:41:43 -0400 Subject: [quiz] Anybody there? In-Reply-To: <20070711220815.GM10251@santa.theclapp.org> References: <90bf67330707061838s195d5b2h3c88240558c75a82@mail.gmail.com> <469245E0.4050801@acceleration.net> <90bf67330707102035w7878475at69fe85e34ac7a3c2@mail.gmail.com> <20070711125929.GJ10251@santa.theclapp.org> <90bf67330707111407v71859532tf92d705b5b425a94@mail.gmail.com> <20070711220815.GM10251@santa.theclapp.org> Message-ID: <46963DA7.2060102@acceleration.net> I submitted it half-jokingly, but I figured the dataset was small enough that it'd still complete, and the comic makes it a little more fun to dig into. It's also a nice example of NP-complete and the knapsack problem for those not familiar. Thanks, Ryan Davis Acceleration.net Director of Programming Services 2831 NW 41st street, suite B Gainesville, FL 32606 Office: 352-335-6500 x 124 Fax: 352-335-6506 Larry Clapp wrote: > On Wed, Jul 11, 2007 at 04:07:53PM -0500, Ivan Salazar wrote: > >> OK, was this quiz a joke then? u_u >> > > Well, I didn't propose it. I don't know what the guy who suggested it > had in mind, one way or the other. > > I initially thought it would be a hard problem (or at least take a > long time to solve), thus my cautionary remarks, but the solution > posted here earlier took ~0.001 seconds on my machine. I guess this > dataset was sufficiently small. :) > > -- L > > _______________________________________________ > quiz mailing list > quiz at common-lisp.net > http://common-lisp.net/cgi-bin/mailman/listinfo/quiz > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From larry at theclapp.org Thu Jul 19 23:36:14 2007 From: larry at theclapp.org (Larry Clapp) Date: Thu, 19 Jul 2007 19:36:14 -0400 Subject: [quiz] [QUIZ 5b & 5e] Date differences, and betting Message-ID: <20070719233613.GA28351@santa.theclapp.org> 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 ] The local high school baseball team, the Kent Pitman High School Phoenixes, will be playing a series of games against their rivals, the Kenny Tilton Memorial High School Growlin' Fungus. The series lasts at most five games, and ends when one team has won three games. You want to bet $80 on the Phoenixes to win the series, but your bookie will only take bets on individual games. (The bookie pays even money on all bets.) A mathematically-inclined friend solves the problem for you, giving you the following instructions: Bet $30 on each of the first two games. Bet $20 on the third game if either team has won both of the first two games, $40 otherwise. Bet $40 on the fourth game, if there is one. Bet $80 on the fifth game, if there is one. At the end of the series, you will be ahead by exactly $80 if the Phoenixes have won the series, and behind by exactly $80 if the Growlin' Fungus have won. We could summarize the instructions in a table like this: If the game score is: 0 to 0, bet $30 1 to 0, bet 30 1 to 1, bet 40 2 to 0, bet 20 2 to 1, bet 40 2 to 2, bet 80 Write a function which calculates the appropriate bet for any such contest, given the total amount you want to risk on the series, the length of the series, and the current game score. For example, (bet 80 5 2 1) => 40 because if you want to risk $80 on a 5-game series, and one team is presently ahead 2 games to 1, you should bet $40. Similarly (bet 1000 7 2 1) => 375 (That is, if you're trying to bet $1000 on the current [circa 10/2002] World Series baseball match, you need to bet $375 on the outcome of tonight's game. If you started with $1000, and followed this function's advice, you'd have $625 left if you had been betting on the Giants and $1375 if you had been betting on the Angels. For people living in places where the World Series is irrelevant: the match is a best-four-of-seven series of games; at present, the Anaheim Angels are beating the San Francisco Giants two games to one, with the fourth game scheduled for tonight.) Error checking: at minimum, signal an error if any of the parameters are invalid or internally inconsistent (e.g. negatives, length of series is less than number of games played so far, etc). More advanced users can establish useful restarts. From c.groner at gmail.com Tue Jul 24 02:08:33 2007 From: c.groner at gmail.com (Carl) Date: Mon, 23 Jul 2007 19:08:33 -0700 Subject: [quiz] [QUIZ 5b & 5e] Date differences, and betting In-Reply-To: <20070719233613.GA28351@santa.theclapp.org> References: <20070719233613.GA28351@santa.theclapp.org> Message-ID: Wow, no takers, huh. With all the talk that has been going around about lisp lately, this list is surprisingly quiet. I suppose I might as well post my attempt. Keep in mind, i'm new to lisp so it may not be very "lispy". I'm sure someone will shortly make me feel dumb by posting a two liner with better error handling < | :) ---- start ---- (defun date-to-time (date &key (tz nil)) "Convert date to timestamp. Date expected to be formatted as: Sat Jul 20 2007 , using 3 letter abbreviations for day of week and month" (let* ((date-list (read-from-string (concatenate 'string "(" date ")"))) (utime (encode-universal-time 0 0 0 (caddr date-list) (+ (position (cadr date-list) '(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)) 1) (cadddr date-list) tz))) (if (equal (seventh (multiple-value-list (decode-universal-time utime))) (position (car date-list) '(Mon Tue Wed Thu Fri Sat Sun))) utime (error "Supplied day (~S) does not match calculated day of week for ~S)" (first date-list) (rest date-list) )))) (defun days-diff (start end) "Calculate difference in days between the two provided dates. Dates must be in the form accepted by date-to-time" (/ (- (date-to-time end) (date-to-time start)) 60 60 24)) ---- end --- CL-USER> (days-diff "Wed Oct 16 2002" "Wed Oct 23 2002") 7 CL-USER> (days-diff "Wed Oct 16 2002" "Tue Oct 16 2001") -365 CL-USER> (days-diff "Thu Jul 19 2007" "Mon Jul 23 2007") 4 Comments or suggestions welcome. Cheers, Carl. On 7/19/07, Larry Clapp wrote: > 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. > --8<-- message cut -->8-- From ivan.salazarv at gmail.com Tue Jul 24 05:26:48 2007 From: ivan.salazarv at gmail.com (Ivan Salazar) Date: Tue, 24 Jul 2007 00:26:48 -0500 Subject: [quiz] [QUIZ 5b & 5e] Date differences, and betting In-Reply-To: References: <20070719233613.GA28351@santa.theclapp.org> Message-ID: <90bf67330707232226h2a3d1d0ejb202842280bab8b8@mail.gmail.com> 2007/7/23, Carl : > > Wow, no takers, huh. > With all the talk that has been going around about lisp lately, this > list is surprisingly quiet. > > > Uhm... I liked your solution, even though I'm a newbie. Thanks for submitting it, you helped me to learn some new functions. And yes, almost anyone is trying to solve the quiz (me included) but at least reading the few solutions of others is helping me a lot. Off topic: I am reading Paul Graham's ANSI Common Lisp and I have a little question: why does he uses a lot (at least until chapter 4) "and" clauses when I think he should use the "when" macro? For example, in this code snip from figure 4.1: ;; This is a binary search algorithm as you may guess. (defun bin-search (obj vec) (let ((len (length vec))) (and (not (zerop len)) ;Here is the and clause that I may change for a when (finder obj vec 0 (- len 1))))) (defun finder (obj vec start end) ...) I've read that at least three times I think and it seems that there will be more. I know that the and clause gives the desired behavior of returning the last value that satisfies it or the first one that doesn't, but I would have used a when for some of the cases I've read (it is more readable for me, I think). Is he using the and clause for some reason that eludes me or is it just a personal taste? Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From ivan.salazarv at gmail.com Tue Jul 24 05:28:54 2007 From: ivan.salazarv at gmail.com (Ivan Salazar) Date: Tue, 24 Jul 2007 00:28:54 -0500 Subject: [quiz] [QUIZ 5b & 5e] Date differences, and betting In-Reply-To: <90bf67330707232226h2a3d1d0ejb202842280bab8b8@mail.gmail.com> References: <20070719233613.GA28351@santa.theclapp.org> <90bf67330707232226h2a3d1d0ejb202842280bab8b8@mail.gmail.com> Message-ID: <90bf67330707232228j315faa58p7b8192a83374bcc@mail.gmail.com> 2007/7/24, Ivan Salazar : > > > > 2007/7/23, Carl : > > > > Wow, no takers, huh. > > With all the talk that has been going around about lisp lately, this > > list is surprisingly quiet. > > > > > > > Uhm... I liked your solution, even though I'm a newbie. Thanks for > submitting it, you helped me to learn some new functions. > And yes, almost anyone is trying to [...] That should be: "And yes, almost no-one is trying to [...]". Sorry, a stupid typo. -------------- next part -------------- An HTML attachment was scrubbed... URL: From larry at theclapp.org Tue Jul 24 13:17:11 2007 From: larry at theclapp.org (Larry Clapp) Date: Tue, 24 Jul 2007 09:17:11 -0400 Subject: [quiz] [QUIZ 5b & 5e] [SPOILER] Date differences, and betting In-Reply-To: <90bf67330707232226h2a3d1d0ejb202842280bab8b8@mail.gmail.com> References: <20070719233613.GA28351@santa.theclapp.org> <90bf67330707232226h2a3d1d0ejb202842280bab8b8@mail.gmail.com> Message-ID: <20070724131711.GC4286@santa.theclapp.org> This message is two replies in one. On Tue, Jul 24, 2007 at 12:26:48AM -0500, Ivan Salazar wrote: > 2007/7/23, Carl : > >Wow, no takers, huh. > >With all the talk that has been going around about lisp lately, > >this list is surprisingly quiet. Carl, I didn't get your email and can't find it in my spam folder. Bummer. But I did find it in the archive: > (defun date-to-time (date &key (tz nil)) > "Convert date to timestamp. Date expected to be formatted as: Sat Jul 20 2007 , > using 3 letter abbreviations for day of week and month" > (let* ((date-list (read-from-string (concatenate 'string "(" date ")"))) > (utime (encode-universal-time 0 0 0 (caddr date-list) > (+ (position (cadr date-list) > '(Jan Feb Mar Apr May Jun > Jul Aug Sep Oct Nov Dec)) > 1) > (cadddr date-list) > tz))) > (if (equal (seventh (multiple-value-list (decode-universal-time utime))) > (position (car date-list) '(Mon Tue Wed Thu Fri Sat Sun))) > utime > (error "Supplied day (~S) does not match calculated day of week for ~S)" > (first date-list) (rest date-list) )))) > > > (defun days-diff (start end) > "Calculate difference in days between the two provided dates. Dates > must be in the form accepted by date-to-time" > (/ (- (date-to-time end) (date-to-time start)) 60 60 24)) > > ---- end --- > > CL-USER> (days-diff "Wed Oct 16 2002" "Wed Oct 23 2002") > 7 > CL-USER> (days-diff "Wed Oct 16 2002" "Tue Oct 16 2001") > -365 > CL-USER> (days-diff "Thu Jul 19 2007" "Mon Jul 23 2007") > 4 > > Comments or suggestions welcome. Your solution seems quite workable, and certainly does the job. I like the way you split the string into lists of atoms using read-from-string. I've seen that idiom before, but forgot about it. I used split-sequence in my solution. Suggestions: o Instead of cadr, caddr, and cadddr, use second, third, and fourth. o Instead of embedding the lists of months and days, use a separately defined constant. (On the other hand, this is a style issue and quite debatable, especially for such a small, throw-away problem.) o Instead of (seventh (multiple-value-list ...)) use nth-value. (I did the same thing at first, though. :) Thanks for posting! Remember to put [SPOILER] in your subject when you include a solution. Ivan said: > Uhm... I liked your solution, even though I'm a newbie. Thanks for > submitting it, you helped me to learn some new functions. > And yes, almost no-one is trying to solve the quiz (me included) but ^^^^^^ fixed > at least reading the few solutions of others is helping me a lot. Well, all we *really* know is that no-one is submitting solutions. :) I worked up a solution shortly after I posted the problem, but then wanted to add some more sophisticated condition handling, and I'm not all that familiar with the condition system, so I had to do some reading, and then Life intervened. :) > Off topic: I am reading Paul Graham's ANSI Common Lisp and I have a > little question: why does he uses a lot (at least until chapter 4) > "and" clauses when I think he should use the "when" macro? For > example, in this code snip from figure 4.1: > > ;; This is a binary search algorithm as you may guess. > (defun bin-search (obj vec) > (let ((len (length vec))) > (and (not (zerop len)) ;Here is the and clause that I may change for > ;a when > (finder obj vec 0 (- len 1))))) > > (defun finder (obj vec start end) > ...) > > I've read that at least three times I think and it seems that there > will be more. > I know that the and clause gives the desired behavior of returning > the last value that satisfies it or the first one that doesn't, but > I would have used a when for some of the cases I've read (it is more > readable for me, I think). > Is he using the and clause for some reason that eludes me or is it > just a personal taste? In many cases I think it's just personal taste, i.e., a style issue like the one I mentioned above. But maybe not. If you consider the published code as a snapshot of a real application, some method to this madness emerges (maybe). In particular, consider the code over time. Perhaps he started out with (and (not (zerop len)) (other code) (finder obj vec 0 (- len 1))) and then deleted the second clause. Maybe he didn't want to change it. Maybe he thought he might, at some later date, have to add some more code in the middle. Once you shift back and forth between (when (foo) (baz)) and (when (and (foo) (bar)) (baz)) enough times, you start to want to skip the middle man and just go for (and (foo) (baz)) every time, in case you have to add the middle (bar) later. Or so it seems to me, anyway. :) I haven't really thought about it all that much, but I might use the following rule of thumb: Start out with (when (foo) (baz)) The first time you have to change it to (when (and (foo) (bar)) (baz)) change it to (and (foo) (bar) (baz)) instead and leave it that way forever after. If it has changed once, it'll probably change again. :) -- Larry From ivan.salazarv at gmail.com Tue Jul 24 19:22:35 2007 From: ivan.salazarv at gmail.com (Ivan Salazar) Date: Tue, 24 Jul 2007 14:22:35 -0500 Subject: [quiz] [QUIZ 5b & 5e] [SPOILER] Date differences, and betting In-Reply-To: <20070724131711.GC4286@santa.theclapp.org> References: <20070719233613.GA28351@santa.theclapp.org> <90bf67330707232226h2a3d1d0ejb202842280bab8b8@mail.gmail.com> <20070724131711.GC4286@santa.theclapp.org> Message-ID: <90bf67330707241222k6cc6819em9012796045b094e7@mail.gmail.com> > > > Off topic: I am reading Paul Graham's ANSI Common Lisp and I have a > > little question: why does he uses a lot (at least until chapter 4) > > "and" clauses when I think he should use the "when" macro? For > > example, in this code snip from figure 4.1: > > > > ;; This is a binary search algorithm as you may guess. > > (defun bin-search (obj vec) > > (let ((len (length vec))) > > (and (not (zerop len)) ;Here is the and clause that I may change for > > ;a when > > (finder obj vec 0 (- len 1))))) > > > > (defun finder (obj vec start end) > > ...) > > > > I've read that at least three times I think and it seems that there > > will be more. > > I know that the and clause gives the desired behavior of returning > > the last value that satisfies it or the first one that doesn't, but > > I would have used a when for some of the cases I've read (it is more > > readable for me, I think). > > Is he using the and clause for some reason that eludes me or is it > > just a personal taste? > > In many cases I think it's just personal taste, i.e., a style issue > like the one I mentioned above. > > But maybe not. If you consider the published code as a snapshot of a > real application, some method to this madness emerges (maybe). In > particular, consider the code over time. Perhaps he started out with > > (and (not (zerop len)) > (other code) > (finder obj vec 0 (- len 1))) > > and then deleted the second clause. Maybe he didn't want to change > it. Maybe he thought he might, at some later date, have to add some > more code in the middle. Once you shift back and forth between > > (when (foo) > (baz)) > > and > > (when (and (foo) (bar)) > (baz)) > > enough times, you start to want to skip the middle man and just go for > > (and (foo) > (baz)) > > every time, in case you have to add the middle (bar) later. > > Or so it seems to me, anyway. :) > > I haven't really thought about it all that much, but I might use the > following rule of thumb: Start out with > > (when (foo) > (baz)) > > The first time you have to change it to > > (when (and (foo) (bar)) > (baz)) > > change it to > > (and (foo) > (bar) > (baz)) > > instead and leave it that way forever after. If it has changed once, > it'll probably change again. :) > > -- Larry > > _______________________________________________ > quiz mailing list > quiz at common-lisp.net > http://common-lisp.net/cgi-bin/mailman/listinfo/quiz > Outstanding. Thanks for the answer, Larry. I was starting to think that was the answer, but you stated it very clear. -------------- next part -------------- An HTML attachment was scrubbed... URL: From chiao at cal.berkeley.edu Wed Jul 25 01:34:15 2007 From: chiao at cal.berkeley.edu (Chiao-Lun Cheng) Date: Wed, 25 Jul 2007 09:34:15 +0800 Subject: [quiz] [QUIZ 5b & 5e] Date differences, and betting (Larry Clapp) [spoiler] In-Reply-To: <208121240707241751o1b26f597t167d3b5014df021c@mail.gmail.com> References: <208121240707241751o1b26f597t167d3b5014df021c@mail.gmail.com> Message-ID: <208121240707241834h3d3dda15yb86120ea744da277@mail.gmail.com> 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 and therefore a = (b+c)/2 That's the recursion that took me some time to realize. Otherwise the coding is straightforward. -- ******************************************* 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 at mit.edu Tel : +1 (617) 253-1539 Website : http://zeno.unixboxen.net *******************************************