Kowal Posted February 9, 2016 Share Posted February 9, 2016 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) Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted February 9, 2016 Share Posted February 9, 2016 (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)) Quote Link to comment Share on other sites More sharing options...
m4rdy Posted February 12, 2016 Share Posted February 12, 2016 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 Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted February 12, 2016 Share Posted February 12, 2016 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)) Quote Link to comment Share on other sites More sharing options...
m4rdy Posted February 13, 2016 Share Posted February 13, 2016 Dear Lee Mac, Thank you. m4rdy Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted February 13, 2016 Share Posted February 13, 2016 You're most welcome - these are interesting functions to write. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.