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..0f935dc 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,55 @@ package io.zipcoder; + +import java.util.Stack; + +//1.Create a class with a method that verifies all parens () are paired. HINT: Use a stack. public class ParenChecker { + + Stack stack = new Stack(); + + public ParenChecker() { + + } + + public boolean parenChecker(String value) { + + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + if (c == '(') { + stack.push(c); + } else if (c == ')') { + if (stack.isEmpty()) { + return false; + } + stack.pop(); + } + + } + return stack.isEmpty(); + } + + + public boolean pairParen(String value) { + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + if (c == '(' || c == '[' || c == '<' || c == '{' || c == '\'' || c == '\"') { + stack.push(c); + } else if (c == ')' || c == ']' || c == '>' || c == '}' || c == '\'' || c == '\"') { + if (stack.isEmpty()) { + return false; + } + char lastValue = stack.peek(); + if (c == ')' && lastValue == '(' || c == '>' && lastValue == '<' || c == '"' && lastValue == '"' || c == '\'' && lastValue == '\'') { + stack.pop(); + } + } + } + return stack.isEmpty(); + } } + +//PseudoCode +//the lastValue equals the stack.peek(); the peek: Looks at the object at the top of this stack without removing it +//from the stack. + diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index babb68c..4ab81b9 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,11 +2,11 @@ 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 TreeMap map = new TreeMap(); public WC(String fileName) { try { @@ -18,6 +18,46 @@ public WC(String fileName) { } public WC(Iterator si) { + this.si = si; } + + public Map count(){ + while(si.hasNext()){ + String[] characters = si.next().replaceAll("\\W", "").toLowerCase().split(" "); + for(int i = 0; i map, String index){ + Integer counter = map.get(index); + if(counter==null){ + counter = 0; + } + map.put(index, counter + 1); + } + + public String displayMap(){ + Collections.sort(Map,(o1, o2) -> o2.getValue().compareTo(o1.getValue())); + + + StringBuilder sb = new StringBuilder(); + sb.append("Count in descending order: \n"); + for(String index : map.keySet()){ + sb.append("\t"+index + ": " + map.get(index) +"\n"); + } + return sb.toString() + + } } +//PseudoCode +//create a counter +//loop through si +//and make all words characters except symbols and numbers and all lowercase + +//Actual Code Explanation +//Create a treemap up top, it holds the keys (index) and the value(word) +// \ No newline at end of file diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/someTextFile.txt index e69de29..2c2dc33 100644 --- a/src/main/resources/someTextFile.txt +++ b/src/main/resources/someTextFile.txt @@ -0,0 +1,14 @@ +TWO TRAMPS + + +A man and a boy were ascending a steep street in a country town in +eastern New York. The man was tall and dark-complexioned, with a +sinister look which of itself excited distrust. He wore a slouch hat, +which, coming down over his forehead, nearly concealed from view his +low, receding brow. A pair of black, piercing eyes looked out from +beneath the brim. The first impression produced upon those who met him +was that he was of gypsy blood, and the impression was a correct one. +Where he was born no one seemed to know; perhaps he did not himself +know, for all his life he had been a wanderer, but English was the +tongue which he spoke, and, apart from the gypsy dialect, he knew no +other. \ No newline at end of file diff --git a/src/main/resources/testFile.txt b/src/main/resources/testFile.txt new file mode 100644 index 0000000..00ec727 --- /dev/null +++ b/src/main/resources/testFile.txt @@ -0,0 +1 @@ +yolo yolo rolly polo \ 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..1e10cf4 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -3,6 +3,47 @@ import org.junit.Assert; import org.junit.Test; +import static org.junit.Assert.*; + public class ParenCheckerTest { + @Test + public void testParenChecker(){ + //Given + ParenChecker parenChecker = new ParenChecker(); + String value = "I rock (rough) and stuff with my (afro) puffs!"; + //When + boolean pairParen = parenChecker.parenChecker(value); + Assert.assertTrue(pairParen); + } + + @Test + public void testParenChecker2(){ + //Given + ParenChecker parenChecker = new ParenChecker(); + String value = "{(/)}<"; + //When + boolean pairParen = parenChecker.parenChecker(value); + Assert.assertTrue(pairParen); + } + + @Test + public void testPairParen(){ + //Given + ParenChecker parenTest = new ParenChecker(); + String value = "I love so much"; + //When + Assert.assertTrue(parenTest.pairParen(value)); + } + + @Test + public void testPairParen2(){ + //Given + ParenChecker parenTest = new ParenChecker(); + String value = " pow (pow)"; + //When + Assert.assertTrue(parenTest.pairParen(value)); + } + + } \ 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..cc1a051 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -7,5 +7,121 @@ import java.util.Arrays; public class WCTest { + @Test + public void count(){ + WC wc = new WC("/Users/katricewilliams-dredden/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/someTextFile.txt"); + wc.count(); + String expected = "Count in descending order: \n" + + + "\ta: 9\n"+ + "\tall: 1\n" + + "\tand: 3\n" + + "\tand,: 1\n" + + "\tapart: 1\n" + + "\tascending: 1\n" + + "\tbeen: 1\n" + + "\tbeneath: 1\n" + + "\tblack,: 1\n" + + "\tblood,: 1\n" + + "\tborn: 1\n" + + "\tboy: 1\n" + + "\tbrim.: 1\n" + + "\tbrow.: 1\n" + + "\tbut: 1\n" + + "\tcoming: 1\n" + + "\tconcealed: 1\n" + + "\tcorrect: 1\n" + + "\tcountry: 1\n" + + "\tdark-complexioned,: 1\n" + + "\tdialect,: 1\n" + + "\tdid: 1\n" + + "\tdistruct.: 1\n" + + "\tdown: 1\n" + + "\teastern: 1\n" + + "\tenglish: 1\n" + + "\texcited: 1\n" + + "\teyes: 1\n" + + "\tfirst: 1\n" + + "\tfor: 1\n" + + "\tforehead: 1\n" + + "\tfrom: 3\n" + + "\tgypsy: 2\n" + + "\thad: 1\n" + + "\that,: 1\n" + + "\the: 7\n" + + "\thim: 1\n" + + "\thimself: 1\n" + + "\this: 3\n" + + "\timpression: 2\n" + + "\tin: 2\n" + + "\titself: 1\n" + + "\tknew: 1\n" + + "\tknow,: 1\n" + + "\tknow;: 1\n" + + "\tlife: 1\n" + + "\tlook: 1\n" + + "\tlooked: 1\n" + + "\tlow,: 1\n" + + "\tman: 2\n" + + "\tmet: 1\n" + + "\tnearly: 1\n" + + "\tnew: 1\n" + + "\tno: 2\n" + + "\tnot: 1\n" + + "\tof: 3\n" + + "\tone: 1\n" + + "\tother.: 1\n" + + "\tout: 1\n" + + "\tover: 1\n" + + "\tpair: 1\n" + + "\tperhaps: 1\n" + + "\tpiercing: 1\n" + + "\tproduced: 1\n" + + "\treceding: 1\n" + + "\tseemed: 1\n" + + "\tsinister: 1\n" + + "\tslouch: 1\n" + + "\tspoke,: 1\n" + + "\tsteep: 1\n" + + "\tstreet: 1\n" + + "\ttall: 1\n" + + "\tthat: 1\n" + + "\tthe: 6\n" + + "\tthose: 1\n" + + "\tto: 1\n" + + "\ttongue: 1\n" + + "\ttown: 1\n" + + "\ttramps: 1\n" + + "\ttwo: 1\n" + + "\tupon: 1\n" + + "\tview: 1\n" + + "\twanderer,: 1\n" + + "\twas: 6\n" + + "\twere: 1\n" + + "\twhere: 1\n" + + "\twhich: 2\n" + + "\twhich,: 1\n" + + "\twho: 1\n" + + "\twith: 1\n" + + "\twore: 1\n" + + "\tyork: 1\n"; + + + String actual = wc.displayMap(); + Assert.assertEquals(expected, actual); + } + + @Test + public void displayMap(){ + WC wc = new WC("/Users/katricewilliams-dredden/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/testFile.txt"); + wc.count(); + String expected = "Count in descending order: \n" + + "\tpolo: 1\n" + + "\trolly: 1\n" + + "\tyolo: 2\n"; + String actual = wc.displayMap(); + Assert.assertEquals(expected, actual); + + } } \ No newline at end of file