@@ -12,77 +12,64 @@ namespace Advanced.Algorithms.Tests.Combinatorics
12
12
public class Permutation_Tests
13
13
{
14
14
//for verification
15
- readonly Func < int , int > factorial = n => n == 0 ? 1 :
15
+ static readonly Func < int , int > factorial = n => n == 0 ? 1 :
16
16
Enumerable . Range ( 1 , n ) . Aggregate ( ( acc , x ) => acc * x ) ;
17
17
18
-
19
- [ TestMethod ]
20
- public void Permutation_Without_Repetitions_Smoke_Test ( )
21
- {
22
- var input = "" . ToCharArray ( ) . ToList ( ) ;
23
- var permuations = Permutation . Find < char > ( input ) ;
24
- Assert . AreEqual ( factorial ( input . Count ) , permuations . Count ) ;
25
-
26
- input = "cookie" . ToCharArray ( ) . ToList ( ) ;
27
- permuations = Permutation . Find < char > ( input ) ;
28
- Assert . AreEqual ( factorial ( input . Count ) , permuations . Count ) ;
29
-
30
- input = "monster" . ToCharArray ( ) . ToList ( ) ;
31
- permuations = Permutation . Find < char > ( input ) ;
32
- Assert . AreEqual ( factorial ( input . Count ) , permuations . Count ) ;
33
- }
34
-
18
+ //for verification
19
+ static readonly Func < int , int , int > permutation = ( int n , int r )
20
+ => n == 0 || r == 0 ? 1 : factorial ( n ) / factorial ( n - r ) ;
35
21
36
22
[ TestMethod ]
37
23
public void Permutation_With_Repetition_Smoke_Test ( )
38
24
{
39
25
var input = "" . ToCharArray ( ) . ToList ( ) ;
40
- var permuations = Permutation . Find < char > ( input , true ) ;
26
+ var permuations = Permutation . Find < char > ( input , input . Count , true ) ;
41
27
Assert . AreEqual ( Math . Pow ( input . Count , input . Count ) , permuations . Count ) ;
42
28
43
29
input = "pen" . ToCharArray ( ) . ToList ( ) ;
44
- permuations = Permutation . Find < char > ( input , true ) ;
30
+ permuations = Permutation . Find < char > ( input , input . Count , true ) ;
45
31
Assert . AreEqual ( Math . Pow ( input . Count , input . Count ) , permuations . Count ) ;
46
32
47
33
input = "scan" . ToCharArray ( ) . ToList ( ) ;
48
- permuations = Permutation . Find < char > ( input , true ) ;
34
+ permuations = Permutation . Find < char > ( input , input . Count , true ) ;
49
35
Assert . AreEqual ( Math . Pow ( input . Count , input . Count ) , permuations . Count ) ;
50
- }
51
36
52
- [ TestMethod ]
53
- public void Permutation_Without_Repetition_Without_Inversions_Smoke_Test ( )
54
- {
55
- var input = "" . ToCharArray ( ) . ToList ( ) ;
56
- var permuations = Permutation . Find < char > ( input , false , false ) ;
57
- Assert . AreEqual ( factorial ( input . Count ) / 2 , permuations . Count ) ;
37
+ input = "scan" . ToCharArray ( ) . ToList ( ) ;
38
+ permuations = Permutation . Find < char > ( input , 2 , true ) ;
39
+ Assert . AreEqual ( Math . Pow ( input . Count , 2 ) , permuations . Count ) ;
58
40
59
- input = "abc " . ToCharArray ( ) . ToList ( ) ;
60
- permuations = Permutation . Find < char > ( input , false , false ) ;
61
- Assert . AreEqual ( factorial ( input . Count ) / 2 , permuations . Count ) ;
41
+ input = "scan " . ToCharArray ( ) . ToList ( ) ;
42
+ permuations = Permutation . Find < char > ( input , 3 , true ) ;
43
+ Assert . AreEqual ( Math . Pow ( input . Count , 3 ) , permuations . Count ) ;
62
44
63
- input = "acde " . ToCharArray ( ) . ToList ( ) ;
64
- permuations = Permutation . Find < char > ( input , false , false ) ;
65
- Assert . AreEqual ( factorial ( input . Count ) / 2 , permuations . Count ) ;
45
+ input = "scaner " . ToCharArray ( ) . ToList ( ) ;
46
+ permuations = Permutation . Find < char > ( input , 4 , true ) ;
47
+ Assert . AreEqual ( Math . Pow ( input . Count , 4 ) , permuations . Count ) ;
66
48
}
67
49
68
50
69
51
[ TestMethod ]
70
- public void Permutation_With_Repetition_Without_Inversions_Smoke_Test ( )
52
+ public void Permutation_Without_Repetitions_Smoke_Test ( )
71
53
{
72
-
73
54
var input = "" . ToCharArray ( ) . ToList ( ) ;
74
- var permuations = Permutation . Find < char > ( input , true , false ) ;
75
- Assert . AreEqual ( 0 , permuations . Count ) ;
55
+ var permuations = Permutation . Find < char > ( input , input . Count ) ;
56
+ Assert . AreEqual ( permutation ( input . Count , input . Count ) , permuations . Count ) ;
76
57
77
- input = "pen " . ToCharArray ( ) . ToList ( ) ;
78
- permuations = Permutation . Find < char > ( input , true , false ) ;
79
- Assert . AreEqual ( 9 , permuations . Count ) ;
58
+ input = "cookie " . ToCharArray ( ) . ToList ( ) ;
59
+ permuations = Permutation . Find < char > ( input , input . Count ) ;
60
+ Assert . AreEqual ( permutation ( input . Count , input . Count ) , permuations . Count ) ;
80
61
81
- input = "cool" . ToCharArray ( ) . ToList ( ) ;
82
- permuations = Permutation . Find < char > ( input , true , false ) ;
83
- Assert . AreEqual ( 80 , permuations . Count ) ;
84
- }
62
+ input = "monster" . ToCharArray ( ) . ToList ( ) ;
63
+ permuations = Permutation . Find < char > ( input , input . Count ) ;
64
+ Assert . AreEqual ( permutation ( input . Count , input . Count ) , permuations . Count ) ;
85
65
86
-
66
+ input = "cookie" . ToCharArray ( ) . ToList ( ) ;
67
+ permuations = Permutation . Find < char > ( input , 2 ) ;
68
+ Assert . AreEqual ( permutation ( input . Count , 2 ) , permuations . Count ) ;
69
+
70
+ input = "monster" . ToCharArray ( ) . ToList ( ) ;
71
+ permuations = Permutation . Find < char > ( input , 3 ) ;
72
+ Assert . AreEqual ( permutation ( input . Count , 3 ) , permuations . Count ) ;
73
+ }
87
74
}
88
75
}
0 commit comments