@@ -12,77 +12,64 @@ namespace Advanced.Algorithms.Tests.Combinatorics
1212 public class Permutation_Tests
1313 {
1414 //for verification
15- readonly Func < int , int > factorial = n => n == 0 ? 1 :
15+ static readonly Func < int , int > factorial = n => n == 0 ? 1 :
1616 Enumerable . Range ( 1 , n ) . Aggregate ( ( acc , x ) => acc * x ) ;
1717
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 ) ;
3521
3622 [ TestMethod ]
3723 public void Permutation_With_Repetition_Smoke_Test ( )
3824 {
3925 var input = "" . ToCharArray ( ) . ToList ( ) ;
40- var permuations = Permutation . Find < char > ( input , true ) ;
26+ var permuations = Permutation . Find < char > ( input , input . Count , true ) ;
4127 Assert . AreEqual ( Math . Pow ( input . Count , input . Count ) , permuations . Count ) ;
4228
4329 input = "pen" . ToCharArray ( ) . ToList ( ) ;
44- permuations = Permutation . Find < char > ( input , true ) ;
30+ permuations = Permutation . Find < char > ( input , input . Count , true ) ;
4531 Assert . AreEqual ( Math . Pow ( input . Count , input . Count ) , permuations . Count ) ;
4632
4733 input = "scan" . ToCharArray ( ) . ToList ( ) ;
48- permuations = Permutation . Find < char > ( input , true ) ;
34+ permuations = Permutation . Find < char > ( input , input . Count , true ) ;
4935 Assert . AreEqual ( Math . Pow ( input . Count , input . Count ) , permuations . Count ) ;
50- }
5136
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 ) ;
5840
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 ) ;
6244
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 ) ;
6648 }
6749
6850
6951 [ TestMethod ]
70- public void Permutation_With_Repetition_Without_Inversions_Smoke_Test ( )
52+ public void Permutation_Without_Repetitions_Smoke_Test ( )
7153 {
72-
7354 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 ) ;
7657
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 ) ;
8061
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 ) ;
8565
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+ }
8774 }
8875}
0 commit comments