From 354c6974d1f6f68988e3833b484e680af9dc382b Mon Sep 17 00:00:00 2001 From: Kthomas Date: Tue, 13 Mar 2018 20:32:54 -0400 Subject: [PATCH 1/4] Part 1 and 2 Done --- pom.xml | 12 ++++++ src/main/java/io/zipcoder/ParenChecker.java | 36 ++++++++++++++++++ src/main/java/io/zipcoder/WC.java | 41 ++++++++++++++++++++- 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index e66b725..5c3991d 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ io.zipcoder collections 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.7 + 1.7 + + + + diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index caee675..6ec8681 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,40 @@ package io.zipcoder; +import java.util.ArrayList; +import java.util.Stack; + public class ParenChecker { + public ParenChecker(){ + } + + public boolean braceChecker(String str) { + Stack stack = new Stack(); + for (int i = 0; i < str.length(); i++) { + char openCharacter = str.charAt(i); + if (openCharacter == '[' || openCharacter == '(' || openCharacter == '{' || openCharacter == '<' || openCharacter == '"') { + stack.push(openCharacter); + } else if (openCharacter == ']') { + if (stack.isEmpty() || stack.pop() != '[') { + return false; + } + } else if (openCharacter == ')') { + if (stack.isEmpty() || stack.pop() != '(') { + return false; + } + } else if (openCharacter == '}') { + if (stack.isEmpty() || stack.pop() != '{') { + return false; + } + } else if (openCharacter == '>') { + if (stack.isEmpty() || stack.pop() != '<') { + return false; + } + } else if (openCharacter == '"') { + if (stack.isEmpty() || stack.pop() != '"') { + return false; + } + } + } + return stack.isEmpty(); + } } diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index babb68c..dd2e324 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,8 +2,18 @@ import java.io.FileNotFoundException; import java.io.FileReader; -import java.util.Iterator; -import java.util.Scanner; +import java.util.*; + +/** +@TODO +*@1.)Create a parenthesis verifier method, that has one job(check if all parenthesis are ('paired') +* @2.)Create a method that checks all opening characters have a closing one +* - These Characters () {} [] <> "" '' +* This program should be able to: + * 1 - count all the words in a file + * 2 - Print out all the words + * 3 - Print the word counts in descending order + */ public class WC { private Iterator si; @@ -21,3 +31,30 @@ public WC(Iterator si) { this.si = si; } } + +// private static boolean isPaired(String str) { +// /*stores braces*/ +// Map bracketPairs = new HashMap(){{ +// put('(', ')'); +// put('{', '}'); +// put('[', ']'); +// put('<', '>'); +// put('"', '"'); +// } +// }; +// +// if(str.length() % 2 != 0) { //a pair will donate a balance of at least 2 characters/braces +// return false; +// } +// +// Stack openBrace = new Stack<>(); //store the open brace +// for(int i = 0; i < str.length(); i++){ +// if(bracketPairs.containsKey(str.charAt(i))){ //if the character is open, +// openBrace.push(str.charAt(i)); // store it into the stack +// } +// else if(openBrace.isEmpty() || bracketPairs.get(openBrace.pop()) != str.charAt(i)){ //if the closing character doesnt match the top of the stack or is empty, return false +// return false; +// } +// } +// return openBrace.isEmpty() ? true : false; +// } From 25eea1b5f7a37206a886be2bc299a42b49f074e3 Mon Sep 17 00:00:00 2001 From: Kthomas Date: Wed, 14 Mar 2018 13:35:29 -0400 Subject: [PATCH 2/4] Workin on map for WC --- src/main/java/io/zipcoder/ParenChecker.java | 22 ++- src/main/java/io/zipcoder/WC.java | 38 +---- .../java/io/zipcoder/ParenCheckerTest.java | 156 ++++++++++++++++++ 3 files changed, 180 insertions(+), 36 deletions(-) diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index 6ec8681..51b5d76 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -4,14 +4,14 @@ import java.util.Stack; public class ParenChecker { - public ParenChecker(){ + public ParenChecker() { } public boolean braceChecker(String str) { - Stack stack = new Stack(); + Stack stack = new Stack<>(); for (int i = 0; i < str.length(); i++) { char openCharacter = str.charAt(i); - if (openCharacter == '[' || openCharacter == '(' || openCharacter == '{' || openCharacter == '<' || openCharacter == '"') { + if (openCharacter == '[' || openCharacter == '(' || openCharacter == '{' || openCharacter == '<') { stack.push(openCharacter); } else if (openCharacter == ']') { if (stack.isEmpty() || stack.pop() != '[') { @@ -29,12 +29,20 @@ public boolean braceChecker(String str) { if (stack.isEmpty() || stack.pop() != '<') { return false; } - } else if (openCharacter == '"') { - if (stack.isEmpty() || stack.pop() != '"') { - return false; + } else if (openCharacter == '\"') { + if (stack.isEmpty() || stack.peek() != '\"') { + stack.push(openCharacter); + } else { + stack.pop(); + } + } else if (openCharacter == '\'') { + if (stack.isEmpty() || stack.peek() != '\'') { + stack.push(openCharacter); + } else { + stack.pop(); } } } return stack.isEmpty(); } -} +} \ No newline at end of file diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index dd2e324..5b77eee 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -1,12 +1,14 @@ package io.zipcoder; +import jdk.nashorn.internal.ir.annotations.Ignore; + import java.io.FileNotFoundException; import java.io.FileReader; import java.util.*; /** @TODO -*@1.)Create a parenthesis verifier method, that has one job(check if all parenthesis are ('paired') +*@1.)Create a parenthesis verifier method, that can (check if all parenthesis are ('paired') * @2.)Create a method that checks all opening characters have a closing one * - These Characters () {} [] <> "" '' * This program should be able to: @@ -20,7 +22,7 @@ public class WC { public WC(String fileName) { try { - this.si = new Scanner(new FileReader(fileName)); + this.si = new Scanner(new FileReader(fileName)).useDelimiter("[^a-zA-Z]+"); } catch (FileNotFoundException e) { System.out.println(fileName + " Does Not Exist"); System.exit(-1); @@ -30,31 +32,9 @@ public WC(String fileName) { public WC(Iterator si) { this.si = si; } -} -// private static boolean isPaired(String str) { -// /*stores braces*/ -// Map bracketPairs = new HashMap(){{ -// put('(', ')'); -// put('{', '}'); -// put('[', ']'); -// put('<', '>'); -// put('"', '"'); -// } -// }; -// -// if(str.length() % 2 != 0) { //a pair will donate a balance of at least 2 characters/braces -// return false; -// } -// -// Stack openBrace = new Stack<>(); //store the open brace -// for(int i = 0; i < str.length(); i++){ -// if(bracketPairs.containsKey(str.charAt(i))){ //if the character is open, -// openBrace.push(str.charAt(i)); // store it into the stack -// } -// else if(openBrace.isEmpty() || bracketPairs.get(openBrace.pop()) != str.charAt(i)){ //if the closing character doesnt match the top of the stack or is empty, return false -// return false; -// } -// } -// return openBrace.isEmpty() ? true : false; -// } + public Map countWords() { + Map expectedCount = new HashMap<>(); + si.next() + } +} \ No newline at end of file diff --git a/src/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index 76aa3b6..68e09e6 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -4,5 +4,161 @@ import org.junit.Test; public class ParenCheckerTest { + ParenChecker parenChecker = new ParenChecker(); + + @Test + public void isPaired1Test(){ + //Given + String strToCheck = "(UltraInstinct)"; + Boolean expected = true; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isNotPaired1Test(){ + //Given + String strToCheck = "(UltraInstinct"; + Boolean expected = false; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isPaired2Test(){ + //Given + String strToCheck = "{}UltraInstinct"; + Boolean expected = true; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isNotPaired2Test(){ + //Given + String strToCheck = "UltraInstinct}"; + Boolean expected = false; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isPaired3Test(){ + //Given + String strToCheck = "[Ultra]Instinct"; + Boolean expected = true; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isNotPaired3Test(){ + //Given + String strToCheck = "UltraInstin]ct"; + Boolean expected = false; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isPaired4Test(){ + //Given + String strToCheck = "Ultra<>Instinct"; + Boolean expected = true; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isNotPaired4Test(){ + //Given + String strToCheck = "UltraInstinc>>t"; + Boolean expected = false; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + @Test + public void isPaired5Test(){ + //Given + String strToCheck = "Ultra\"Instinc\"t"; + Boolean expected = true; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isNotPaired5Test(){ + //Given + String strToCheck = "Ultra\"Instinct"; + Boolean expected = false; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isPaired6Test(){ + //Given + String strToCheck = "Ult\'raIn\'stinct"; + Boolean expected = true; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } + + @Test + public void isNotPaired6Test(){ + //Given + String strToCheck = "Ultra\'Inst\'inct\'"; + Boolean expected = false; + + //When + boolean actual = parenChecker.braceChecker(strToCheck); + + //Then + Assert.assertEquals(expected,actual); + } } \ No newline at end of file From bdf4c7c028f0fe53ebffdb96982cf8fa55f3acea Mon Sep 17 00:00:00 2001 From: Kthomas Date: Wed, 14 Mar 2018 17:35:02 -0400 Subject: [PATCH 3/4] Finished the scanner..maybe --- pom.xml | 4 ++-- src/main/java/io/zipcoder/WC.java | 33 ++++++++++++++++++++-------- src/main/resources/aGoodBookFile.txt | 0 3 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 src/main/resources/aGoodBookFile.txt diff --git a/pom.xml b/pom.xml index 5c3991d..efb68c4 100644 --- a/pom.xml +++ b/pom.xml @@ -13,8 +13,8 @@ org.apache.maven.plugins maven-compiler-plugin - 1.7 - 1.7 + 1.8 + 1.8 diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index 5b77eee..3fa72bc 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -1,7 +1,5 @@ package io.zipcoder; -import jdk.nashorn.internal.ir.annotations.Ignore; - import java.io.FileNotFoundException; import java.io.FileReader; import java.util.*; @@ -19,22 +17,39 @@ public class WC { private Iterator si; + private TreeMap book; public WC(String fileName) { - try { + try { //As soon as the scanner recieves my book(map) it will get + this.book = new TreeMap<>(); this.si = new Scanner(new FileReader(fileName)).useDelimiter("[^a-zA-Z]+"); + this.metaCharRemover(); } catch (FileNotFoundException e) { - System.out.println(fileName + " Does Not Exist"); + System.out.println(fileName + "Does Not Exist"); System.exit(-1); } } - public WC(Iterator si) { - this.si = si; + public static void main(String[] args) { + WC wordCounts = new WC(WC.class.getResource("/").getFile()); + wordCounts.descendAndPrintText(); + } + + public WC(Iterator stringIterator) { + this.si = stringIterator; + } + + private void metaCharRemover() { + while (si.hasNext()) { //while the iterator has the next line do this stuff below it + String word = si.next().toLowerCase().replaceAll("[\"^${}().+&~!@#%*]", ""); //change all characters within file to lower case and replace all metacharacters with nothing so that only the words are counted when its time to count + Integer wordCount = book.getOrDefault(word, 0); //set my default value for word count to 0 + book.put(word, (wordCount + 1)); //insert my word and word count into my value + } } - public Map countWords() { - Map expectedCount = new HashMap<>(); - si.next() + private void descendAndPrintText() { //stream is pulling out JUST my words in sequential order first before sorting them in descending order, reverse is so that the highest number will print first and get lower accordingly + String newBook = ""; + newBook += book.entrySet().stream().sorted(Map.Entry.comparingByValue().reversed()) + "\n"; + System.out.println(newBook); } } \ No newline at end of file diff --git a/src/main/resources/aGoodBookFile.txt b/src/main/resources/aGoodBookFile.txt new file mode 100644 index 0000000..e69de29 From 7e5f475bc47326e720bd8ccdad4c436e8ed2e4b5 Mon Sep 17 00:00:00 2001 From: Kthomas Date: Wed, 14 Mar 2018 19:01:58 -0400 Subject: [PATCH 4/4] needs refactoring, but done, sort not working though --- src/main/java/io/zipcoder/WC.java | 19 +++++------ .../{someTextFile.txt => LesMiserables.txt} | 0 src/main/resources/aGoodBookFile.txt | 1 + src/test/java/io/zipcoder/WCTest.java | 32 ++++++++++++------- 4 files changed, 32 insertions(+), 20 deletions(-) rename src/main/resources/{someTextFile.txt => LesMiserables.txt} (100%) diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index 3fa72bc..4bf4619 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -3,6 +3,7 @@ import java.io.FileNotFoundException; import java.io.FileReader; import java.util.*; +import java.util.TreeMap; /** @TODO @@ -19,8 +20,13 @@ public class WC { private Iterator si; private TreeMap book; - public WC(String fileName) { - try { //As soon as the scanner recieves my book(map) it will get + public static void main(String[] args) { + WC wordCounts = new WC("/Users/kieranthomas/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/aGoodBookFile.txt"); + wordCounts.descendAndPrintText(); + } + + public WC(String fileName) { //nullary constructor + try { //As soon as the scanner receives my book(map) it will get this.book = new TreeMap<>(); this.si = new Scanner(new FileReader(fileName)).useDelimiter("[^a-zA-Z]+"); this.metaCharRemover(); @@ -30,20 +36,15 @@ public WC(String fileName) { } } - public static void main(String[] args) { - WC wordCounts = new WC(WC.class.getResource("/").getFile()); - wordCounts.descendAndPrintText(); - } - public WC(Iterator stringIterator) { this.si = stringIterator; } - private void metaCharRemover() { + public void metaCharRemover() { while (si.hasNext()) { //while the iterator has the next line do this stuff below it String word = si.next().toLowerCase().replaceAll("[\"^${}().+&~!@#%*]", ""); //change all characters within file to lower case and replace all metacharacters with nothing so that only the words are counted when its time to count Integer wordCount = book.getOrDefault(word, 0); //set my default value for word count to 0 - book.put(word, (wordCount + 1)); //insert my word and word count into my value + book.put(word, (wordCount + 1)); //insert my word and word count into my map } } diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/LesMiserables.txt similarity index 100% rename from src/main/resources/someTextFile.txt rename to src/main/resources/LesMiserables.txt diff --git a/src/main/resources/aGoodBookFile.txt b/src/main/resources/aGoodBookFile.txt index e69de29..afe5c10 100644 --- a/src/main/resources/aGoodBookFile.txt +++ b/src/main/resources/aGoodBookFile.txt @@ -0,0 +1 @@ +Mary had a little lamb. Mary is 24 years old. \ No newline at end of file diff --git a/src/test/java/io/zipcoder/WCTest.java b/src/test/java/io/zipcoder/WCTest.java index 895e831..53665c8 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -1,11 +1,21 @@ -package io.zipcoder; - -import org.junit.Assert; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.Arrays; - -public class WCTest { - -} \ No newline at end of file +//package io.zipcoder; +// +//import org.junit.Assert; +//import org.junit.Test; +// +//import java.util.ArrayList; +//import java.util.Arrays; +// +//public class WCTest { +// +// @Test +// public void test() { +// WC wc = new WC("/Users/kieranthomas/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/aGoodBookFile.txt"); +// +// String exptected = ""; +// String actual = wc.descendAndPrintText(); +// +// Assert.assertEquals(exptected, actual); +// +// } +//} \ No newline at end of file