(archive 'newLISPer)

November 1, 2006

1 + 2 = 3

Filed under: newLISP — newlisper @ 17:11
Tags:

One of the good things about the Ruby programming language is how much activity the community generates for promoting and exploring the language. One of the cool things they get up to is the Ruby Quiz. This is a weekly problem-solving ‘contest’: a problem is set on Friday and Ruby-ists submit their solutions before the following Thursday, when the results are discussed on the list. No winners, just solutions varying in length, speed, and elegance.

I believe that there are similar rolling quizzes for other languages, including Perl and Haskell. Unfortunately I don’t think that there are enough newLISP users out there (yet) for something similar, and it would also need an administrator with a good knowledge of the language, a spare web site, a mailing list, and quite a lot of time as well…

I bumped into one of their problems the other day, while looking for something else. How to print out one of these, Pascal’s triangle:

                            1
                         1     1
                      1     2     1
                   1     3     3     1
                1     4     6     4     1
             1     5    10    10     5     1
          1     6    15    20    15     6     1
       1     7    21    35    35    21     7     1
    1     8    28    56    70    56    28     8     1
 1     9    36    84    126   126   84    36     9     1

You might enjoy reading the Ruby solutions here.

I would have tried to produce a newLISP solution myself. But before I could muster up the necessary brain power to make an attempt, I discovered that Peter had got there before me. And his solution looks like it would be hard to improve.

Advertisements

5 Comments »

  1. >I looked at Peter’s solution and I gotta say that Pascal’s triangle screams for a dynamic programming solution than a recursive one.I wrote a dynamic programming version and it’s an order of magnitude faster.26985 ms vs 2643 ms for 100 reps to depth 34.If you want to start a quiz page, perhaps newLISP on Noodles is a good place. I even made you a place holder. :)And yes, that means we’re live …

    Comment by sarken — November 1, 2006 @ 20:28 | Reply

  2. >The biggest trouble was printing the results in a readable manner… probably Don Lucio is able to improve the program even more!Pjot

    Comment by Peter — November 2, 2006 @ 20:36 | Reply

  3. >More solutions overe here:http://newlisp-on-noodles.org/int the Quizzes section.

    Comment by don Lucio — November 3, 2006 @ 21:12 | Reply

  4. >beeing a lisp-novice my code probably looksvery ugly to the experts eye. I tried to seperatethe calculation/logic from the presentation –the “presentation” part lacks a lot in comparisonto the other presented versions :)(define (pascal n l) (let ((pascal_step (lambda (l) (map ‘+ (append ‘(0) l) (append l ‘(0)))))) (if (= 0 n) (list l) (cons l (pascal (- n 1) (pascal_step l))))))(define (center_print n s) (let ((str (replace “)” (replace “(” (string s) “”) “”))) (println (string (dup ” ” (- n (/ (length str) 2)))) str)))(define (pascal_triangle n) (silent (set ‘triangle (pascal n ‘(1))) (set ‘center (/ (length (string (last triangle))) 2)) (set ‘printit (lambda (s) (center_print center s))) (map ‘printit triangle))); execute by “(pascal_triangle 10)” or so (there is; probably a obi-wan error in the code (= 1 n); would probably be more approriate)

    Comment by rschmutz — May 31, 2007 @ 20:38 | Reply

  5. >rschmutz – your solution looks very good to me! Of course, beauty and execution speed are sometimes seen together, but too often are seen separately… :-)– newlisper

    Comment by Anonymous — May 31, 2007 @ 20:50 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: