Brandon Defrancis 6 lat temu
rodzic
commit
c04f2db381

+ 12
- 0
pom.xml Wyświetl plik

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

BIN
src/main/java/io/.DS_Store Wyświetl plik


+ 69
- 0
src/main/java/io/zipcoder/ParenChecker.java Wyświetl plik

@@ -1,4 +1,73 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.Stack;
4
+
3 5
 public class ParenChecker {
6
+
7
+    public static boolean parenChecker(String s) {
8
+
9
+        Stack<Character> stack = new Stack<Character>();
10
+        for (int i = 0; i < s.length(); i++) {
11
+            char c = s.charAt(i);
12
+            if (c == '(') {
13
+                stack.push(c);
14
+            } else if (c == ')') {
15
+                if (stack.isEmpty() || stack.pop() != '(') {
16
+                    return false;
17
+                }
18
+            }
19
+        }
20
+        return stack.isEmpty();
21
+    }
22
+
23
+    public static boolean allParenChecker(String s) {
24
+        int counter = 0;
25
+        int counter2 = 0;
26
+
27
+        Stack<Character> stack = new Stack<Character>();
28
+        for (int i = 0; i < s.length(); i++) {
29
+            char c = s.charAt(i);
30
+            if (c == '(' || c == '{' || c == '[' || c == '<') {
31
+                stack.push(c);
32
+            } else if (c == ')') {
33
+                if (stack.isEmpty() || stack.pop() != '(') {
34
+                    return false;
35
+                }
36
+            } else if (c == '}') {
37
+                if (stack.isEmpty() || stack.pop() != '{') {
38
+                    return false;
39
+                }
40
+            } else if (c == ']') {
41
+                if (stack.isEmpty() || stack.pop() != '[') {
42
+                    return false;
43
+                }
44
+            } else if (c == '>') {
45
+                if (stack.isEmpty() || stack.pop() != '<') {
46
+                    return false;
47
+                }
48
+            } else if (c == '"') {
49
+                if (counter > 0){
50
+                    if (stack.pop() != '"') {
51
+                        return false;
52
+                    }
53
+                } else {
54
+                    stack.push(c);
55
+                    counter++;
56
+                }
57
+
58
+            } else if (c == '\'') {
59
+                if (counter2 > 0) {
60
+                    if (stack.pop() != '\'') {
61
+                        return false;
62
+                    }
63
+                } else {
64
+                    stack.push(c);
65
+                    counter2++;
66
+                }
67
+            }
68
+        }
69
+            return stack.isEmpty();
70
+
71
+
72
+    }
4 73
 }

+ 39
- 5
src/main/java/io/zipcoder/WC.java Wyświetl plik

@@ -2,22 +2,56 @@ package io.zipcoder;
2 2
 
3 3
 import java.io.FileNotFoundException;
4 4
 import java.io.FileReader;
5
-import java.util.Iterator;
6
-import java.util.Scanner;
5
+import java.io.File;
6
+import java.util.*;
7 7
 
8 8
 public class WC {
9 9
     private Iterator<String> si;
10
+    private Map<String, Integer> wordCount = new HashMap<>();
10 11
 
11 12
     public WC(String fileName) {
13
+
14
+        fileName = WC.class.getResource("someTextFile.txt").getFile();
15
+
16
+
12 17
         try {
13
-            this.si = new Scanner(new FileReader(fileName));
18
+            Scanner sc = new Scanner(new FileReader(fileName));
19
+            sc.useDelimiter("[\\p{Punct}\\s]+");
20
+            this.si = sc;
14 21
         } catch (FileNotFoundException e) {
15 22
             System.out.println(fileName + " Does Not Exist");
16 23
             System.exit(-1);
17 24
         }
18
-    }
25
+
26
+        }
19 27
 
20 28
     public WC(Iterator<String> si) {
21 29
         this.si = si;
22 30
     }
