Browse Source

rough drafts of part two and wc code

mpierse 6 years ago
parent
commit
a983d4325e

+ 12
- 0
src/main/java/io/zipcoder/Main.java View File

@@ -0,0 +1,12 @@
1
+package io.zipcoder;
2
+
3
+public class Main {
4
+
5
+    public static void main(String[] args) {
6
+
7
+        WC we = new WC(WC.class.getResource("/filename").getFile());
8
+
9
+
10
+    }
11
+
12
+}

+ 29
- 6
src/main/java/io/zipcoder/ParenChecker.java View File

@@ -6,23 +6,23 @@ public class ParenChecker {
6 6
 
7 7
     public boolean isMatchingpair(char char1, char char2) {
8 8
 
9
-        if(char1 == '(' && char2 == ')')
10
-            return true;
9
+        if(char1 == '(' && char2 == ')' || char1 == '{' && char2 == '}' || char1 == '[' && char2 == ']' ||char1 == '<' && char2 == '>')
10
+        {return true;}
11 11
         else
12
-            return false;
12
+        {return false;}
13 13
     }
14 14
 
15
-    public boolean balancedParenthesis(String brackets) {
15
+    public boolean balPare(String brackets, char open, char close) {
16 16
 
17 17
         Stack st = new Stack();
18 18
 
19 19
         for(int i=0; i<brackets.length(); i++){
20 20
 
21
-            if(brackets.charAt(i) == '('){
21
+            if(brackets.charAt(i) == open){
22 22
                 st.push(brackets.charAt(i));
23 23
             }
24 24
 
25
-            if(brackets.charAt(i)== ')'){
25
+            if(brackets.charAt(i)== close){
26 26
                 if (st.isEmpty()){
27 27
                     return false;
28 28
                 } else if (!isMatchingpair((Character) st.pop(), brackets.charAt(i))){
@@ -36,4 +36,27 @@ public class ParenChecker {
36 36
             return false;
37 37
         }
38 38
     }
39
+
40
+    public boolean balancedCharacters(String brackets) {
41
+
42
+        int singleCount = 0;
43
+        int doubleCount = 0;
44
+
45
+        for (int i = 0; i < brackets.length(); i++) {
46
+            if (brackets.charAt(i) == '\'') { singleCount++;}
47
+            else if(brackets.charAt(i) == '"'){doubleCount++;}
48
+        }
49
+
50
+        boolean parenth = balPare(brackets, '(', ')');
51
+        boolean square = balPare(brackets, '[', ']');
52
+        boolean curly = balPare(brackets, '{', '}');
53
+        boolean angle = balPare(brackets, '<', '>');
54
+
55
+        if (parenth &&  square && curly && angle && singleCount%2 == 0 && doubleCount%2 == 0) {
56
+            return true;
57
+        } else {return false;}
58
+
59
+    }
60
+
61
+
39 62
 }

+ 15
- 0
src/main/java/io/zipcoder/WC.java View File

@@ -1,8 +1,11 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.io.File;
3 4
 import java.io.FileNotFoundException;
4 5
 import java.io.FileReader;
6
+import java.util.HashMap;
5 7
 import java.util.Iterator;
8
+import java.util.Map;
6 9
 import java.util.Scanner;
7 10
 
8 11
 public class WC {
@@ -15,9 +18,21 @@ public class WC {
15 18
             System.out.println(fileName + " Does Not Exist");
16 19
             System.exit(-1);
17 20
         }
21
+        Map<String, Integer> countWords = new HashMap<String, Integer>();
22
+        while (si.hasNext()){
23
+            String next = si.next();
24
+            Integer count = countWords.get(next);
25
+            if (count != null) {
26
+                countWords.put(next, count + 1);
27
+            } else {
28
+                countWords.put(next, 1);
29
+            }
30
+        }
31
+
18 32
     }
19 33
 
20 34
     public WC(Iterator<String> si) {
21 35
         this.si = si;
22 36
     }
37
+
23 38
 }

+ 25
- 2
src/test/java/io/zipcoder/ParenCheckerTest.java View File

@@ -28,7 +28,7 @@ public class ParenCheckerTest {
28 28
     public void areParenBalancedTrueTest(){
29 29
         String brackets = "(((()()))())";
30 30
         ParenChecker checker = new ParenChecker();
31
-        boolean actual = checker.balancedParenthesis(brackets);
31
+        boolean actual = checker.balPare(brackets, '(', ')');
32 32
         Assert.assertTrue(actual);
33 33
     }
34 34
 
@@ -36,8 +36,31 @@ public class ParenCheckerTest {
36 36
     public void areParenBalancedFalseTest(){
37 37
         String brackets = "))))))";
38 38
         ParenChecker checker = new ParenChecker();
39
-        boolean actual = checker.balancedParenthesis(brackets);
39
+        boolean actual = checker.balPare(brackets, '(', ')');
40 40
         Assert.assertFalse(actual);
41 41
     }
42 42
 
43
+    @Test
44
+    public void balancedCharacterTestEven(){
45
+        String brackets = "({}[)('')]";
46
+        ParenChecker checker = new ParenChecker();
47
+        boolean actual = checker.balancedCharacters(brackets);
48
+        Assert.assertFalse(actual);
49
+    }
50
+
51
+    @Test
52
+    public void balancedCharacterTestOdd(){
53
+        String brackets = "({}[('')";
54
+        ParenChecker checker = new ParenChecker();
55
+        boolean actual = checker.balancedCharacters(brackets);
56
+        Assert.assertFalse(actual);
57
+    }
58
+
59
+    @Test
60
+    public void balancedCharacterTestTrueeven(){
61
+        String brackets = "([{''}{\"\"}])";
62
+        ParenChecker checker = new ParenChecker();
63
+        boolean actual = checker.balancedCharacters(brackets);
64
+        Assert.assertTrue(actual);
65
+    }
43 66
 }