Message 1 of 11
All Possible Number Outcomes, with Duplicates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello!
As I was working on THIS question, I came across an interesting function that I needed to complete this task.
I needed all possible number outcomes (combinations?) for a set of numbers and could not readily find this information.
Lee Mac has a Permutations function, but this didn't seem readily suitable for me.
So, I made my own and I just want to post it for users searching for something similar.
Here is some sample inputs / outputs
(BuildOutcomes 3 2 nil)
((1 1) (1 2) (1 3) (2 1) (2 2) (2 3) (3 1) (3 2) (3 3))
(BuildOutcomes 3 2 t) ((0 0) (0 1) (0 2) (0 3) (1 0) (1 1) (1 2) (1 3) (2 0) (2 1) (2 2) (2 3) (3 0) (3 1) (3 2) (3 3))
(BuildOutcomes 2 4 nil) ((1 1 1 1) (1 1 1 2) (1 1 2 1) (1 1 2 2) (1 2 1 1) (1 2 1 2) (1 2 2 1) (1 2 2 2)
(2 1 1 1) (2 1 1 2) (2 1 2 1) (2 1 2 2) (2 2 1 1) (2 2 1 2) (2 2 2 1) (2 2 2 2))
(BuildOutcomes 2 4 t) ((0 0 0 0) (0 0 0 1) (0 0 0 2) (0 0 1 0) (0 0 1 1) (0 0 1 2) (0 0 2 0) (0 0 2 1) (0 0 2 2) (0 1 0 0) (0 1 0 1) (0 1 0 2) (0 1 1 0) (0 1 1 1) (0 1 1 2) (0 1 2 0) (0 1 2 1) (0 1 2 2) (0 2 0 0) (0 2 0 1) (0 2 0 2) (0 2 1 0) (0 2 1 1) (0 2 1 2) (0 2 2 0) (0 2 2 1) (0 2 2 2) (1 0 0 0) (1 0 0 1) (1 0 0 2) (1 0 1 0) (1 0 1 1) (1 0 1 2) (1 0 2 0) (1 0 2 1) (1 0 2 2) (1 1 0 0) (1 1 0 1) (1 1 0 2) (1 1 1 0) (1 1 1 1) (1 1 1 2) (1 1 2 0) (1 1 2 1) (1 1 2 2) (1 2 0 0) (1 2 0 1) (1 2 0 2) (1 2 1 0) (1 2 1 1) (1 2 1 2) (1 2 2 0) (1 2 2 1) (1 2 2 2) (2 0 0 0) (2 0 0 1) (2 0 0 2) (2 0 1 0) (2 0 1 1) (2 0 1 2) (2 0 2 0) (2 0 2 1) (2 0 2 2) (2 1 0 0) (2 1 0 1) (2 1 0 2) (2 1 1 0) (2 1 1 1) (2 1 1 2) (2 1 2 0) (2 1 2 1) (2 1 2 2) (2 2 0 0) (2 2 0 1) (2 2 0 2) (2 2 1 0) (2 2 1 1) (2 2 1 2) (2 2 2 0) (2 2 2 1) (2 2 2 2))
...as you can see, these results can become quite exponential, so be cautious of the results you are expecting.
I am very much open to an alternative or reformed solution of my own function!
I hope this will benefit users in the future.
Best,
~DD
Source Code (with example call):
(defun c:TEST ( / lst n l a) (initget 7) (setq n (getint "\nEnter High Number: ")) (initget 7) (setq l (getint "\nEnter List Length: ")) (initget 1 "Y N") (setq a (cond ((getkword "\nInclude Zero [Y/N]<Y>: ")) ("Y"))) (if (eq "Y" (strcase a)) (setq a t) (setq a nil)) (setq lst (BuildOutcomes n l a)) (princ "\n.................") (princ lst) (princ "\n.................") (princ) );defun (defun BuildOutcomes (highNumber listLength includeZero / tmp tmpL lst allreps stepcount repcount final) ;---------------------------------------------------------------------- ;IN ;highNumber: Largest number that will be used in lists ;listLength: length of each list that will be storing outcomes of highNumber ;includeZero: t or nil, decides whether 0 will be used in outcomes or not ;OUT ;final: list that returns all possible outcomes for highNumber in lists that are "listLength" long for each outcome ;---------------------------------------------------------------------- (if includeZero (setq highNumber (1+ highNumber))) (setq allreps (expt highNumber listLength)) (setq stepcount (/ allreps highNumber)) (setq repcount 0 final nil) ;(setq tmp '()) (repeat listLength (while (/= allreps repcount) (if includeZero (setq tmp 0) (setq tmp 1)) (repeat highNumber (repeat stepcount (setq lst (reverse (cons (list tmp) (reverse lst)))) );repeat (setq tmp (1+ tmp) repcount (+ repcount stepcount)) );repeat );while (setq repcount 0 stepcount (/ stepcount highNumber)) (if (not (null final)) (progn (setq tmp 0) (repeat allreps (setq tmpL (reverse (cons (append (nth tmp final) (nth tmp lst)) (reverse tmpL)))) (setq tmp (1+ tmp)) );repeat (setq final tmpL tmpL '()) );progn (setq final lst) );if (setq lst '()) );repeat final );defun