Author Topic: [Challenge] Pascal's triangle  (Read 11082 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: [Challenge] Pascal's triangle
« Reply #15 on: January 14, 2011, 04:49:02 PM »
note to self: probably shouldn't share "the voices"

doh, did it again

:-D
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: [Challenge] Pascal's triangle
« Reply #16 on: January 14, 2011, 05:34:26 PM »
note to self: probably shouldn't share "the voices"

doh, did it again

:-D

I undestand about the 'voices' ....
I've started to discuss issues with one of the back verandah posts ... If it ignores me I don't post critiques ..
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: [Challenge] Pascal's triangle
« Reply #17 on: January 16, 2011, 01:23:37 PM »
lol, figured out another by accident (was working on some thing else and went 'hey wait a second'):

Code: [Select]
def pascals_triangle(n):
    # python 3.x
    x=[[1]]
    for i in range(n-1):
        x.append(list(map(sum,zip([0]+x[-1],x[-1]+[0]))))
    return x

python
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: [Challenge] Pascal's triangle
« Reply #18 on: January 16, 2011, 02:30:55 PM »
works too:

Code: [Select]
def pascals_triangle(n):
    x=[[1]] ; f = lambda *a: list(map(sum,zip(*a)))
    for i in range(n-1): x.append(f([0]+x[-1], x[-1]+[0]))
    return x
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: [Challenge] Pascal's triangle
« Reply #19 on: January 17, 2011, 03:01:22 PM »
Another F#

Code: [Select]
let rec foo l =
    match l with
    | []      -> []
    | h :: [] -> [1]
    | h :: t  -> h + t.Head :: foo t
   
let pascalTri n = List.scan(fun l i -> 1 :: foo l) [1] [1 .. n]

let pascalRow n = List.fold(fun l i -> 1 :: foo l) [1] [1 .. n]

Works too with :
Code: [Select]
let rec foo (l : int list) =
    if l.Tail = [] then [1] else l.Head + l.Tail.Head :: foo l.Tail
Speaking English as a French Frog

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: [Challenge] Pascal's triangle
« Reply #20 on: January 19, 2011, 07:40:18 AM »
my new version NTH:
Code: [Select]
(defun f_nth (n / f)
 (defun f (a b c)
  (if (>= b 0)
   (cons a (f (* a (/ b c)) (1- b) (1+ c)))
  )
 )
 (f 1. n 1.)
)


Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: [Challenge] Pascal's triangle
« Reply #21 on: January 19, 2011, 09:01:08 AM »
Fantastic Evgeniy!

A much better method to simplify the factorials than what I did here:-)

Here's how I understand your code:
Code: [Select]
Binomial Coefficient is given by:

   n!
--------
k!(n-k)!

Your code operating on the (k-1)th iteration:

       n!              n-(k-1)         n!
----------------  x   ---------  =  --------
(k-1)!(n-(k-1))!          k         k!(n-k)!

« Last Edit: January 19, 2011, 10:10:39 AM by Lee Mac »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: [Challenge] Pascal's triangle
« Reply #22 on: January 19, 2011, 08:26:18 PM »
cool thread is cool 8-)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

chlh_jd

  • Guest
Re: [Challenge] Pascal's triangle
« Reply #23 on: January 20, 2011, 12:05:02 PM »
Great!
I found (f 1. n 1.)
 
that the dot is very important,othewise it will take a mistake 'negative' results ,
just like
Code: [Select]
(pascalrow 1000)

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: [Challenge] Pascal's triangle
« Reply #24 on: February 15, 2011, 01:33:22 PM »
Some new ways implementing fold, scan and unfold F# functions in LISP.

EDIT: new definitions so that the functions require a *quoted* function as argument (as others 'higher order' LISP functions as apply, mapcar, ...)

gc:fold
Code: [Select]
;|
gc:fold
Returns the final state of an accumulator which initial state is changed
by applying a function to the accumulator and each item of a list.

Arguments
fun: a function (lambda or defun) which takes a state and a list item as arguments
     and returns the modified state
acc : accumulator initial state
lst ; list
|;

(defun gc:fold (fun acc lst)
  (foreach n lst (setq acc ((eval fun) acc n)))
)

gc:scan
Code: [Select]
;|
gc:scan
Returns the list of all intermediates states mor final state of an accumulator
which initial state is changed by applying a function to the accumulator and
each item of a list.

Arguments
fun: a function (lambda or defun) which takes a state and a list item as arguments
     and returns the modified state
acc : accumulator initial state
lst ; list
|;

(defun gc:scan (fun acc lst)
  (cons acc (mapcar '(lambda (x) (setq acc ((eval fun) acc x))) lst))
)

gc:unfold
Code: [Select]
;|
gc:unfold
Generates a list from a calculation function which takes a state
and modify it to product the next item.

Arguments
fun: a function (lambda or defun) which takes a state as argument and
     returns a list of type: (result next_state) or nil (requiered stop condition).
state: the initial state
|;

(defun gc:unfold (fun state)
  ((lambda (r)
     (if r
       (cons (car r) (gc:unfold fun (cadr r)))
     )
   )
    ((eval fun) state)
  )
)

Code: [Select]
(gc:fold '(lambda (a l) (mapcar '+ (cons 0 a) (append a '(0)))) '(1) '(1 2 3 4 5))returns: (1 5 10 10 5 1)

Code: [Select]
(gc:scan '(lambda (a l) (mapcar '+ (cons 0 a) (append a '(0)))) '(1) '(1 2 3 4 5))returns: ((1) (1 1) (1 2 1) (1 3 3 1) (1 4 6 4 1) (1 5 10 10 5 1))

Code: [Select]
(gc:unfold '(lambda (a)
      (if (<= (length a) 6)
(list a (mapcar '+ (cons 0 a) (append a '(0))))
      )
    )
   '(1)
)
returns: ((1) (1 1) (1 2 1) (1 3 3 1) (1 4 6 4 1) (1 5 10 10 5 1))
« Last Edit: March 04, 2011, 04:33:39 PM by gile »
Speaking English as a French Frog

chlh_jd

  • Guest
Re: [Challenge] Pascal's triangle
« Reply #25 on: February 16, 2011, 08:22:50 AM »
Excellent Gile !
how can change the following code into 'gc:unfold method ?

Code: [Select]
(defun get-Ndim-IA-lst (l / a b c d e i)
  (if (< (length l) 2)
    l
    (progn
      (setq a nil
   i 1
      )
      (while (setq b (nth i l))
(if (null a)
 (setq a (car l))
)
(setq e nil)
(foreach c b
 (foreach d a
   (if (<= (if (listp c)
 (car c)
 c
)
(if (listp d)
 (car d)
 d
)
)
     (setq e
    (cons
      (if (and (null (listp c)) (listp d))
(cons c d)
(if (and (null (listp c)) (null (listp d)))
  (list c d)
  (append c d)
)
      )
      e
    )
     )
   )
 )
)
(setq a e)
(setq i (1+ i))
      )
      a
    )
  )
)
arg :
Quote
Code: [Select]
(setq l '((0.95 0.9 0.85 0.8 0.75 0.7 0.65 0.6 0.55 0.5) (0.95 0.9 0.85 0.8 0.75 0.7 0.65 0.6 0.55 0.5 0.45)
 (0.95 0.9 0.85 0.8 0.75 0.7 0.65 0.6 0.55 0.5 0.45 0.4) (0.95 0.9 0.85 0.8 0.75 0.7 0.65 0.6 0.55 0.5 0.45 0.4 0.35)
 (0.95 0.9 0.85 0.8 0.75 0.7 0.65 0.6 0.55 0.5 0.45 0.4 0.35 0.3)))
Returns :
Quote
Code: [Select]
((0.3 0.8 0.85 0.9 0.95) (0.3 0.75 0.8 0.9 0.95) (0.3 0.75 0.8 0.85 0.95) (0.3 0.75 0.8 0.85 0.9)
(0.3 0.75 0.85 0.9 0.95) (0.3 0.7 0.75 0.9 0.95) (0.3 0.7 0.75 0.85 0.95)...)
« Last Edit: February 16, 2011, 09:19:54 AM by chlh_jd »

krampaul82

  • Guest
Re: [Challenge] Pascal's triangle
« Reply #26 on: February 24, 2011, 05:25:29 PM »
Sigh..... :-(

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: [Challenge] Pascal's triangle
« Reply #27 on: February 24, 2011, 07:13:01 PM »