From 46ccfcb03765552261a6c4495e43c4b90fff436c Mon Sep 17 00:00:00 2001 From: Katrice Williams-Dredden Date: Tue, 13 Mar 2018 13:09:39 -0400 Subject: [PATCH 1/4] part2 --- pom.xml | 12 +++++ src/main/java/io/zipcoder/ParenChecker.java | 48 +++++++++++++++++++ .../java/io/zipcoder/ParenCheckerTest.java | 11 +++++ 3 files changed, 71 insertions(+) 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..5434b2f 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,52 @@ 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(); + } + +//TODO need to add " and ' to method maybe in a seperate if statment + public boolean isMatchChecker(String value){ + for(int i = 0; i' || c == '}'){ + + } + + + } + } + + } + + diff --git a/src/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index 76aa3b6..48a398e 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -3,6 +3,17 @@ 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.assertFalse(pairParen); + } } \ No newline at end of file From 17d9661efdbb7bb9062830464406ec637e5a526e Mon Sep 17 00:00:00 2001 From: Katrice Williams-Dredden Date: Tue, 13 Mar 2018 13:29:08 -0400 Subject: [PATCH 2/4] added another test --- src/main/java/io/zipcoder/ParenChecker.java | 9 +++++---- src/test/java/io/zipcoder/ParenCheckerTest.java | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index 5434b2f..5e71c67 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -36,14 +36,15 @@ public boolean isMatchChecker(String value){ //question about last two comparisons if(c == '(' || c == '[' || c== '<'|| c == '{'){ stack.push(c); - }else if(c==')'){ - return false; }else if(c==')' || c==']' || c== '>' || c == '}'){ - + if(stack.isEmpty()){ + return false; + } + stack.pop(); } - } + return stack.isEmpty(); } diff --git a/src/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index 48a398e..713d0a3 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -14,6 +14,19 @@ public void TestParenChecker(){ String value = "I rock (rough) and stuff with my (afro) puffs!"; //When boolean pairParen = parenChecker.parenChecker(value); - Assert.assertFalse(pairParen); + Assert.assertTrue(pairParen); } + + @Test + public void testIsMatchChecker(){ + //Given + ParenChecker parenChecker = new ParenChecker(); + String value = "{(/)}<"; + //When + boolean pairParen = parenChecker.parenChecker(value); + Assert.assertTrue(pairParen); + + } + + @ } \ No newline at end of file From 0e9f82953152545a1afe1e089a26de6af5a2628a Mon Sep 17 00:00:00 2001 From: Katrice Williams-Dredden Date: Tue, 13 Mar 2018 15:09:10 -0400 Subject: [PATCH 3/4] done pt 1 & 2 --- src/main/java/io/zipcoder/ParenChecker.java | 36 ++++++++++--------- .../java/io/zipcoder/ParenCheckerTest.java | 23 ++++++++++-- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index 5e71c67..0f935dc 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -8,18 +8,18 @@ public class ParenChecker { Stack stack = new Stack(); - public ParenChecker(){ + public ParenChecker() { } - public boolean parenChecker(String value){ + public boolean parenChecker(String value) { - for(int i = 0; i < value.length(); i++){ + for (int i = 0; i < value.length(); i++) { char c = value.charAt(i); - if(c == '('){ + if (c == '(') { stack.push(c); - } else if (c == ')'){ - if(stack.isEmpty()){ + } else if (c == ')') { + if (stack.isEmpty()) { return false; } stack.pop(); @@ -29,25 +29,27 @@ public boolean parenChecker(String value){ return stack.isEmpty(); } -//TODO need to add " and ' to method maybe in a seperate if statment - public boolean isMatchChecker(String value){ - for(int i = 0; i' || c == '}'){ - if(stack.isEmpty()){ + } else if (c == ')' || c == ']' || c == '>' || c == '}' || c == '\'' || c == '\"') { + if (stack.isEmpty()) { return false; } - stack.pop(); + 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/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index 713d0a3..1e10cf4 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -8,7 +8,7 @@ public class ParenCheckerTest { @Test - public void TestParenChecker(){ + public void testParenChecker(){ //Given ParenChecker parenChecker = new ParenChecker(); String value = "I rock (rough) and stuff with my (afro) puffs!"; @@ -18,15 +18,32 @@ public void TestParenChecker(){ } @Test - public void testIsMatchChecker(){ + 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 From 3c5b583dca79f94d38d873d6f0197c965af0a389 Mon Sep 17 00:00:00 2001 From: Katrice Williams-Dredden Date: Wed, 14 Mar 2018 17:57:16 -0400 Subject: [PATCH 4/4] forgot to descend, working on it --- src/main/java/io/zipcoder/WC.java | 44 +++++++++- src/main/resources/someTextFile.txt | 14 ++++ src/main/resources/testFile.txt | 1 + src/test/java/io/zipcoder/WCTest.java | 116 ++++++++++++++++++++++++++ 4 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/testFile.txt 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/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