Last active
December 31, 2015 05:09
-
-
Save mtwilliams/7939085 to your computer and use it in GitHub Desktop.
Coalescent of the most occurring largest number (in that order of priority.)
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
coalesce(L, R[n], occurrences = 1): | |
if L == R[0]: | |
return coalesce(L, R[1..n], occurrences + 1) | |
else: | |
A = coalesce(L, R[1..n], occurrences) | |
B = coalesce(R[0], R[1..n]) | |
if A occurs more often than B: | |
return A | |
else if B occurs more often than A: | |
return B | |
else if A > B: | |
return A | |
else | |
return B | |
V = [1, 2, 4, 1, 1, 2, 4, 3] | |
coalesce(V[0], V[1..7]), aka: | |
coalesce(1, [2, 4, 1, 1, 2, 4, 3]): | |
1 != 2: | |
A = coalesce(1, [4, 1, 1, 2, 4, 3], 1): | |
1 != 4: | |
A = coalesce(1, [1, 1, 2, 4, 3], 1): | |
1 == 1: | |
return coalesce(1, [1, 2, 4, 3], 2): | |
1 == 1: | |
return coalesce(1, [2, 4, 3], 3): | |
1 != 2: | |
A = coalesce(1, [4, 3], 3): | |
1 != 4: | |
A = coalesce(1, [3], 3): | |
1 != 3: | |
A = 1 occurs 3 times | |
B = 3 occurs 1 time | |
return A | |
= 1 occurs 3 times | |
B = coalesce(4, [3], 1): | |
4 != 3: | |
A = 4 occurs 1 time | |
B = 3 occurs 1 time | |
return A | |
= 4 occurs 1 time | |
= 1 occurs 3 times | |
B = coalesce(2, [4, 3], 1): | |
2 != 4: | |
A = coalesce(2, [3], 1) | |
2 != 3: | |
A = 2 occurs 1 time | |
B = 3 occurs 1 time | |
return B | |
= 3 occurs 1 time | |
B = coalesce(4, [3], 1): | |
4 != 3: | |
A = 4 occurs 1 time | |
B = 3 occurs 1 time | |
return A | |
= 4 occurs 1 time | |
= 4 occurs 1 time | |
= 1 occurs 3 times | |
= 1 occurs 3 times | |
= 1 occurs 3 times | |
B = coalesce(4, [1, 1, 2, 4, 3], 1): | |
... | |
= 4 occurs 2 times | |
= 1 occurs 3 times | |
B = coalesce(2, [4, 1, 1, 2, 4, 3], 1) | |
... | |
= 4 occurs 2 times | |
= 1 occurs 3 times | |
= 1 occurs 3 times |
Can this be implemented with a fold? I'll still need to implement it in C
though.
Aha! See encode on this page!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There might be an early out (implemented via non-functional/non-pure constructs?) to reduce the number of recursions and consequently the growth rate of
coalesce
.