@matfah/

Scramble Word FR 2014

Java

No description

fork
loading
Files
  • Main.java
  • ClassNotShown.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
150
import java.util.*;

public class Main {
  public static void main(String[] args) {
    for(int i = 0; i < 10; i++)
      testScrambleWord();
    for(int i = 0; i < 10; i++)
      testScrambleOrRemove(i);
      
    System.out.println("All tests passed!");
  }
  
  public static void testScrambleWord() {
    
    for(int length = 0; length < 20; length++) {
      
      String toReverse = "";
      String reversed = "";
      boolean flipNext = false;
      
      for(int i = 0; i < length; i++) {
        
        char letter;
        if(Math.random() < 0.25)
          letter = 'A';
        else
          letter = (char)('B' + (int)(Math.random()*25));
        toReverse += letter;
        
        if(letter == 'A') { //flip the next 
          if(flipNext) { //double A
            reversed += "A";
          }
          else {
            flipNext = true;
          }
          
          if(i == length - 1)
              reversed += "A";
        }
        else {
          reversed += letter;
          if(flipNext) {
            reversed += "A";
            flipNext = false;
          }
        }
        
        
      }
      
      ClassNotShown theClass = new ClassNotShown();
      
      String result = null;
      try {
        result = theClass.scrambleWord(toReverse);
      }
      catch(Exception e) {
        System.out.println("Error calling scrambleWord(\""+ toReverse + "\")");
        System.exit(0);
      }
      
      if(!reversed.equals(result)) {
        System.out.println("Result of scrambleWord(" + toReverse + ") should be " + reversed + ", but result is " + result);
        System.exit(0);
      }
    }
  }
  
  public static void testScrambleOrRemove(int wordCount) {
    
    ArrayList<String> wordsList = new ArrayList<String>();
    ArrayList<String> wordsList2 = new ArrayList<String>();
    ArrayList<String> changedList = new ArrayList<String>();
    
    for(int words = 0; words < wordCount; words++) {
    
      int length = (int)(Math.random()*20);

      String toReverse = "";
      String reversed = "";
      boolean flipNext = false;
      
      for(int i = 0; i < length; i++) {
        
        char letter;
        if(Math.random() < 0.25)
          letter = 'A';
        else
          letter = (char)('B' + (int)(Math.random()*25));
        toReverse += letter;
        
        if(letter == 'A') { //flip the next 
          if(flipNext) { //double A
            reversed += "A";
          }
          else {
            flipNext = true;
          }
          
          if(i == length - 1)
              reversed += "A";
        }
        else {
          reversed += letter;
          if(flipNext) {
            reversed += "A";
            flipNext = false;
          }
        }
        
        
      }
      wordsList.add(toReverse);
      wordsList2.add(toReverse);
      if(!toReverse.equals(reversed))
        changedList.add(reversed);
    }
    
    CustomClassNotShown theClass = new CustomClassNotShown();
    
    try {
      theClass.scrambleOrRemove(wordsList);
    }
    catch(Exception e) {
      System.out.println("Error calling scrambleOrRemove(\""+ wordsList +"\")");
      System.exit(0);
    }
    
    if(!wordsList.equals(changedList)) {
      System.out.println("Result of calling scrambleOrRemove(" + wordsList2 + ") is " + wordsList + ", should be " + changedList);
      System.exit(0);
    }
    
    for(String x : wordsList2) {
      if(!theClass.called.contains(x)) {
        System.out.println("scrambleOrRemove() does not call scrambleWord() for all words in the wordList");
        System.exit(0);
      }
    }
  }
}

class CustomClassNotShown extends ClassNotShown {
    ArrayList<String> called = new ArrayList<String>();
    public String scrambleWord(String word) {
      called.add(word);
      return super.scrambleWord(word);
    }
}