Ver código fonte

Characters encapsulated

NedRedmond 6 anos atrás
pai
commit
fa8be8f3a0

+ 13
- 0
pom.xml Ver arquivo

@@ -8,6 +8,19 @@
8 8
     <artifactId>collections</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10 10
 
11
+    <build>
12
+        <plugins>
13
+            <plugin>
14
+                <groupId>org.apache.maven.plugins</groupId>
15
+                <artifactId>maven-compiler-plugin</artifactId>
16
+                <configuration>
17
+                    <source>1.8</source>
18
+                    <target>1.8</target>
19
+                </configuration>
20
+            </plugin>
21
+        </plugins>
22
+    </build>
23
+
11 24
     <dependencies>
12 25
         <dependency>
13 26
             <groupId>junit</groupId>

+ 78
- 0
src/main/java/io/zipcoder/PairChecker.java Ver arquivo

@@ -0,0 +1,78 @@
1
+package io.zipcoder;
2
+
3
+import java.util.*;
4
+
5
+public class PairChecker {
6
+
7
+    private Stack<Character> openers = new Stack<>();
8
+
9
+    private static final Map<Character, Character> pairs = createMap();
10
+
11
+    private static Map<Character, Character> createMap() {
12
+        Map<Character, Character> pairs = new HashMap<Character, Character>();
13
+        pairs.put(')', '(');
14
+        pairs.put('}', '{');
15
+        pairs.put(']', '[');
16
+        pairs.put('>', '<');
17
+        pairs.put('"', '"');
18
+        pairs.put('\'', '\'');
19
+        return pairs;
20
+    }
21
+
22
+    public boolean checkPairs(String textInput) {
23
+        char[] charArray = textInput.toCharArray();
24
+
25
+        // loop through characters in string
26
+        for (char character : charArray) {
27
+            // if opener is found
28
+            if (pairs.containsValue(character)) {
29
+                // push it onto the stack
30
+                openers.push(character);
31
+                // if closer is found
32
+            } else if (pairs.containsKey(character)) {
33
+                // && no opener is stored
34
+                if (openers.isEmpty() || openers.peek() != pairs.get(character)) {
35
+                    return false;
36
+                    // && opener if at top of stack
37
+                } else {
38
+                    // pop that opener off the stack
39
+                    openers.pop();
40
+                    // otherwise, closer is improperly nested
41
+                }
42
+            }
43
+        }
44
+        //        } else if (pairs.containsKey(character)) {
45
+        //            // && no opener is stored
46
+        //            if (pairs.isEmpty()) {
47
+        //                return false;
48
+        //                // && opener if at top of stack
49
+        //            } else if (openers.peek() == pairs.get(character)) {
50
+        //                // pop that opener off the stack
51
+        //                openers.pop();
52
+        //                // otherwise, closer is improperly nested
53
+        //            } else {
54
+        //                // so we return false
55
+        //                return false;
56
+        //            }
57
+        //        }
58
+
59
+        // parens checker
60
+//        for (char character : charArray) {
61
+//            if (character == '(') {
62
+//                this.openers.push(character);
63
+//            } else if (character == ')' && openers.empty() == true) {
64
+//                return false;
65
+//            } else if (character == ')' && openers.peek() == '(') {
66
+//                this.openers.pop();
67
+//            } else {
68
+//                continue;
69
+//            }
70
+//        }
71
+
72
+        if (openers.empty()) {
73
+            return true;
74
+        } else {
75
+            return false;
76
+        }
77
+    }
78
+}

+ 22
- 0
src/main/java/io/zipcoder/ParenChecker.java Ver arquivo

@@ -1,4 +1,26 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.Stack;
4
+
3 5
 public class ParenChecker {
6
+
7
+    Stack<Character> parens = new Stack<>();
8
+
9
+    public boolean checkParens(String textInput) {
10
+        char[] charArray = textInput.toCharArray();
11
+
12
+        for (char character : charArray) {
13
+            if (character == '(') {
14
+                this.parens.push(character);
15
+            } else if (character == ')' && parens.empty() == true) {
16
+                return false;
17
+            } else if (character == ')' && parens.peek() == '(') {
18
+                this.parens.pop();
19
+            } else {
20
+                continue;
21
+            }
22
+        }
23
+
24
+        if (parens.empty()) { return true; } else { return false; }
25
+    }
4 26
 }

+ 24
- 0
src/main/java/io/zipcoder/WC.java Ver arquivo

@@ -3,10 +3,15 @@ package io.zipcoder;
3 3
 import java.io.FileNotFoundException;
4 4
 import java.io.FileReader;
5 5
 import java.util.Iterator;
6
+import java.util.Map;
6 7
 import java.util.Scanner;
8
+import java.util.TreeMap;
9
+
10
+import static java.util.Map.Entry.comparingByValue;
7 11
 
