blob: 2c56d3b9eb633c5db5795144d8180e3f3274406e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
import java.util.ArrayList;
public class PermutationTester
{
public static void main(String[] args)
{
int point = 0;
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<ArrayList<Integer>> result;
for(int i = 1; i <= 5; i++)
{
System.out.println("Testing permutation with " + i + " element(s)");
list.add(i);
/*Take this out*///System.out.println("[ME]Before" + list);
result = Permutation.permutation(list);
/*Take this out*///System.out.println("[ME]After" + list);
System.out.println("Your code said, the permutation of " + list + " is as follows:");
System.out.println(result);
boolean result1 = checkPermutationSize(list, result);
boolean result2 = checkPermutation(list, result);
if(result1 && result2)
{
point += 2;
System.out.println("Your current point is " + point + ".");
System.out.println();
}
else
{
System.out.println("Your current point is " + point + ".");
System.out.println("There is someting wrong with your method permutation().");
System.out.println("Fix your code and run this tester class again.");
break;
}
}
}
private static int factorial(int n)
{
if(n == 0)
return 1;
else
return n * factorial(n - 1);
}
private static boolean checkPermutation(ArrayList<Integer> list, ArrayList<ArrayList<Integer>> result)
{
int numberOfElements = list.size();
int numberOfResults = result.size();
System.out.print("Check the size of each element: ");
for(int i = 0; i < numberOfResults; i++)
{
if(result.get(i).size() != list.size())
{
System.out.println("FAIL");
System.out.println("Every element in your result should contains " + numberOfElements + " elements.");
System.out.println("But an element of your result contains " + result.get(i).size() + " elements.");
return false;
}
}
System.out.println("PASS");
System.out.print("Check for duplicate elements: ");
for(int i = 0; i < numberOfResults; i++)
{
ArrayList<Integer> temp = result.get(i);
for(int j = i + 1; j < numberOfResults; j++)
{
if(temp.equals(result.get(j)))
{
System.out.println("FAIL");
System.out.println("Your result contains duplicate items which is " + temp + ".");
return false;
}
}
}
System.out.println("PASS");
System.out.print("Check each element: ");
for(int i = 0; i < numberOfResults; i++)
{
ArrayList<Integer> temp = result.get(i);
for(int j = 0; j < numberOfElements; j++)
{
int aNumber = list.get(j);
if(!temp.contains(aNumber))
{
System.out.println("FAIL");
System.out.println("The element " + temp + " in your result does not contain every element in " + list + ".");
}
}
}
System.out.println("PASS");
return true;
}
private static boolean checkPermutationSize(ArrayList<Integer> list, ArrayList<ArrayList<Integer>> result)
{
System.out.print("Check the size of the result: ");
int numberOfElements = list.size();
int numberOfResults = result.size();
if(factorial(numberOfElements) != numberOfResults)
{
System.out.println("FAIL");
System.out.println(" The list " + list + " contains " + numberOfElements + " elements.");
System.out.println(" Permutation of " + list + " should contains " + factorial(numberOfElements) + "elements.");
System.out.println(" But your result contains " + numberOfResults + " elements.");
return false;
}
else
{
System.out.println("PASS");
return true;
}
}
}
|