From 1b1252a2270e68f1df1ab5fbe97b3725a7253011 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Tue, 13 Mar 2018 20:36:19 -0400 Subject: [PATCH 1/5] part 1 --- src/main/java/io/zipcoder/PairedChecker.java | 32 ++++++++++++++++ src/main/java/io/zipcoder/ParenChecker.java | 20 +++++++++- .../java/io/zipcoder/ParenCheckerTest.java | 38 ++++++++++++++++++- 3 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 src/main/java/io/zipcoder/PairedChecker.java diff --git a/src/main/java/io/zipcoder/PairedChecker.java b/src/main/java/io/zipcoder/PairedChecker.java new file mode 100644 index 0000000..57d4794 --- /dev/null +++ b/src/main/java/io/zipcoder/PairedChecker.java @@ -0,0 +1,32 @@ +package io.zipcoder; +import java.util.EmptyStackException; +import java.util.Stack; + +public class PairedChecker { + + /** + * Check to see if opening and closing characters are balanced in String input + * @param opening token + * @param closing token + * @param input string to scan + * @return true if all opening tokens match all closing tokens + */ + public boolean check(Character opening, Character closing, String input) { + Stack stack = new Stack(); + for (int i = 0; i < input.length(); i++) { + Character value = input.charAt(i); + if (value.equals(opening)) { + stack.push(value); + } else if (value.equals(closing)) { + try { + stack.pop(); + } + catch (EmptyStackException ex) { + return false; + } + } + } + + return stack.isEmpty(); + } +} \ No newline at end of file diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index caee675..2c72a2c 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,22 @@ package io.zipcoder; +import java.util.Stack; + public class ParenChecker { -} + + private PairedChecker checker; + + public ParenChecker() { + this.checker = new PairedChecker(); + } + + + /** + * Check to see if open and closing paren's are balanced + * @param inputString string that is iterated over + * @return true if all parenthesis in string are matched + */ + public boolean check(String inputString) { + return this.checker.check('(', ')', inputString); + } + } \ 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..824174b 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -2,7 +2,41 @@ import org.junit.Assert; import org.junit.Test; - +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; public class ParenCheckerTest { + private ParenChecker checker; + + @Before + public void setup() { + this.checker = new ParenChecker(); + } + + @Test //happyPath + public void checkBaseCase() { + boolean result = this.checker.check("()"); + Assert.assertTrue(result); + } + + @Test //left char left paren, final check checks to see if stack is empty + public void checkFailingBaseCase() { + boolean result = this.checker.check("()("); + Assert.assertFalse(result); + } + + @Test //tried to pop a second time on an emptyStack + public void checkExceptionCaughtAndThrowsFalseBaseCase() { + boolean result = this.checker.check("())"); + Assert.assertFalse(result); + } + + @Test //ignore other char checks + public void checkOtherCharsIgnoredBaseCase() { + boolean result = this.checker.check("(jklm86&>>)"); + Assert.assertTrue(result); + } +} + + -} \ No newline at end of file From d202210e2f36127eb2d49d35861813d5442150f9 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Tue, 13 Mar 2018 21:10:34 -0400 Subject: [PATCH 2/5] updated parenchecker to accept additional tokens/added method to resolve quotes --- src/main/java/io/zipcoder/PairedChecker.java | 17 ++++++++++++++ src/main/java/io/zipcoder/ParenChecker.java | 22 +++++++++++++------ .../java/io/zipcoder/ParenCheckerTest.java | 15 +++++++++++-- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/main/java/io/zipcoder/PairedChecker.java b/src/main/java/io/zipcoder/PairedChecker.java index 57d4794..6e6360e 100644 --- a/src/main/java/io/zipcoder/PairedChecker.java +++ b/src/main/java/io/zipcoder/PairedChecker.java @@ -29,4 +29,21 @@ public boolean check(Character opening, Character closing, String input) { return stack.isEmpty(); } + + /** + * Specific for opening and closing chars that are the same char, ie "" and '' + * @param quote token used for both opening and closing + * @param input string to scan + * @return true if the tokens are evenly distributed in the string + */ + public boolean checkSame(Character quote, String input) { + int count = 0; + for(int i = 0; i < input.length(); i++){ + if( quote.equals(input.charAt(i))){ + count++; + } + } + return count%2 == 0; + } + } \ No newline at end of file diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index 2c72a2c..8111ff9 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -4,19 +4,27 @@ public class ParenChecker { - private PairedChecker checker; + private PairedChecker checker; - public ParenChecker() { - this.checker = new PairedChecker(); - } + public ParenChecker() { + this.checker = new PairedChecker(); + } /** * Check to see if open and closing paren's are balanced + * * @param inputString string that is iterated over * @return true if all parenthesis in string are matched */ public boolean check(String inputString) { - return this.checker.check('(', ')', inputString); - } - } \ No newline at end of file + PairedChecker checker = new PairedChecker(); + return checker.check('(', ')', inputString) && + checker.check('{', '}', inputString) && + checker.check('[', ']', inputString) && + checker.check('<', '>', inputString) && + checker.checkSame('"', inputString) && + checker.checkSame('\'', inputString); + } + +} \ 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 824174b..aabc023 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -6,6 +6,7 @@ import org.junit.Before; import org.junit.Test; public class ParenCheckerTest { + private ParenChecker checker; @Before @@ -19,6 +20,17 @@ public void checkBaseCase() { Assert.assertTrue(result); } + @Test + public void checkBraceBaseCase() { + boolean result = this.checker.check("[[<'cats'>''''][{}][()]]"); + Assert.assertTrue(result); + } + + @Test + public void checkExtraBraceBaseCase() { + boolean result = this.checker.check("[[>][{}][()]]"); + Assert.assertFalse(result); + } @Test //left char left paren, final check checks to see if stack is empty public void checkFailingBaseCase() { boolean result = this.checker.check("()("); @@ -33,10 +45,9 @@ public void checkExceptionCaughtAndThrowsFalseBaseCase() { @Test //ignore other char checks public void checkOtherCharsIgnoredBaseCase() { - boolean result = this.checker.check("(jklm86&>>)"); + boolean result = this.checker.check("(jklm86&)"); Assert.assertTrue(result); } } - From dce89a58488453e80084d38f7f88e4b26805a5dd Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Wed, 14 Mar 2018 08:52:02 -0400 Subject: [PATCH 3/5] completed word counter,works but need more testing --- pom.xml | 12 +++++ src/main/java/io/zipcoder/PairedChecker.java | 2 +- src/main/java/io/zipcoder/WC.java | 47 +++++++++++++++++-- .../java/io/zipcoder/ParenCheckerTest.java | 18 +++++++ 4 files changed, 73 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index e66b725..efb68c4 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.8 + 1.8 + + + + diff --git a/src/main/java/io/zipcoder/PairedChecker.java b/src/main/java/io/zipcoder/PairedChecker.java index 6e6360e..84263b5 100644 --- a/src/main/java/io/zipcoder/PairedChecker.java +++ b/src/main/java/io/zipcoder/PairedChecker.java @@ -10,6 +10,7 @@ public class PairedChecker { * @param closing token * @param input string to scan * @return true if all opening tokens match all closing tokens + * Catch exception and return false if try to pop off an empty stack */ public boolean check(Character opening, Character closing, String input) { Stack stack = new Stack(); @@ -45,5 +46,4 @@ public boolean checkSame(Character quote, String input) { } return count%2 == 0; } - } \ 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 babb68c..16e7eee 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -4,20 +4,57 @@ import java.io.FileReader; import java.util.Iterator; import java.util.Scanner; - +import java.util.Map; +import java.util.TreeMap; public class WC { - private Iterator si; + private Iterator stringIterator; + private TreeMap map; public WC(String fileName) { try { - this.si = new Scanner(new FileReader(fileName)); + this.stringIterator = new Scanner(new FileReader(fileName)); + this.map = new TreeMap(); + //parsing file in constructor, already has scanner for the file, might as well parse as + // soon as recieve, not sure if best practice + this.parse(); } catch (FileNotFoundException e) { System.out.println(fileName + " Does Not Exist"); System.exit(-1); } } - public WC(Iterator si) { - this.si = si; + //never used +// public WC(Iterator stringIterator) { +// this.stringIterator = stringIterator; +// this.map = new TreeMap(); +// this.parse(); +// } + + private void parse() { + while (this.stringIterator.hasNext()) { + String s = this.stringIterator.next().toLowerCase().replaceAll("[^\\w]", ""); + Integer count = this.map.getOrDefault(s, 0); + this.map.put(s, count+1); + } + } + + public void print() { + this.map.entrySet() + .stream() + .sorted(Map.Entry.comparingByValue().reversed()) + .forEach(System.out::println); + } + + public static void main(String[] args) { + WC wc = new WC("/Users/kaitrinahigh/Downloads/47366-0.txt"); + wc.print(); } } + + + + + + + + diff --git a/src/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index aabc023..8597916 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -26,6 +26,24 @@ public void checkBraceBaseCase() { Assert.assertTrue(result); } + @Test //happy path even quote + public void checkDoubleQuoteBaseCase() { + boolean result = this.checker.check("\"\"\"\"\"\""); + Assert.assertTrue(result); + } + + @Test //happy path even single quote + public void checkSingleQuoteBaseCase() { + boolean result = this.checker.check("\'\'\'\'\'\'"); + Assert.assertTrue(result); + } + + @Test //happy path even mixed quote + public void checkMixedQuoteBaseCase() { + boolean result = this.checker.check("\"\'\'\'\'\'\'\"\"\"\"\""); + Assert.assertTrue(result); + } + @Test public void checkExtraBraceBaseCase() { boolean result = this.checker.check("[[>][{}][()]]"); From df68d40aa4e516742e790d4f6fdad2d94fe0372b Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Wed, 14 Mar 2018 20:07:47 -0400 Subject: [PATCH 4/5] refactored print method to return a string instead of printing to console --- src/main/java/io/zipcoder/WC.java | 35 ++++++++++++++------------- src/main/resources/someTextFile.txt | 7 ++++++ src/test/java/io/zipcoder/WCTest.java | 6 ++++- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index 16e7eee..1a36ca4 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,10 +2,8 @@ import java.io.FileNotFoundException; import java.io.FileReader; -import java.util.Iterator; -import java.util.Scanner; -import java.util.Map; -import java.util.TreeMap; +import java.util.*; + public class WC { private Iterator stringIterator; private TreeMap map; @@ -14,23 +12,14 @@ public WC(String fileName) { try { this.stringIterator = new Scanner(new FileReader(fileName)); this.map = new TreeMap(); - //parsing file in constructor, already has scanner for the file, might as well parse as - // soon as recieve, not sure if best practice - this.parse(); + this.parseAndCountWords(); } catch (FileNotFoundException e) { System.out.println(fileName + " Does Not Exist"); System.exit(-1); } } - //never used -// public WC(Iterator stringIterator) { -// this.stringIterator = stringIterator; -// this.map = new TreeMap(); -// this.parse(); -// } - - private void parse() { + private void parseAndCountWords() { while (this.stringIterator.hasNext()) { String s = this.stringIterator.next().toLowerCase().replaceAll("[^\\w]", ""); Integer count = this.map.getOrDefault(s, 0); @@ -38,11 +27,23 @@ private void parse() { } } - public void print() { + + public String toString() { + ArrayList> sortedValues = new ArrayList<>(); this.map.entrySet() .stream() .sorted(Map.Entry.comparingByValue().reversed()) - .forEach(System.out::println); + .forEach(sortedValues::add); + StringBuilder sb = new StringBuilder(); + for (Map.Entry entry : sortedValues) { + sb.append(entry.getKey()).append(" = ").append(entry.getValue()).append("\n"); + } + + return sb.toString(); + } + + public void print() { + System.out.println(this.toString()); } public static void main(String[] args) { diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/someTextFile.txt index e69de29..0df0ad1 100644 --- a/src/main/resources/someTextFile.txt +++ b/src/main/resources/someTextFile.txt @@ -0,0 +1,7 @@ +Strive for progress, not perfection. +The expert in everything was once a beginner. +Start where you are. Use what you have. Do what you can. + + + + diff --git a/src/test/java/io/zipcoder/WCTest.java b/src/test/java/io/zipcoder/WCTest.java index 895e831..99a0d18 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -8,4 +8,8 @@ public class WCTest { -} \ No newline at end of file +//public void wordCount +//} +} + + From 84e0cf1ed3139fd797b5b56051ccc11628b20618 Mon Sep 17 00:00:00 2001 From: Kaitrina High Date: Wed, 14 Mar 2018 20:42:59 -0400 Subject: [PATCH 5/5] finished, outputs word count in string format with tests --- src/main/java/io/zipcoder/WC.java | 6 +++-- src/test/java/io/zipcoder/WCTest.java | 32 ++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index 1a36ca4..618514a 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -47,8 +47,10 @@ public void print() { } public static void main(String[] args) { - WC wc = new WC("/Users/kaitrinahigh/Downloads/47366-0.txt"); - wc.print(); + //WC wc = new WC("/Users/kaitrinahigh/Downloads/47366-0.txt"); + WC testFile = new WC("/Users/kaitrinahigh/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/someTextFile.txt"); + testFile.print(); + } } diff --git a/src/test/java/io/zipcoder/WCTest.java b/src/test/java/io/zipcoder/WCTest.java index 99a0d18..a1f02a7 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -1,6 +1,7 @@ package io.zipcoder; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import java.util.ArrayList; @@ -8,8 +9,33 @@ public class WCTest { -//public void wordCount -//} + @Test + public void testWordCountBaseCase() { + WC testFile = new WC("/Users/kaitrinahigh/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/someTextFile.txt"); + String actual = testFile.toString(); + String expected = "you = 3\n" + + "what = 2\n" + + "a = 1\n" + + "are = 1\n" + + "beginner = 1\n" + + "can = 1\n" + + "do = 1\n" + + "everything = 1\n" + + "expert = 1\n" + + "for = 1\n" + + "have = 1\n" + + "in = 1\n" + + "not = 1\n" + + "once = 1\n" + + "perfection = 1\n" + + "progress = 1\n" + + "start = 1\n" + + "strive = 1\n" + + "the = 1\n" + + "use = 1\n" + + "was = 1\n" + + "where = 1\n"; + Assert.assertEquals(expected, actual); + } } -