Created
September 5, 2024 14:22
-
-
Save pbartxl/0253416c8b83f4235ce381ca5bc711d9 to your computer and use it in GitHub Desktop.
An Excel Lambda function that generates all the combinations of m objects taken from n. The result is returned as an array of binary numbers 1 = selected, 0 = omitted.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* FUNCTION NAME: COMBINATIONAλ | |
DESCRIPTION:*//*Lists all comninations of m objects taken from n*/ | |
/* REVISIONS: Date Developer Description | |
29 Mar 2024 Peter Bartholomew Original development | |
*/ | |
/* REQUIRED: NextColumnAλ, ϑSTACKλ, THUNKλ, FINALVALUEλ, | |
*/ | |
COMBINATIONAλ = LAMBDA( | |
// Parameter Declarations | |
[n], | |
[m], | |
// Procedure | |
LET( | |
Help, TRIM( | |
TEXTSPLIT( | |
"DESCRIPTION: →Lists all combinations of m objects taken from n.¶" & | |
"VERSION: →29 Mar 2024¶" & "PARAMETERS: →¶" & | |
" n →(Required) A scalar value giving the count of objects available ¶" & | |
" m →(Required) A scalar value giving the count of objects selected ¶" & | |
"Result →Formula ¶" & "{0,1;1,0} →COMBINATIONAλ(3,2) ", | |
"→", | |
"¶" | |
) | |
), | |
// Check inputs | |
n, IF(OR(ISOMITTED(n), n < 2), #VALUE!, n), | |
m, IF(OR(ISOMITTED(m), m < 1), #VALUE!, m), | |
// Procedure | |
/* Generate initialisation array of n values taken from n as a thunk array | |
Although the sequence 1,3,7,15 are decimal numbers they are to be interpreted as | |
binary numbers 1, 11, 111, 1111 showing all values are selected. */ | |
init, POWER(2, SEQUENCE(m)) - 1, | |
initColumnϑ, MAP(init, THUNKλ), | |
// Apply NextColumnAλ until required column is reached | |
finalColumnϑ, REDUCE(initColumnϑ, SEQUENCE(n - m), NextColumnAλ), | |
// Convert last element of final column to decimal from thunk | |
decimal, INDEX(finalColumnϑ, m, 1)(), | |
// Display results as binary list | |
result, BASE(decimal, 2, n), | |
// Handle Error | |
Error, MAX(ISERROR(Result) + 1), | |
// Return result | |
CHOOSE(Error, result, Help) | |
) | |
); | |
/* FUNCTION NAME: NextColumnAλ | |
DESCRIPTION:*//*Given a diagonal (as Pascal's triangle) 'column' from the combinations uses SCAN to form the next column. */ | |
/* REVISIONS: Date Developer Description | |
30 Mar 2024 Peter Bartholomew Original development | |
*/ | |
/* REQUIRED: ϑSTACKλ, THUNKλ | |
*/ | |
NextColumnAλ = LAMBDA( | |
// Parameter Declarations | |
[priorColumn], | |
[k], | |
// Procedure | |
LET( | |
Help, TRIM( | |
TEXTSPLIT( | |
"DESCRIPTION: →Given a diagonal 'column' from the combinations array,¶" & | |
" →uses SCAN to form the next column.¶" & | |
"VERSION: →30 Mar 2024¶" & "PARAMETERS: →¶" & | |
" priorColumn →(Required) An array containing the prior diagonal thunked 'column' of combinations¶" & | |
" k →(Optional) A scalar counter that provides the correct signature for REDUCE¶" & | |
"Result →Formula (combined with FINAVALUE to expand the final thunk of the return array) ¶" & | |
"{3;5;6} →FINALVALUE(NextColumnAλ(VSTACK(THUNK(1),THUNK(3)), 2))", | |
"→", | |
"¶" | |
) | |
), | |
// Check inputs | |
priorColumn, IF(ISOMITTED(priorColumn), #VALUE!, priorColumn), | |
// Procedure | |
result, SCAN(THUNKλ(0), priorColumn, ϑSTACKλ), | |
// Handle Error | |
Error, MAX(ISERROR(Result) + 1), | |
// Return result | |
CHOOSE(Error, result, Help) | |
) | |
); | |
/* FUNCTION NAME: ϑSTACKλ | |
DESCRIPTION:*//*Stacks two component lists, appending 1 to the end of each term | |
within the first and a zero to each term of the second*/ | |
/* REVISIONS: Date Developer Description | |
29 Mar 2024 Peter Bartholomew Original development | |
*/ | |
/* REQUIRED: THUNKλ | |
*/ | |
ϑSTACKλ = LAMBDA(xϑ, yϑ, THUNKλ(VSTACK(2 * xϑ() + 1, 2 * yϑ()))); | |
THUNKλ = LAMBDA(x, LAMBDA(x)); | |
FINALVALUEλ = LAMBDA(arrϑ, INDEX(arrϑ, ROWS(arrϑ), 1)()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment