You should be automatically redirected . If not, visit
and update your bookmarks.


1 + 2 = 3

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     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.


At 20:28, Blogger sarken said...

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 ...

At 20:36, Blogger Peter said...

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


At 21:12, Blogger don Lucio said...

More solutions overe here:
int the Quizzes section.

At 21:37, Blogger rschmutz said...

This comment has been removed by the author.

At 21:38, Blogger rschmutz said...

beeing a lisp-novice my code probably looks
very ugly to the experts eye. I tried to seperate
the calculation/logic from the presentation --
the "presentation" part lacks a lot in comparison
to 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)
(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)

At 21:50, Anonymous Anonymous said...

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


Post a Comment

Links to this post:

Create a Link

<< Home