Post Reply 
Little problem(s) July 2022
07-07-2022, 05:27 PM
Post: #26
RE: Little problem(s) July 2022
My attempt at #3.

I wouldn't even try this without the use of the following ListExt commands:

LSUM: basically a slightly faster ΣLIST (and is a heck of lot easier to type Smile ).
LPICK: given a list and a corresponding set of indices, returns the values from the original list indicated by those indices. Similar to calling GET for each specified index against the original source list.
DOCOMB: generates specified combinations of the provided indices and passes them to a supplied program (similar to DOLIST/DOSUBS, but passes combinations of the elements instead of each element).

DOCOMB also has a feature wherein you can tell it to abort further processing by storing something that evaluates to TRUE in the local 'XCMB'. This local is created by DOCOMB automatically and is only available while the command is processing. It is used in this case to only provide the first solution encountered.

This will slow down rather noticeably as the list size grows, of course. I believe this will be the case with just about any approach, but I don't assume that the method I've used is necessarily the best or fastest. But I believe it works for this particular situation.

Code:
\<<
   OVER SIZE DUP LSEQ                  @ setup local vars
   \-> list targ n ndx                 @ list: original list (given)
                                       @ targ: the target sum for comparison (given)
                                       @ n: current count of elements for summing, initially = count of elements
                                       @ ndx: static list of index numbers (1..count of elements)
   \<<
      {} n                             @ preload stack for loop: {}=no solution yet, count of elements to check
      WHILE
         OVER SIZE NOT AND             @ continue if no solution yet and count > 0
      REPEAT
         DROP                          @ drop the previous (empty) list of solutions
         ndx                           @ recall the complete index list
         n                             @ recall the current count
         \<<
            list OVER LPICK            @ obtain the values from the original list for summing
            LSUM targ SAME             @ check for sum = target value
            DUP 'XCMB' STO             @ set to exit DOCOMB if target found
            NOT DROPN                  @ drop the current list of indices if it did not match
         \>>
         DOCOMB                        @ execute the above program object for each combination of n indices
         'n' DECR                      @ decrement index count, recall to stack
      END
      DUP SIZE ::EVAL IFT              @ if non-empty list for final result, remove inner list brackets
   \>>
\>>
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Little problem(s) July 2022 - pier4r - 07-04-2022, 06:04 PM
RE: Little problem(s) July 2022 - pauln - 07-05-2022, 01:01 AM
RE: Little problem(s) July 2022 - pier4r - 07-05-2022, 10:00 AM
RE: Little problem(s) July 2022 - pier4r - 07-05-2022, 01:56 PM
RE: Little problem(s) July 2022 - pier4r - 07-07-2022, 10:51 AM
RE: Little problem(s) July 2022 - DavidM - 07-07-2022, 12:02 PM
RE: Little problem(s) July 2022 - DavidM - 07-07-2022, 03:09 PM
RE: Little problem(s) July 2022 - pier4r - 07-07-2022, 05:19 PM
RE: Little problem(s) July 2022 - Werner - 07-06-2022, 07:48 AM
RE: Little problem(s) July 2022 - DavidM - 07-07-2022 05:27 PM
RE: Little problem(s) July 2022 - pier4r - 07-07-2022, 05:37 PM
RE: Little problem(s) July 2022 - pauln - 07-11-2022, 05:34 AM
RE: Little problem(s) July 2022 - pauln - 07-11-2022, 10:32 PM



User(s) browsing this thread: 1 Guest(s)