Jump to content

Recommended Posts

Posted

Please publish a function that creates combinations.

List of any length.

Example:

'(1 2 3 4 5)

The result for the 3-element subsets.

    '(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)

 

 

The result for the 4-element subsets.

    '(1     2     3     4)
    (1     2     3     5)
    (1     2     4     5)
    (1     3     4     5)
    (2     3     4     5)

Posted
(defun nCr ( l r )
   (cond
       (   (< r 2)
           (mapcar 'list l)
       )
       (   l
           (append
               (mapcar '(lambda ( x ) (cons (car l) x)) (nCr (cdr l) (1- r)))
               (nCr (cdr l) r)
           )
       )
   )
)

_$ (ncr '(1 2 3 4 5) 3)
((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))

Posted

Dear Lee Mac

 

How to find all combinations from list of numbers that have sum of each of combination below specific number.

For example :

1. List (1 2 3 4 5)

2. Maximum sum of each combination: 6

 

So the result is : ((1 5) (2 4) (1 2 3))

 

Thank you.

 

m4rdy

Posted
How to find all combinations from list of numbers that have sum of each of combination below specific number.

For example :

1. List (1 2 3 4 5)

2. Maximum sum of each combination: 6

 

So the result is : ((1 5) (2 4) (1 2 3))

 

See my post here.

 

Example:

_$ (lst< '(1 2 3 4 5) 7)
((1 2 3) (1 2) (1 3) (1 4) (1 5) (2 3) (2 4) (1) (2) (3) (4) (5))

Or, for equality:

(defun lst= ( l n )
   (vl-remove-if-not '(lambda ( x ) (= (sum x) n)) (combinations l))
)

_$ (lst= '(1 2 3 4 5) 6)
((1 2 3) (1 5) (2 4))

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...