A combinatorics module for Monkey

Monkey Forums/Monkey Programming/A combinatorics module for Monkey

SpaceAce(Posted 2017) [#1]
Have you ever wanted to arrange things in groups of varying sizes? Boy, have I got the module for you!

https://github.com/SpaceAceMonkey/combinatorics

I spent the last couple of days banging away at this. The github page has a readme file, and the code has lots of comments. There is also a file full of examples.

A brief overview:
Combinatorics.MassPermutationsCount(12)
1302061344

Combinatorics.Choose(16, 8)
12870

Combinatorics.Factorial(16)
20922789888000

Combinatorics.FactorialRadix(64, 4)
[0, 0, 2, 2, 2]

Combinatorics.MassPermutationsCount(8)
109600

Combinatorics.PermutationsCount(16, 4)
43680


Combinator([a, b, c, d, e, f, g, h], 6)

[c, d, e, f, g, h], [b, d, e, f, g, h], [a, d, e, f, g, h], [b, c, e, f, g, h]
[a, c, e, f, g, h], [a, b, e, f, g, h], [b, c, d, f, g, h], [a, c, d, f, g, h]
[a, b, d, f, g, h], [a, b, c, f, g, h], [b, c, d, e, g, h], [a, c, d, e, g, h]
[a, b, d, e, g, h], [a, b, c, e, g, h], [a, b, c, d, g, h], [b, c, d, e, f, h]
[a, c, d, e, f, h], [a, b, d, e, f, h], [a, b, c, e, f, h], [a, b, c, d, f, h]
[a, b, c, d, e, h], [b, c, d, e, f, g], [a, c, d, e, f, g], [a, b, d, e, f, g]
[a, b, c, e, f, g], [a, b, c, d, f, g], [a, b, c, d, e, g], [a, b, c, d, e, f]


Permuter([a, b, c, d])

[a, b, c, d], [b, a, c, d], [a, c, b, d], [c, a, b, d], [b, c, a, d], [c, b, a, d]
[a, b, d, c], [b, a, d, c], [a, d, b, c], [d, a, b, c], [b, d, a, c], [d, b, a, c]
[a, c, d, b], [c, a, d, b], [a, d, c, b], [d, a, c, b], [c, d, a, b], [d, c, a, b]
[b, c, d, a], [c, b, d, a], [b, d, c, a], [d, b, c, a], [c, d, b, a], [d, c, b, a]


CombinatorPermuter([a, b, c, d, e, f], 2)

[e, f], [f, e], [d, f], [f, d], [c, f], [f, c]
[b, f], [f, b], [a, f], [f, a], [d, e], [e, d]
[c, e], [e, c], [b, e], [e, b], [a, e], [e, a]
[c, d], [d, c], [b, d], [d, b], [a, d], [d, a]
[b, c], [c, b], [a, c], [c, a], [a, b], [b, a]


MassPermuter([a, b, c, d])

[d], [c], [b], [a], 
[c, d], [d, c], [b, d], [d, b], 
[a, d], [d, a], [b, c], [c, b],
[a, c], [c, a], [a, b], [b, a], 
[b, c, d], [c, b, d], [b, d, c], [d, b, c], [c, d, b], [d, c, b], 
[a, c, d], [c, a, d], [a, d, c], [d, a, c], [c, d, a], [d, c, a], 
[a, b, d], [b, a, d], [a, d, b], [d, a, b], [b, d, a], [d, b, a], 
[a, b, c], [b, a, c], [a, c, b], [c, a, b], [b, c, a], [c, b, a], 
[a, b, c, d], [b, a, c, d], [a, c, b, d], [c, a, b, d], 
[b, c, a, d], [c, b, a, d], [a, b, d, c], [b, a, d, c],
[a, d, b, c], [d, a, b, c], [b, d, a, c], [d, b, a, c], 
[a, c, d, b], [c, a, d, b], [a, d, c, b], [d, a, c, b], 
[c, d, a, b], [d, c, a, b], [b, c, d, a], [c, b, d, a],
[b, d, c, a], [d, b, c, a], [c, d, b, a], [d, c, b, a]


Import spaceace.combinatorics.cCombinator
Import spaceace.combinatorics.cPermuter
Import spaceace.combinatorics.cCombinatorPermuter
Import spaceace.combinatorics.cMassPermuter

Function Main:Int()
	Local aString:String = "abcdefgh"
	Local numberToChoose:Int = 6
	Local elements:String[]
	Local g:ICombinatoricsGenerator<String>
	
	elements = aString.Split("")
	g = New Combinator<String>(elements, numberToChoose)
	Print "Combinator([" + Implode(elements, ", ") + "], " + numberToChoose + ")"
	ShowResultsBlock(g)
	Print ""
	
	aString = "abcd"
	elements = aString.Split("")
	g = New Permuter<String>(elements)
	Print "Permuter([" + Implode(elements, ", ") + "])"
	ShowResultsBlock(g, 40, 6)
	Print ""
	
	aString = "abcdef"
	numberToChoose = 2
	elements = aString.Split("")
	g = New CombinatorPermuter<String>(elements, numberToChoose)
	Print "CombinatorPermuter([" + Implode(elements, ", ") + "], " + numberToChoose + ")"
	ShowResultsBlock(g, 40, 6)
	Print ""
	
	aString = "abcd"
	elements = aString.Split("")
	g = New MassPermuter<String>(elements)
	Print "MassPermuter([" + Implode(elements, ", ") + "])"
	ShowResultsBlock(g, 64, 6)

	Return 0
End Function



Function Implode:String(arr:String[], seperator:String = "")
	Local s:String
	For Local i:Int = 0 To arr.Length - 1
		s += arr[i]
		If (i < arr.Length - 1)
			s += seperator
		EndIf
	Next
	
	Return s
End Function

Function ShowResultsBlock:Void(g:ICombinatoricsGenerator<String>, limit:Int = 40, columns:Int = 4)
	Local toShow:Int = Min(g.Length(), limit)
	Print "---- Showing " + toShow + " of " + g.Length() + " groupings ----"
	Local seriesStack:Stack<String[] > = New Stack<String[] > (g.ToArray())
	Local i:Int = 0
	While (i < toShow)
		Local j:Int = 0
		Local stringStack:StringStack = New StringStack()
		While (j < columns And seriesStack.Length() > 0 And i < toShow)
			stringStack.Push("[" + Implode(seriesStack.Pop(), ", ") + "]")
			j += 1
			i += 1
		Wend
		Print Implode(stringStack.ToArray(), ", ")
	Wend
End Function


I hope someone finds it useful. I would like to add more functions, such as power sets. I'll definitely do that if people find this library useful. Otherwise, feel free to make pull requests against the Gibhub repository if you want to add something.