TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Q1241274614 on July 20, 2014, 06:18:33 AM

Title: lst<100
Post by: Q1241274614 on July 20, 2014, 06:18:33 AM
(setq al (list (list('(45 45) '(45)))
               (list('(34 34) '(34)))
               (list('(23 23 23 23) '(23 23 23) '(23 23) '(23)))
               (list('(12 12 12 12 12 12 12 12) '(12 12 12 12 12 12 12) '(12 12 12 12 12 12) '(12 12 12 12 12) '(12 12 12 12) '(12 12 12) '(12 12) '(12))))
      a2 100)

;result
;<100
'(45 45)
'(45)
'(34 34)
'(34)
'(23 23 23 23)
'(23 23 23)
'(23 23)
'(23)
'(12 12 12 12 12 12 12 12)
'(12 12 12 12 12 12 12)
'(12 12 12 12 12 12)
'(12 12 12 12 12)
'(12 12 12 12)
'(12 12 12)
'(12 12)
'(12)
'(45) + '(34)  + '(12)
'(45) + '(12 12 12 12 12)
'(34 34) + '(23)
'(34 34) + '(12 12)
'(12 12 12 12 12 12) + '(23)
'(23 23 23) + '(12 12)
......
Title: Re: lst<100
Post by: Kerry on July 20, 2014, 06:34:46 AM
That's nice.
Title: Re: lst<100
Post by: Lee Mac on July 20, 2014, 06:43:07 AM
A brute force approach - do not use with long lists (you'll be waiting a long time...)

Code - Auto/Visual Lisp: [Select]
  1. (defun lst< ( l n )
  2.     (vl-remove-if-not '(lambda ( x ) (< (sum x) n)) (combinations l))
  3. )
  4.  
  5. (defun sum ( l )
  6.     (apply '+ (mapcar '(lambda ( x ) (if (numberp x) x (sum x))) l))
  7. )
  8.  
  9. (defun combinations ( l )
  10.     (defun nCr ( l r )
  11.         (cond
  12.             (   (< r 2)
  13.                 (mapcar 'list l)
  14.             )
  15.             (   l
  16.                 (append
  17.                     (mapcar '(lambda ( x ) (cons (car l) x)) (nCr (cdr l) (1- r)))
  18.                     (nCr (cdr l) r)
  19.                 )
  20.             )
  21.         )
  22.     )
  23.     (defun nCr< ( l r )
  24.         (if (< 0 r)
  25.             (append (nCr l r) (nCr< l (1- r)))
  26.         )
  27.     )
  28.     (nCr< l (length l))
  29. )

Example:
Code: [Select]
(setq al
    (list
        '(45 45) '(45)
        '(34 34) '(34)
        '(23 23 23 23)
        '(12 12 12 12 12) '(12 12 12 12) '(12 12 12) '(12 12) '(12)
    )
)
Code: [Select]
_$ (lst< al 100)
(
    ((45) (34) (12))
    ((45) (12 12 12) (12))
    ((45) (12 12) (12))
    ((34) (12 12 12 12) (12))
    ((34) (12 12 12) (12 12))
    ((34) (12 12 12) (12))
    ((34) (12 12) (12))
    ((12 12 12 12 12) (12 12) (12))
    ((12 12 12 12) (12 12 12) (12))
    ((12 12 12 12) (12 12) (12))
    ((12 12 12) (12 12) (12))
    ((45) (34))
    ((45) (12 12 12 12))
    ((45) (12 12 12))
    ((45) (12 12))
    ((45) (12))
    ((34 34) (12 12))
    ((34 34) (12))
    ((34) (12 12 12 12 12))
    ((34) (12 12 12 12))
    ((34) (12 12 12))
    ((34) (12 12))
    ((34) (12))
    ((12 12 12 12 12) (12 12 12))
    ((12 12 12 12 12) (12 12))
    ((12 12 12 12 12) (12))
    ((12 12 12 12) (12 12 12))
    ((12 12 12 12) (12 12))
    ((12 12 12 12) (12))
    ((12 12 12) (12 12))
    ((12 12 12) (12))
    ((12 12) (12))
    ((45 45))
    ((45))
    ((34 34))
    ((34))
    ((23 23 23 23))
    ((12 12 12 12 12))
    ((12 12 12 12))
    ((12 12 12))
    ((12 12))
    ((12))
)
Title: Re: lst<100
Post by: Kerry on July 20, 2014, 06:43:49 AM
I'm too lazy to evaluate those assignments, but I'm pretty sure you'll get an error ... something like missing or bad function ...

added: addressed to the OP.
Title: Re: lst<100
Post by: Q1241274614 on July 20, 2014, 06:45:39 AM
thank!
Title: Re: lst<100
Post by: andy_lee on July 20, 2014, 08:11:18 PM
Is this your homework to the student?
Title: Re: lst<100
Post by: irneb on July 21, 2014, 01:37:55 AM
I'm too lazy to evaluate those assignments, but I'm pretty sure you'll get an error ... something like missing or bad function ...

added: addressed to the OP.
Yep, correct. There are too many parentheses in there. A very good reason for using a decent editor. The OP's code should look like this instead:
Code - Auto/Visual Lisp: [Select]
  1. (setq a1 (list (list '(45 45) '(45))
  2.                (list '(34 34) '(34))
  3.                (list '(23 23 23 23) '(23 23 23) '(23 23) '(23))
  4.                (list '(12 12 12 12 12 12 12 12)
  5.                      '(12 12 12 12 12 12 12) '(12 12 12 12 12 12) '(12 12 12 12 12) '(12 12 12 12) '(12 12 12)
  6.                      '(12 12) '(12)))
  7.       a2 100)
Then that should result in:
Code: [Select]
_$ a1
(((45 45) (45)) ((34 34) (34)) ((23 23 23 23) (23 23 23) (23 23) (23)) ((12 12 12 12 12 12 12 12) (12 12 12 12 12 12 12) (12 12 12 12 12 12) (12 12 12 12 12) (12 12 12 12) (12 12 12) (12 12) (12)))
_$ a2
100
Now is there a reason you have them in sub-lists? Should the permutations only happen between these? Or should some combinations also occur between items in the same sublist, e.g.:
Code: [Select]
(12 12) + (12)Sorry, from samples this is unclear. If this is allowable, then making it slightly simpler would be to append all the sublists (i.e. flatten) into one list like so:
Code: [Select]
_$ (apply 'append a1)
((45 45) (45) (34 34) (34) (23 23 23 23) (23 23 23) (23 23) (23) (12 12 12 12 12 12 12 12) (12 12 12 12 12 12 12) (12 12 12 12 12 12) (12 12 12 12 12) (12 12 12 12) (12 12 12) (12 12) (12))
Else the combinations become slightly more complex.

Edit: Also are the numbers always positive? That could allow for some algorithmic optimization as some combinations could immediately be omitted.
Title: Re: lst<100
Post by: Q1241274614 on October 12, 2014, 09:30:48 AM
1.Avoid ‘(12 12) + ’(12), requirements are not the same
2. (apply 'append a1) ------ Very slow
Title: Re: lst<100
Post by: well20152016 on December 18, 2021, 06:28:29 PM
no brute force approach? Faster?

Known: value  a1 + maximum a2
find: value -  quantity

(setq a1 (list 45 23 12 33))
        a2 100)

< 100  45 - 2
< 100  23 - 4
< 100  33 - 3
< 100  12 - 8