8 12
 public class WC {
9 13
     private Iterator<String> si;
14
+    TreeMap<String, Integer> wordData = new TreeMap<String, Integer>();
10 15
 
11 16
     public WC(String fileName) {
12 17
         try {
@@ -20,4 +25,23 @@ public class WC {
20 25
     public WC(Iterator<String> si) {
21 26
         this.si = si;
22 27
     }
28
+
29
+    public void wordCounter() {
30
+        String word;
31
+
32
+        while (si.hasNext()) {
33
+            String rawWord = si.next().toLowerCase();
34
+            word = rawWord.replaceAll("[^a-z]", "");
35
+
36
+            if (wordData.containsKey(word)) {
37
+                wordData.put(word, wordData.get(word)+1);
38
+            } else {
39
+                wordData.put(word, 1);
40
+            }
41
+        }
42
+
43
+        wordData.entrySet().stream()
44
+                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
45
+                .forEach(entry -> System.out.println(String.format("%15d    %s\n", entry.getValue(), entry.getKey())));
46
+    }
23 47
 }

+ 75
- 0
src/main/java/io/zipcoder/test.java Ver arquivo

@@ -0,0 +1,75 @@
1
+package io.zipcoder;// File: WordCounter.java
2
+// Program from Section 5.7 to illustrate the use of TreeMaps and Iterators.
3
+// The program opens and reads a file called words.txt. Each line in this
4
+// file should consist of one or more English words separated by spaces.
5
+// The end of each line must not have any extra spaces after the last word.
6
+// The program reads the file and then a table is printed of
7
+// all words and their counts.
8
+
9
+import java.util.*;  // Provides TreeMap, Iterator, Scanner
10
+import java.io.*;    // Provides FileReader, FileNotFoundException
11
+
12
+public class test
13
+{
14
+    public static void main(String[ ] args)
15
+    {
16
+        TreeMap<String, Integer> frequencyData = new TreeMap<String, Integer>( );
17
+
18
+        readWordFile(frequencyData);
19
+        printAllCounts(frequencyData);
20
+    }
21
+
22
+    public static int getCount
23
+            (String word, TreeMap<String, Integer> frequencyData)
24
+    {
25
+        if (frequencyData.containsKey(word))
26
+        {  // The word has occurred before, so get its count from the map
27
+            return frequencyData.get(word); // Auto-unboxed
28
+        }
29
+        else
30
+        {  // No occurrences of this word
31
+            return 0;
32
+        }
33
+    }
34
+
35
+    public static void printAllCounts(TreeMap<String, Integer> frequencyData)
36
+    {
37
+        System.out.println("-----------------------------------------------");
38
+        System.out.println("    Occurrences    Word");
39
+
40
+        for(String word : frequencyData.keySet( ))
41
+        {
42
+            System.out.printf("%15d    %s\n", frequencyData.get(word), word);
43
+        }
44
+
45
+        System.out.println("-----------------------------------------------");
46
+    }
47
+
48
+    public static void readWordFile(TreeMap<String, Integer> frequencyData)
49
+    {
50
+        Scanner wordFile;
51
+        String word;     // A word read from the file
52
+        Integer count;   // The number of occurrences of the word
53
+
54
+        try
55
+        {
56
+            wordFile = new Scanner(new FileReader("words.txt"));
57
+        }
58
+        catch (FileNotFoundException e)
59
+        {
60
+            System.err.println(e);
61
+            return;
62
+        }
63
+
64
+        while (wordFile.hasNext( ))
65
+        {
66
+            // Read the next word and get rid of the end-of-line marker if needed:
67
+            word = wordFile.next( );
68
+
69
+            // Get the current count of this word, add one, and then store the new count:
70
+            count = getCount(word, frequencyData) + 1;
71
+            frequencyData.put(word, count);
72
+        }
73
+    }
74
+
75
+}

+ 36484
- 0
src/main/resources/28054-0.txt
Diferenças do arquivo suprimidas por serem muito extensas
Ver arquivo


+ 78
- 0
src/test/java/io/zipcoder/PairCheckerTest.java Ver arquivo

@@ -0,0 +1,78 @@
1
+package io.zipcoder;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class PairCheckerTest {
7
+
8
+    @Test public void singlePairCheckerTrue() {
9
+        PairChecker pairChecker = new PairChecker();
10
+
11
+        String textTest = "Testing (this jawn)";
12
+        boolean expected = true;
13
+        boolean actual = pairChecker.checkPairs(textTest);
14
+
15
+        Assert.assertEquals(expected, actual);
16
+    }
17
+
18
+    @Test public void singlePairCheckerFalse() {
19
+        PairChecker pairChecker = new PairChecker();
20
+
21
+        String textTest = "Testing (((((({[[[[[{{{[[[{{{{(((({(this jawn";
22
+        boolean expected = false;
23
+        boolean actual = pairChecker.checkPairs(textTest);
24
+
25
+        Assert.assertEquals(expected, actual);
26
+    }
27
+
28
+    @Test public void doublePairCheckerTrue() {
29
+        PairChecker pairChecker = new PairChecker();
30
+
31
+        String textTest = "{Testing [this] jawn}";
32
+        boolean expected = true;
33
+        boolean actual = pairChecker.checkPairs(textTest);
34
+
35
+        Assert.assertEquals(expected, actual);
36
+    }
37
+
38
+    @Test public void doublePairCheckerFalse() {
39
+        PairChecker pairChecker = new PairChecker();
40
+
41
+        String textTest = "Testing (this (jawn)";
42
+        boolean expected = false;
43
+        boolean actual = pairChecker.checkPairs(textTest);
44
+
45
+        Assert.assertEquals(expected, actual);
46
+    }
47
+
48
+    @Test public void wildPairCheckerTrue() {
49
+        PairChecker pairChecker = new PairChecker();
50
+
51
+        String textTest = "Testing (this (jawn)) And (((this))) jawn";
52
+        boolean expected = true;
53
+        boolean actual = pairChecker.checkPairs(textTest);
54
+
55
+        Assert.assertEquals(expected, actual);
56
+    }
57
+
58
+    @Test public void wildPairCheckerFalse() {
59
+        PairChecker pairChecker = new PairChecker();
60
+
61
+        String textTest = "Testing (this (jawn)) And ((this))) jawn";
62
+        boolean expected = false;
63
+        boolean actual = pairChecker.checkPairs(textTest);
64
+
65
+        Assert.assertEquals(expected, actual);
66
+    }
67
+
68
+    @Test public void lonePairCheckerFalse() {
69
+        PairChecker pairChecker = new PairChecker();
70
+
71
+        String textTest = ")";
72
+        boolean expected = false;
73
+        boolean actual = pairChecker.checkPairs(textTest);
74
+
75
+        Assert.assertEquals(expected, actual);
76
+    }
77
+
78
+}

+ 62
- 0
src/test/java/io/zipcoder/ParenCheckerTest.java Ver arquivo

@@ -1,8 +1,70 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 import org.junit.Assert;
4
+import org.junit.Before;
4 5
 import org.junit.Test;
5 6
 
6 7
 public class ParenCheckerTest {
7 8
 
9
+    @Test public void singlePairCheckerTrue() {
10
+        ParenChecker parenChecker = new ParenChecker();
11
+
12
+        String textTest = "\"Testin\" {[](this 'jawn')}";
13
+        boolean expected = true;
14
+        boolean actual = parenChecker.checkParens(textTest);
15
+
16
+        Assert.assertEquals(expected, actual);
17
+    }
18
+
19
+    @Test public void singlePairCheckerFalse() {
20
+        ParenChecker parenChecker = new ParenChecker();
21
+
22
+        String textTest = "{Testing} (this [jawn]";
23
+        boolean expected = false;
24
+        boolean actual = parenChecker.checkParens(textTest);
25
+
26
+        Assert.assertEquals(expected, actual);
27
+    }
28
+
29
+    @Test public void doublePairCheckerTrue() {
30
+        ParenChecker parenChecker = new ParenChecker();
31
+
32
+        String textTest = "Testing (this (jawn))";
33
+        boolean expected = true;
34
+        boolean actual = parenChecker.checkParens(textTest);
35
+
36
+        Assert.assertEquals(expected, actual);
37
+    }
38
+
39
+    @Test public void doublePairCheckerFalse() {
40
+        ParenChecker parenChecker = new ParenChecker();
41
+
42
+        String textTest = "Testing (this (jawn)";
43
+        boolean expected = false;
44
+        boolean actual = parenChecker.checkParens(textTest);
45
+
46
+        Assert.assertEquals(expected, actual);
47
+    }
48
+
49
+    @Test public void wildPairCheckerTrue() {
50
+        ParenChecker parenChecker = new ParenChecker();
51
+
52
+        String textTest = "Testing (this (jawn)) And (((this))) jawn";
53
+        boolean expected = true;
54
+        boolean actual = parenChecker.checkParens(textTest);
55
+
56
+        Assert.assertEquals(expected, actual);
57
+    }
58
+
59
+    @Test public void wildPairCheckerFalse() {
60
+        ParenChecker parenChecker = new ParenChecker();
61
+
62
+        String textTest = "Testing (this (jawn)) And ((this))) jawn";
63
+        boolean expected = false;
64
+        boolean actual = parenChecker.checkParens(textTest);
65
+
66
+        Assert.assertEquals(expected, actual);
67
+    }
68
+
69
+
8 70
 }

+ 5
- 0
src/test/java/io/zipcoder/WCTest.java Ver arquivo

@@ -8,4 +8,9 @@ import java.util.Arrays;
8 8
 
9 9
 public class WCTest {
10 10
 
11
+    @Test public void test() {
12
+        WC wc = new WC(WC.class.getResource("/28054-0.txt").getFile());
13
+        wc.wordCounter();
14
+    }
15
+
11 16
 }