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/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index caee675..19f0b55 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/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index babb68c..e478e7d 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,15 +2,18 @@ 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<>(); + 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); @@ -20,4 +23,40 @@ public WC(String fileName) { public WC(Iterator si) { this.si = si; } + + public void splitFile() { + String eachWord; + while (si.hasNext()) { + eachWord = si.next(); + words.add(eachWord.toLowerCase()); + } + } + + public Map wordCount() { + for (String s : words) { + Integer freq = wordMap.get(s); + wordMap.put(s, (freq == null) ? 1 : freq + 1); + } + return wordMap; + } + + public 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)); + + 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/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/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 = " 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.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"); + + Map expected2 = new HashMap<>(); + expected2.put("hiya", 1); + expected2.put("this", 2); + expected2.put("is", 2); + expected2.put("a", 1); + expected2.put("test", 1); + + 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