Skip to content

Commit d49939f

Browse files
committed
Fix variations
1 parent 66474d6 commit d49939f

File tree

2 files changed

+13
-31
lines changed

2 files changed

+13
-31
lines changed

Advanced.Algorithms.Tests/Combinatorics/Variation_Tests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public void Variation_With_Repetitions_Smoke_Test()
4141
{
4242
var input = "abcd".ToCharArray().ToList();
4343
var variations = Variation.Find<char>(input, 2, true);
44-
Assert.AreEqual(Math.Pow(input.Count, 2), variations.Count);
44+
Assert.AreEqual(combination(input.Count + 2 - 1, 2) * factorial(2), variations.Count);
4545

4646
input = "scan".ToCharArray().ToList();
4747
variations = Variation.Find<char>(input, 3, true);
48-
Assert.AreEqual(Math.Pow(input.Count, 3), variations.Count);
48+
Assert.AreEqual(combination(input.Count + 3 - 1, 3) * factorial(3), variations.Count);
4949

5050
input = "".ToCharArray().ToList();
5151
variations = Variation.Find<char>(input, 3, true);

Advanced.Algorithms/Combinatorics/Variation.cs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,49 +9,31 @@ namespace Advanced.Algorithms.Combinatorics
99
public class Variation
1010
{
1111
/*Variations are arrangements of selections of objects, where the order of the selected objects matters.
12-
To count k-element variations of n objects, we first need to choose a k-element combination and then
12+
To count r-element variations of n objects, we first need to choose a r-element combination and then
1313
a permutation of the selected objects*/
1414

1515
//Without repetition
16-
/* It is also the number of ways of putting r distinct balls into input.Count distinct boxes such that each box
16+
/* It is also the number of ways of putting n distinct balls into r distinct boxes such that each box
1717
receives at most one element. */
1818

1919
//With repetition
20-
/* It is the number of all ways of putting r distinct balls into input.Count distinct boxes */
20+
/* It is the number of all ways of putting n distinct balls into r distinct boxes */
2121
public static List<List<T>> Find<T>(List<T> input, int r, bool withRepetition)
2222
{
23-
var result = new List<List<T>>();
23+
var combinations = new List<List<T>>();
2424

25-
Recurse(input, r, withRepetition, new List<T>(), new HashSet<int>(), result);
25+
combinations.AddRange(Combination.Find(input, r, withRepetition));
2626

27-
return result;
28-
}
27+
var variations = new List<List<T>>();
2928

30-
private static void Recurse<T>(List<T> input, int r, bool withRepetition,
31-
List<T> prefix, HashSet<int> prefixIndices,
32-
List<List<T>> result)
33-
{
34-
if (prefix.Count == r)
29+
foreach (var combination in combinations)
3530
{
36-
result.Add(new List<T>(prefix));
37-
return;
31+
variations.AddRange(Permutation.Find(combination, combination.Count));
3832
}
3933

40-
for (int j = 0; j < input.Count; j++)
41-
{
42-
if (prefixIndices.Contains(j) && !withRepetition)
43-
{
44-
continue;
45-
}
46-
47-
prefix.Add(input[j]);
48-
prefixIndices.Add(j);
49-
50-
Recurse(input, r, withRepetition, prefix, prefixIndices, result);
51-
52-
prefix.RemoveAt(prefix.Count - 1);
53-
prefixIndices.Remove(j);
54-
}
34+
return variations;
5535
}
36+
37+
5638
}
5739
}

0 commit comments

Comments
 (0)