23
-}
31
+
32
+    public void count(){
33
+        while (si.hasNext()) {
34
+            String words = si.next().toLowerCase();
35
+            if(wordCount.containsKey(words)){
36
+                Integer counter = wordCount.get(words);
37
+                wordCount.put(words, (counter++));
38
+            } else {
39
+                wordCount.put(words, 1);
40
+            }
41
+
42
+        }
43
+        wordCount.entrySet()
44
+                .stream()
45
+                .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
46
+                .forEach(System.out::println);
47
+    }
48
+
49
+    public static void main(String[] args) {
50
+        String file = "/Users/brandondefrancis/Labs/Wk3/CR-MesoLab-Collections-EncapsulativeCharacters/src/main/resources/someTextFile.txt";
51
+        String path =WC.class.getResource(file).getFile();
52
+        WC wc = new WC(path);
53
+        wc.count();
54
+
55
+    }
56
+
57
+}

+ 5028
- 0
src/main/resources/someTextFile.txt
Plik diff jest za duży
Wyświetl plik


+ 126
- 0
src/test/java/io/zipcoder/ParenCheckerTest.java Wyświetl plik

@@ -5,4 +5,130 @@ import org.junit.Test;
5 5
 
6 6
 public class ParenCheckerTest {
7 7
 
8
+    ParenChecker parenChecker = new ParenChecker();
9
+
10
+    @Test
11
+    public void parenCheckerTest() {
12
+        //given
13
+        String goodCheck = "()";
14
+
15
+        //when
16
+        boolean actual = parenChecker.parenChecker(goodCheck);
17
+
18
+        //then
19
+        Assert.assertTrue(actual);
20
+
21
+    }
22
+
23
+
24
+    @Test
25
+    public void parenCheckerTest1() {
26
+        //given
27
+        String badCheck = ")hello";
28
+
29
+        //when
30
+        boolean actual = parenChecker.parenChecker(badCheck);
31
+
32
+        //then
33
+        Assert.assertFalse(actual);
34
+
35
+    }
36
+
37
+
38
+    @Test
39
+    public void allParenCheckerTest() {
40
+        //given
41
+        String goodCheck = "{}";
42
+        //when
43
+        boolean actual = parenChecker.allParenChecker(goodCheck);
44
+
45
+        //then
46
+        Assert.assertTrue(actual);
47
+
48
+    }
49
+
50
+    @Test
51
+    public void allParenCheckerTest7() {
52
+        //given
53
+        String goodCheck = "{(<>)}";
54
+        //when
55
+        boolean actual = parenChecker.allParenChecker(goodCheck);
56
+
57
+        //then
58
+        Assert.assertTrue(actual);
59
+
60
+    }
61
+
62
+    @Test
63
+    public void allParenCheckerTest3() {
64
+        //given
65
+        String goodCheck = "\"\"";
66
+        //when
67
+        boolean actual = parenChecker.allParenChecker(goodCheck);
68
+
69
+        //then
70
+        Assert.assertTrue(actual);
71
+
72
+    }
73
+
74
+    @Test
75
+    public void allParenCheckerTest1() {
76
+        //given
77
+        String badCheck = "\"";
78
+        //when
79
+        boolean actual = parenChecker.allParenChecker(badCheck);
80
+
81
+        //then
82
+        Assert.assertFalse(actual);
83
+
84
+    }
85
+
86
+    @Test
87
+    public void allParenCheckerTest4() {
88
+        //given
89
+        String badCheck = "\'";
90
+        //when
91
+        boolean actual = parenChecker.allParenChecker(badCheck);
92
+
93
+        //then
94
+        Assert.assertFalse(actual);
95
+
96
+    }
97
+
98
+    @Test
99
+    public void allParenCheckerTest5() {
100
+        //given
101
+        String goodCheck = "\'\'";
102
+        //when
103
+        boolean actual = parenChecker.allParenChecker(goodCheck);
104
+
105
+        //then
106
+        Assert.assertTrue(actual);
107
+
108
+    }
109
+
110
+    @Test
111
+    public void allParenCheckerTest2() {
112
+        //given
113
+        String badCheck = "}hello";
114
+        //when
115
+        boolean actual = parenChecker.allParenChecker(badCheck);
116
+
117
+        //then
118
+        Assert.assertFalse(actual);
119
+
120
+    }
121
+
122
+    @Test
123
+    public void allParenCheckerTest6() {
124
+        //given
125
+        String badCheck = "{(})";
126
+        //when
127
+        boolean actual = parenChecker.allParenChecker(badCheck);
128
+
129
+        //then
130
+        Assert.assertFalse(actual);
131
+
132
+    }
133
+
8 134
 }