Each column corresponds to each column, all combinations?

Each column corresponds to each column, all combinations?

463017170
Advocate Advocate
422 Views
3 Replies
Message 1 of 4

Each column corresponds to each column, all combinations?

463017170
Advocate
Advocate

(list
(a1 a2 a3)
(b1 b2 b3 b4)
(c1 c2 )
)

 

Each column corresponds to each column, all combinations?

 

result:

(a1 b1 c1)
(a1 b2 c1)
(a1 b3 c1)
(a1 b4 c1)
(a1 b1 c2)
(a1 b2 c2)
(a1 b3 c2)
(a1 b4 c2)
(a2 b1 c1)
(a2 b2 c1)
(a2 b3 c1)
(a2 b4 c1)
(a2 b1 c2)
(a2 b2 c2)
(a2 b3 c2)
(a2 b4 c2)
(a3 b1 c1)
(a3 b2 c1)
(a3 b3 c1)
(a3 b4 c1)
(a3 b1 c2)
(a3 b2 c2)
(a3 b3 c2)
(a3 b4 c2)

。。。。。。

 

Is it faster and simpler?

(defun sol1 (l / r) (setq r (mapcar'list (car l))) (while (cdr l) (setq rr nil) (foreach a (cadr l) (setq rr (cons (mapcar'(lambda (x)(cons a x)) r) rr)) ) (setq r (apply 'append rr) l (cdr l)) ) (princ r) )

0 Likes
Accepted solutions (1)
423 Views
3 Replies
Replies (3)
Message 2 of 4

ВeekeeCZ
Consultant
Consultant
Accepted solution

Probably very similar to yours.

 

(defun :allcomb (lst / out)
  
  (foreach sub (reverse lst)
    (setq out (if (not out)
		(mapcar '(lambda (s) (list s)) sub)
		(apply 'append (mapcar '(lambda (s) (mapcar '(lambda (o) (cons s o)) out)) sub)))))
  out)
0 Likes
Message 3 of 4

463017170
Advocate
Advocate

;;;(test2 28 '(8 3 5.5 4.5 ))
;;;(8 3 5.5 4.5 2.5) x (0 1 0 0 10) = 28.0 -1
;;;(8 3 5.5 4.5 2.5) x (0 0 1 0 9) = 28.0 -2
;;;(8 3 5.5 4.5 2.5) x (1 0 0 0 😎 = 28.0 -3
;;;(8 3 5.5 4.5 2.5) x (0 2 0 1 7) = 28.0 -4
;;;(8 3 5.5 4.5 2.5) x (0 1 1 1 6) = 28.0 -5
;;;(8 3 5.5 4.5 2.5) x (0 0 2 1 5) = 28.0 -6
;;;(8 3 5.5 4.5 2.5) x (1 1 0 1 5) = 28.0 -7
;;;(8 3 5.5 4.5 2.5) x (0 0 0 4 4) = 28.0 -8
;;;(8 3 5.5 4.5 2.5) x (0 3 0 2 4) = 28.0 -9
;;;(8 3 5.5 4.5 2.5) x (1 0 1 1 4) = 28.0 -10
;;;(8 3 5.5 4.5 2.5) x (0 6 0 0 4) = 28.0 -11
;;;(8 3 5.5 4.5 2.5) x (0 2 1 2 3) = 28.0 -12
;;;(8 3 5.5 4.5 2.5) x (2 0 0 1 3) = 28.0 -13
;;;(8 3 5.5 4.5 2.5) x (0 5 1 0 3) = 28.0 -14
;;;(8 3 5.5 4.5 2.5) x (0 1 2 2 2) = 28.0 -15
;;;(8 3 5.5 4.5 2.5) x (1 2 0 2 2) = 28.0 -16
;;;(8 3 5.5 4.5 2.5) x (0 4 2 0 2) = 28.0 -17
;;;(8 3 5.5 4.5 2.5) x (1 5 0 0 2) = 28.0 -18
;;;(8 3 5.5 4.5 2.5) x (0 1 0 5 1) = 28.0 -19
;;;(8 3 5.5 4.5 2.5) x (0 4 0 3 1) = 28.0 -20
;;;(8 3 5.5 4.5 2.5) x (0 0 3 2 1) = 28.0 -21
;;;(8 3 5.5 4.5 2.5) x (1 1 1 2 1) = 28.0 -22
;;;(8 3 5.5 4.5 2.5) x (0 7 0 1 1) = 28.0 -23
;;;(8 3 5.5 4.5 2.5) x (0 3 3 0 1) = 28.0 -24
;;;(8 3 5.5 4.5 2.5) x (1 4 1 0 1) = 28.0 -25
;;;(8 3 5.5 4.5 2.5) x (0 0 1 5 0) = 28.0 -26
;;;(8 3 5.5 4.5 2.5) x (0 3 1 3 0) = 28.0 -27
;;;(8 3 5.5 4.5 2.5) x (1 0 2 2 0) = 28.0 -28
;;;(8 3 5.5 4.5 2.5) x (2 1 0 2 0) = 28.0 -29
;;;(8 3 5.5 4.5 2.5) x (0 6 1 1 0) = 28.0 -30
;;;(8 3 5.5 4.5 2.5) x (0 2 4 0 0) = 28.0 -31
;;;(8 3 5.5 4.5 2.5) x (1 3 2 0 0) = 28.0 -32
;;;(8 3 5.5 4.5 2.5) x (2 4 0 0 0) = 28.0 -33

;;; Completion time 0.05 seconds

0 Likes
Message 4 of 4

Kent1Cooper
Consultant
Consultant

@463017170 wrote:

;;;(test2 28 '(8 3 5.5 4.5 ))
;;;(8 3 5.5 4.5 2.5) x (0 1 0 0 10) = 28.0 -1
;;;(8 3 5.5 4.5 2.5) x (0 0 1 0 9) = 28.0 -2
....


I don't understand how this relates to Message 1.  The "result" doesn't look anything like the result there.  There's no (test2) function defined in this Topic.  Where did the 2.5 added to the initial list come from in all the lower lists?  Where are the second lists coming from, such as the (0 1 0 0 10)?  What do the negative numbers at the ends mean?  Why are there 33 result lines?

 

Meanwhile, I notice curious differences between @463017170's (sol1) function and @ВeekeeCZ's (:allcomb) function, using an arbitrary list of sub-lists that I built:

 

Command: (sol1 '((1 2 3) ("A" "B" "C" "D") (8 7)))
((7 D 1) (7 D 2) (7 D 3) (7 C 1) (7 C 2) (7 C 3) (7 B 1) (7 B 2) (7 B 3) (7 A 1) (7 A 2) (7 A 3) (8 D 1) (8 D 2) (8 D 3) (8 C 1) (8 C 2) (8 C 3) (8 B 1) (8 B 2) (8 B 3) (8 A 1) (8 A 2) (8 A 3))

((7 "D" 1) (7 "D" 2) (7 "D" 3) (7 "C" 1) (7 "C" 2) (7 "C" 3) (7 "B" 1) (7 "B" 2) (7 "B" 3) (7 "A" 1) (7 "A" 2) (7 "A" 3) (8 "D" 1) (8 "D" 2) (8 "D" 3) (8 "C" 1) (8 "C" 2) (8 "C" 3) (8 "B" 1) (8 "B" 2) (8 "B" 3) (8 "A" 1) (8 "A" 2) (8 "A" 3))

 

Note the two lists returned, the first one without the quotation marks around the letter items.  And note the reversal of the elements in the returned sub-lists from those in the source sub-lists.  But (:allcomb) seems to do what's apparently intended in Message 1 [given that an 8 followed by a right parenthesis is converted to an emoji by "the system", which I've complained about several times]:

 

Command: (:allcomb '((1 2 3) ("A" "B" "C" "D") (8 7)))
((1 "A" 😎 (1 "A" 7) (1 "B" 😎 (1 "B" 7) (1 "C" 😎 (1 "C" 7) (1 "D" 😎 (1 "D" 7) (2 "A" 😎 (2 "A" 7) (2 "B" 😎 (2 "B" 7) (2 "C" 😎 (2 "C" 7) (2 "D" 😎 (2 "D" 7) (3 "A" 😎 (3 "A" 7) (3 "B" 😎 (3 "B" 7) (3 "C" 😎 (3 "C" 7) (3 "D" 😎 (3 "D" 7))

Kent Cooper, AIA
0 Likes