Author Topic: combinations  (Read 1747 times)

0 Members and 1 Guest are viewing this topic.

Peter Guappa

  • Guest
combinations
« on: July 14, 2015, 03:08:58 PM »
Given a list of n-integers (no duplicates)
Result: a list containing all unique combinations of 3 integers

eg (1 2 3 4 5) => ((1 2 3) (1 2 4) (1 2 5) (2 3 4) (2 3 5) (3 4 5))

any suggestions?
Thanks

kpblc

  • Bull Frog
  • Posts: 396
Re: combinations
« Reply #1 on: July 14, 2015, 03:51:50 PM »
something like this?
Code - Auto/Visual Lisp: [Select]
  1. (setq lst '(1 2 3 4 5 6 7))
  2.  
  3. (defun test (lst)
  4.   (cond
  5.     ((= (length lst) 3) (list lst))
  6.     ((> (length lst) 3)
  7.      (append (mapcar
  8.                (function
  9.                  (lambda (x)
  10.                    (list (car lst) (cadr lst) x)
  11.                    ) ;_ end of lambda
  12.                  ) ;_ end of function
  13.                (cddr lst)
  14.                ) ;_ end of mapcar
  15.              (test (cdr lst))
  16.              ) ;_ end of append
  17.      )
  18.     ) ;_ end of cond
  19.   ) ;_ end of defun
Sorry for my English.

ronjonp

  • Needs a day job
  • Posts: 7529
Re: combinations
« Reply #2 on: July 14, 2015, 04:08:46 PM »
Another:

Code - Auto/Visual Lisp: [Select]
  1. (defun _foo (l / i out tmp)
  2.   (while l
  3.     (setq i   (car l)
  4.           l   (cdr l)
  5.           tmp (cdr l)
  6.     )
  7.     (while tmp (setq out (cons (list i (car l) (car tmp)) out)) (setq tmp (cdr tmp)))
  8.   )
  9.   (reverse out)
  10. )
  11. (_foo '(0 1 2 3 4 5 6))
  12. ;; ((0 1 2) (0 1 3) (0 1 4) (0 1 5) (0 1 6) (1 2 3) (1 2 4) (1 2 5) (1 2 6) (2 3 4) (2 3 5) (2 3 6) (3 4 5) (3 4 6) (4 5 6))
« Last Edit: July 14, 2015, 04:12:03 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

Peter Guappa

  • Guest
Re: combinations
« Reply #3 on: July 14, 2015, 05:17:44 PM »
Thanks guys  :-)


ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: combinations
« Reply #5 on: July 16, 2015, 04:55:43 AM »
I got different results  :-(
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l)
  2.   (if (cddr l)
  3.            (cons (f (cdr l))
  4.                  (mapcar (function
  5.                            (lambda (b) (mapcar (function (lambda (a) (list (car l) b a))) (cdr (member b l))))
  6.                          )
  7.                          (cdr l)
  8.                  )
  9.            )
  10.     )
  11.   )
  12. )


Code - Auto/Visual Lisp: [Select]
  1. (f '(1 2 3 4 5))
  2. =>> ((3 4 5) (2 3 4) (2 3 5) (2 4 5) (1 2 3) (1 2 4) (1 2 5) (1 3 4) (1 3 5) (1 4 5))
  3. (_foo  '(1 2 3 4 5))
  4. =>> ((1 2 3) (1 2 4) (1 2 5) (2 3 4) (2 3 5) (3 4 5))
  5. (test  '(1 2 3 4 5))
  6. =>> ((1 2 3) (1 2 4) (1 2 5) (2 3 4) (2 3 5) (3 4 5))

Is it true I realized exercise?  :-o
« Last Edit: July 16, 2015, 05:55:13 AM by ElpanovEvgeniy »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: combinations
« Reply #6 on: July 16, 2015, 06:16:24 AM »
variant to sorted lists:

Code - Auto/Visual Lisp: [Select]
  1. (defun f (l)
  2.   (if (cddr l)
  3.            (append (mapcar (function
  4.                              (lambda (b) (mapcar (function (lambda (a) (list (car l) b a))) (cdr (member b l))))
  5.                            )
  6.                            (cdr l)
  7.                    )
  8.                    (list (f (cdr l)))
  9.            )
  10.     )
  11.   )
  12. )

Test:
Code - Auto/Visual Lisp: [Select]
  1. (f '(1 2 3 4 5))
  2. =>>((1 2 3) (1 2 4) (1 2 5) (1 3 4) (1 3 5) (1 4 5) (2 3 4) (2 3 5) (2 4 5) (3 4 5))

ronjonp

  • Needs a day job
  • Posts: 7529
Re: combinations
« Reply #7 on: July 16, 2015, 09:03:08 AM »
I got different results  :(
Code - Auto/Visual Lisp: [Select]
  1. (defun f (l)
  2.   (if (cddr l)
  3.            (cons (f (cdr l))
  4.                  (mapcar (function
  5.                            (lambda (b) (mapcar (function (lambda (a) (list (car l) b a))) (cdr (member b l))))
  6.                          )
  7.                          (cdr l)
  8.                  )
  9.            )
  10.     )
  11.   )
  12. )


Code - Auto/Visual Lisp: [Select]
  1. (f '(1 2 3 4 5))
  2. =>> ((3 4 5) (2 3 4) (2 3 5) (2 4 5) (1 2 3) (1 2 4) (1 2 5) (1 3 4) (1 3 5) (1 4 5))
  3. (_foo  '(1 2 3 4 5))
  4. =>> ((1 2 3) (1 2 4) (1 2 5) (2 3 4) (2 3 5) (3 4 5))
  5. (test  '(1 2 3 4 5))
  6. =>> ((1 2 3) (1 2 4) (1 2 5) (2 3 4) (2 3 5) (3 4 5))

Is it true I realized exercise?  :o
Knowing your skills I bet you're right  8)  I just put something together that matched the OP's list and desired result.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC