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 new file mode 100644 index 0000000..84263b5 --- /dev/null +++ b/src/main/java/io/zipcoder/PairedChecker.java @@ -0,0 +1,49 @@ +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 + * 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(); + 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(); + } + + /** + * 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 caee675..8111ff9 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,30 @@ 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) { + 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/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index babb68c..618514a 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,22 +2,62 @@ import java.io.FileNotFoundException; import java.io.FileReader; -import java.util.Iterator; -import java.util.Scanner; +import java.util.*; 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(); + this.parseAndCountWords(); } catch (FileNotFoundException e) { System.out.println(fileName + " Does Not Exist"); System.exit(-1); } } - public WC(Iterator si) { - this.si = si; + private void parseAndCountWords() { + 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 String toString() { + ArrayList> sortedValues = new ArrayList<>(); + this.map.entrySet() + .stream() + .sorted(Map.Entry.comparingByValue().reversed()) + .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) { + //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/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/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index 76aa3b6..8597916 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -2,7 +2,70 @@ import org.junit.Assert; import org.junit.Test; - +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; public class ParenCheckerTest { -} \ No newline at end of file + 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 + public void checkBraceBaseCase() { + boolean result = this.checker.check("[[<'cats'>''''][{}][()]]"); + 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("[[>][{}][()]]"); + 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("()("); + 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); + } +} + + diff --git a/src/test/java/io/zipcoder/WCTest.java b/src/test/java/io/zipcoder/WCTest.java index 895e831..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,4 +9,33 @@ public class WCTest { -} \ No newline at end of file + @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); + } +} +