Author Topic: lst<100  (Read 4028 times)

0 Members and 1 Guest are viewing this topic.

Q1241274614

  • Guest
lst<100
« 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)
......

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: lst<100
« Reply #1 on: July 20, 2014, 06:34:46 AM »
That's nice.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: lst<100
« Reply #2 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))
)

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: lst<100
« Reply #3 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.
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Q1241274614

  • Guest
Re: lst<100
« Reply #4 on: July 20, 2014, 06:45:39 AM »
thank!

andy_lee

  • Newt
  • Posts: 147
Re: lst<100
« Reply #5 on: July 20, 2014, 08:11:18 PM »
Is this your homework to the student?
andy.
Best regards.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: lst<100
« Reply #6 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.
« Last Edit: July 21, 2014, 01:41:01 AM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Q1241274614

  • Guest
Re: lst<100
« Reply #7 on: October 12, 2014, 09:30:48 AM »
1.Avoid ‘(12 12) + ’(12), requirements are not the same
2. (apply 'append a1) ------ Very slow

well20152016

  • Newt
  • Posts: 130
Re: lst<100
« Reply #8 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