From be1486e81ba7ff3fc2d2ff2c730431d5378a0847 Mon Sep 17 00:00:00 2001 From: April Rivera Date: Mon, 12 Mar 2018 23:54:45 -0400 Subject: [PATCH 1/4] Part 1 and 2 --- src/main/java/io/zipcoder/ParenChecker.java | 46 +++++++++++++++++ .../java/io/zipcoder/ParenCheckerTest.java | 50 +++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index caee675..1f97ef7 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,50 @@ package io.zipcoder; +import java.util.Stack; + public class ParenChecker { + Stack stack = new Stack(); + + public ParenChecker() { + } + + public boolean parenChecker(String s){ + for(int i = 0; i < s.length(); i++){ + char c = s.charAt(i); + if(c == '(' ){ + stack.push(c); + }else if(c == ')'){ + if(stack.isEmpty() || stack.pop() != '('){ + return false; + } + } + } + return stack.isEmpty(); + } + public boolean pairChecker(String s){ + for(int i = 0; i < s.length(); i++){ + char c = s.charAt(i); + if(c == '(' || c == '{' || c == '[' || c == '<' || c == ' ' || c == '"') { + stack.push(c); + }else if(c == ')') { + if (stack.isEmpty() || stack.pop() != '(') return false; + } + else if(c == '}'){ + if(stack.isEmpty() || stack.pop() != '{') return false; + } + else if(c == ']'){ + if(stack.isEmpty() || stack.pop() != '[') return false; + } + else if(c == '>'){ + if(stack.isEmpty() || stack.pop() != '<') return false; + } + else if(c == '\''){ + if(stack.isEmpty() || stack.pop() != '\'') return false; + } + else if(c == '\"'){ + if(stack.isEmpty() || stack.pop() != '\"') return false; + } + } + return stack.isEmpty(); + } } diff --git a/src/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index 76aa3b6..ee16e78 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -5,4 +5,54 @@ public class ParenCheckerTest { + @Test + public void parenCheckerTest(){ + //Given + ParenChecker parenChecker = new ParenChecker(); + String s = ")()("; + //When + boolean allPair = parenChecker.parenChecker(s); + //Then + Assert.assertFalse(allPair); + } + @Test + public void parenCheckerTest2(){ + //Given + ParenChecker parenChecker = new ParenChecker(); + String s = "()()"; + //When + boolean hasAllPair = parenChecker.parenChecker(s); + //Then + Assert.assertTrue(hasAllPair); + } + @Test + public void pairCheckerTest(){ + //Given + ParenChecker parenChecker = new ParenChecker(); + String s = "{true}"; + //When + boolean hasPair = parenChecker.pairChecker(s); + //Then + Assert.assertTrue(hasPair); + } + @Test + public void pairCheckTest2(){ + //Given + ParenChecker parenChecker = new ParenChecker(); + String s = " Date: Tue, 13 Mar 2018 14:35:42 -0400 Subject: [PATCH 2/4] still working --- pom.xml | 12 +++++++ src/main/java/io/zipcoder/WC.java | 37 +++++++++++++++++-- src/main/resources/someTextFile.txt | 51 +++++++++++++++++++++++++++ src/test/java/io/zipcoder/WCTest.java | 27 ++++++++++++++ 4 files changed, 125 insertions(+), 2 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/WC.java b/src/main/java/io/zipcoder/WC.java index babb68c..0f48b3b 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,12 +2,14 @@ import java.io.FileNotFoundException; import java.io.FileReader; -import java.util.Iterator; -import java.util.Scanner; +import java.util.*; +import java.util.stream.Collectors; public class WC { private Iterator si; + Map wordMap = new HashMap<>(); + public WC(String fileName) { try { this.si = new Scanner(new FileReader(fileName)); @@ -20,4 +22,35 @@ public WC(String fileName) { public WC(Iterator si) { this.si = si; } + private void splitFile(){ + ArrayList words = new ArrayList<>(); + String eachWord; + while(si.hasNext()){ + eachWord = si.next(); + words.add(eachWord); + } + } + private void wordCount(){ + while(si.hasNext()){ + String word = si.next().toLowerCase(); + Integer freq = wordMap.get(word); + if(freq == null){ + wordMap.put(word, 1); + }else wordMap.put(word, freq+1); + } + + } + private Map sortedMap(){ + Map + myMap = wordMap + .entrySet() + .stream() + .sorted(Collections.reverseOrder(Map.Entry.comparingByValue())) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new)); + + for(Map.Entry entry: myMap){ + System.out.println(entry); + } + return myMap; + } } diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/someTextFile.txt index e69de29..e560f17 100644 --- a/src/main/resources/someTextFile.txt +++ b/src/main/resources/someTextFile.txt @@ -0,0 +1,51 @@ +But this murder--was it to dog him all his life? Was he always to be +burdened by his past? Was he really to confess? Never. There was +only one bit of evidence left against him. The picture itself--that +was evidence. He would destroy it. Why had he kept it so long? Once +it had given him pleasure to watch it changing and growing old. Of +late he had felt no such pleasure. It had kept him awake at night. +When he had been away, he had been filled with terror lest other eyes +should look upon it. It had brought melancholy across his passions. +Its mere memory had marred many moments of joy. It had been like +conscience to him. Yes, it had been conscience. He would destroy it. + +He looked round and saw the knife that had stabbed Basil Hallward. He +had cleaned it many times, till there was no stain left upon it. It +was bright, and glistened. As it had killed the painter, so it would +kill the painter's work, and all that that meant. It would kill the +past, and when that was dead, he would be free. It would kill this +monstrous soul-life, and without its hideous warnings, he would be at +peace. He seized the thing, and stabbed the picture with it. + +There was a cry heard, and a crash. The cry was so horrible in its +agony that the frightened servants woke and crept out of their rooms. +Two gentlemen, who were passing in the square below, stopped and looked +up at the great house. They walked on till they met a policeman and +brought him back. The man rang the bell several times, but there was +no answer. Except for a light in one of the top windows, the house was +all dark. After a time, he went away and stood in an adjoining portico +and watched. + +"Whose house is that, Constable?" asked the elder of the two gentlemen. + +"Mr. Dorian Gray's, sir," answered the policeman. + +They looked at each other, as they walked away, and sneered. One of +them was Sir Henry Ashton's uncle. + +Inside, in the servants' part of the house, the half-clad domestics +were talking in low whispers to each other. Old Mrs. Leaf was crying +and wringing her hands. Francis was as pale as death. + +After about a quarter of an hour, he got the coachman and one of the +footmen and crept upstairs. They knocked, but there was no reply. +They called out. Everything was still. Finally, after vainly trying +to force the door, they got on the roof and dropped down on to the +balcony. The windows yielded easily--their bolts were old. + +When they entered, they found hanging upon the wall a splendid portrait +of their master as they had last seen him, in all the wonder of his +exquisite youth and beauty. Lying on the floor was a dead man, in +evening dress, with a knife in his heart. He was withered, wrinkled, +and loathsome of visage. It was not till they had examined the rings +that they recognized who it was. \ 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..e7abdb2 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -8,4 +8,31 @@ public class WCTest { + @Test + public void splitFileTest(){ + //Given + + //When + + //Then + + } + @Test + public void wordCountTest(){ + //Given + + //When + WC.class.getResource("/someFileText").getFile(); + //Then + + } + @Test + public void sortedMapTest(){ + //Given + + //When + + //Then + + } } \ No newline at end of file From fdef555d11e4b694db5ccff52313edcda61906f3 Mon Sep 17 00:00:00 2001 From: April Rivera Date: Tue, 13 Mar 2018 19:25:37 -0400 Subject: [PATCH 3/4] DONE YAAAAY --- src/main/java/io/zipcoder/ParenChecker.java | 2 +- src/main/java/io/zipcoder/WC.java | 45 ++++++++----- src/main/resources/SomeOtherTextFile.txt | 1 + src/test/java/io/zipcoder/WCTest.java | 74 +++++++++++++++++++-- 4 files changed, 97 insertions(+), 25 deletions(-) create mode 100644 src/main/resources/SomeOtherTextFile.txt diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index 1f97ef7..19f0b55 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -24,7 +24,7 @@ public boolean parenChecker(String s){ public boolean pairChecker(String s){ for(int i = 0; i < s.length(); i++){ char c = s.charAt(i); - if(c == '(' || c == '{' || c == '[' || c == '<' || c == ' ' || c == '"') { + if(c == '(' || c == '{' || c == '[' || c == '<' || c == '\'' || c == '\"') { stack.push(c); }else if(c == ')') { if (stack.isEmpty() || stack.pop() != '(') return false; diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index 0f48b3b..6a724d1 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -9,10 +9,11 @@ public class WC { private Iterator si; Map wordMap = new HashMap<>(); + ArrayList words = new ArrayList<>(); 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); @@ -22,35 +23,43 @@ public WC(String fileName) { public WC(Iterator si) { this.si = si; } - private void splitFile(){ - ArrayList words = new ArrayList<>(); + + public void splitFile() { String eachWord; - while(si.hasNext()){ + while (si.hasNext()) { eachWord = si.next(); - words.add(eachWord); + words.add(eachWord.toLowerCase()); } } - private void wordCount(){ - while(si.hasNext()){ - String word = si.next().toLowerCase(); - Integer freq = wordMap.get(word); - if(freq == null){ - wordMap.put(word, 1); - }else wordMap.put(word, freq+1); - } + public Map wordCount() { + for (String s : words) { + Integer freq = wordMap.get(s); + wordMap.put(s, (freq == null) ? 1 : freq + 1); +// if(freq == null){ +// wordMap.put(s, 1); +// }else wordMap.put(s, freq+1); + } + return wordMap; } - private Map sortedMap(){ + + public Map sortedMap() { Map - myMap = wordMap + myMap = wordMap .entrySet() .stream() .sorted(Collections.reverseOrder(Map.Entry.comparingByValue())) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new)); - for(Map.Entry entry: myMap){ - System.out.println(entry); - } return myMap; } + + + public String getSi() { + while (si.hasNext()) { + String currentString = si.next(); + return currentString; + } + return null; + } } diff --git a/src/main/resources/SomeOtherTextFile.txt b/src/main/resources/SomeOtherTextFile.txt new file mode 100644 index 0000000..6ef79f1 --- /dev/null +++ b/src/main/resources/SomeOtherTextFile.txt @@ -0,0 +1 @@ +Hiya! This this is is a test. \ 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 e7abdb2..4280fc7 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -1,38 +1,100 @@ package io.zipcoder; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; import java.util.ArrayList; -import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; public class WCTest { + private WC wc; + + @Before + public void setup(){ + wc = new WC(WC.class.getResource("/someOtherTextFile.txt").getFile()); + wc.getSi(); + } + @Test public void splitFileTest(){ //Given - + wc = new WC(WC.class.getResource("/someOtherTextFile.txt").getFile()); + ArrayList expected = new ArrayList<>(); + expected.add("hiya"); + expected.add("this"); + expected.add("this"); + expected.add("is"); + expected.add("is"); + expected.add("a"); + expected.add("test"); //When - + wc.splitFile(); //Then + Assert.assertEquals(expected, wc.words); } @Test public void wordCountTest(){ //Given + wc = new WC(WC.class.getResource("/someOtherTextFile.txt").getFile()); + ArrayList expected1 = new ArrayList<>(); + expected1.add("hiya"); + expected1.add("this"); + expected1.add("this"); + expected1.add("is"); + expected1.add("is"); + expected1.add("a"); + expected1.add("test"); + Map expected = new HashMap<>(); + expected.put("hiya", 1); + expected.put("this", 2); + expected.put("is", 2); + expected.put("a", 1); + expected.put("test", 1); //When - WC.class.getResource("/someFileText").getFile(); + wc.splitFile(); + wc.wordCount(); //Then + Assert.assertEquals(expected, wc.wordMap); } @Test public void sortedMapTest(){ //Given + wc = new WC(WC.class.getResource("/someOtherTextFile.txt").getFile()); + ArrayList expected1 = new ArrayList<>(); + expected1.add("hiya"); + expected1.add("this"); + expected1.add("this"); + expected1.add("is"); + expected1.add("is"); + expected1.add("a"); + expected1.add("test"); - //When + Map expected2 = new HashMap<>(); + expected2.put("hiya", 1); + expected2.put("this", 2); + expected2.put("is", 2); + expected2.put("a", 1); + expected2.put("test", 1); - //Then + Map expected = new LinkedHashMap<>(); + expected.put("hiya", 1); + expected.put("this", 2); + expected.put("is", 2); + expected.put("a", 1); + expected.put("test", 1); + //When + wc.splitFile(); + wc.wordCount(); + wc.sortedMap(); + //Then + Assert.assertEquals(expected, wc.sortedMap()); } } \ No newline at end of file From fff1508001160b1aab9188f53bd171c21891f0e0 Mon Sep 17 00:00:00 2001 From: April Rivera Date: Tue, 13 Mar 2018 19:27:52 -0400 Subject: [PATCH 4/4] delete commented code --- src/main/java/io/zipcoder/WC.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index 6a724d1..e478e7d 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -36,9 +36,6 @@ public Map wordCount() { for (String s : words) { Integer freq = wordMap.get(s); wordMap.put(s, (freq == null) ? 1 : freq + 1); -// if(freq == null){ -// wordMap.put(s, 1); -// }else wordMap.put(s, freq+1); } return wordMap; }