@matfah/

Word List FR 2004

Java

No description

fork
loading
Files
  • Main.java
  • WordList.java
Main.java
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.util.*;
import java.lang.reflect.*;

public class Main {
  
  static Field f;
  
  public static void main(String[] args) {
    
    try {
      f = WordList.class.getDeclaredField("myList");
      f.setAccessible(true);
      
      System.out.println("Testing numWordsOfLength()");
      
      WordList list = new WordList();

      String[][] tests = {
        {},
        {"cat", "mouse", "frog", "dog", "dog"},
        {"mouse"}
      };
      
      int[][] lengths = {
        {1},
        {1, 3, 4},
        {5, 7}
      };
      
      int[][] counts = {
        {0},
        {0, 3, 1},
        {1, 0}
      };
      
      int test = 1;
      
      for(int row = 0; row < tests.length; row++) {
        
        for(int col = 0; col < lengths[row].length; col++) {
          f.set(list, toArrayList(tests[row]));
  
          int value = list.numWordsOfLength(lengths[row][col]);
          if(value != counts[row][col]) {
            System.out.println("   FAILED test " + test);
            
            System.out.println("   Original values: " + toArrayList(tests[row]));
            System.out.println("   Length to find: " + lengths[row][col]);
            System.out.println("   Count should be: " + counts[row][col]);
            System.out.println("   Your code calculated: " + value);
            return;
          }
          else {
            System.out.println("   Passed test "+ test);
          }
  
          test++;
        }      
        
      }
      
      System.out.println("\nTesting removeWordsOfLength()");
      
      String[][] tests2 = {
        {},
        {"cat", "mouse", "frog", "dog", "dog"},
        {"bird", "bird", "bird", "bird", "bird"},
        {"mouse"}
      };
      
      int[][] toRemove = {
        {1},
        {3, 4, 5, 6},
        {4},
        {5}
      };
      
      String[][][] results = {
        {
          {}
          
        },
        {
          {"mouse", "frog"},
          {"cat", "mouse", "dog", "dog"},
          {"cat", "frog", "dog", "dog"},
          {"cat", "mouse", "frog", "dog", "dog"}
        },
        {
          {}
        },
        {
          {}
        }
      };
      
      test = 1;
      
      for(int row = 0; row < tests2.length; row++) {
        
        for(int col = 0; col < toRemove[row].length; col++) {
          f.set(list, toArrayList(tests2[row]));
          
          list.removeWordsOfLength(toRemove[row][col]);
          
          ArrayList<String> resultList = (ArrayList<String>)f.get(list);
          
          if(!listsEqual(resultList, toArrayList(results[row][col]))) {
            System.out.println("   FAILED test " + test);
            
            System.out.println("   Original values: " + toArrayList(tests2[row]));
            System.out.println("   Length to remove: " + toRemove[row][col]);
            System.out.println("   End result should be: " + toArrayList(results[row][col]));
            System.out.println("   Your code calculated: " + resultList);
            return;
          }
          else {
            System.out.println("   Passed test "+ test);
          }
  
          test++;
        }
      }
    
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }
  
  public static ArrayList<String> toArrayList(String[] array) {
    ArrayList<String> list = new ArrayList<String>();
    for(String x: array)
      list.add(x);
    return list;
  }
  
  public static boolean listsEqual(ArrayList<String> a, ArrayList<String> b) {
    if(a.size() != b.size())
      return false;
      
    for(int i = 0; i < a.size(); i++) {
      if(!a.get(i).equals(b.get(i)))
        return false;
    }
    
    return true;
  }
}