From 8c09cda5b8a658494d81354f6e199f724d740ec3 Mon Sep 17 00:00:00 2001 From: mbowe4 Date: Mon, 12 Mar 2018 22:10:41 -0400 Subject: [PATCH 1/2] parts 1 & 2 complete --- src/main/java/io/zipcoder/ParenChecker.java | 52 +++++++++++++++++ .../java/io/zipcoder/ParenCheckerTest.java | 57 +++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/src/main/java/io/zipcoder/ParenChecker.java b/src/main/java/io/zipcoder/ParenChecker.java index caee675..64cc508 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,56 @@ package io.zipcoder; +import java.util.*; + public class ParenChecker { + + /** + * Create a class with a method that verifies all parens () are paired. + * HINT: Use a stack. + **/ + public boolean isPaired(String input) { + return hasPair(input, '(', ')'); + } + + + /** + * Create a method that checks that all opening characters have a closing one. + * Characters include () {} [] <> "" '' + **/ + public boolean hasClosing(String input) { + return(hasPair(input, '(', ')') + && hasPair(input, '{', '}') + && hasPair(input, '[', ']') + && hasPair(input, '<', '>') + && hasPair(input, '\"', '\"') + && hasPair(input, '\'', '\'')); + } + + + public boolean hasPair(String input, Character openingChar, Character closingChar) { + Stack stack = new Stack(); + + for(int i = 0; i < input.length(); i++) { + char currentChar = input.charAt(i); + + // if currentChar is opening, add to stack + if (currentChar == openingChar) { + stack.push(currentChar); + continue; + } + + // if currentChar is closing but there's no opening in stack, return false + if (currentChar == closingChar) { + if(stack.isEmpty()) { + return false; + } else if(stack.peek() == openingChar) { + stack.pop(); + } else { + return false; + } + } + } + // if stack is empty then all parentheses were paired + return stack.empty() || (stack.size() % 2 == 0); + } } diff --git a/src/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index 76aa3b6..c8f1839 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -5,4 +5,61 @@ public class ParenCheckerTest { + @Test + public void isPairedTest1() { + ParenChecker parenChecker = new ParenChecker(); + String str = "()()()()"; + + Assert.assertTrue(parenChecker.isPaired(str)); + } + + @Test + public void isPairedTest2() { + ParenChecker parenChecker = new ParenChecker(); + String str = ")()()()()(("; + + Assert.assertFalse(parenChecker.isPaired(str)); + } + + @Test + public void isPairedTest3() { + ParenChecker parenChecker = new ParenChecker(); + String str = ")((()"; + + Assert.assertFalse(parenChecker.isPaired(str)); + } + + @Test + public void hasClosingTest1() { + ParenChecker parenChecker = new ParenChecker(); + String str = "<<>>(){}[]"; + + Assert.assertTrue(parenChecker.hasClosing(str)); + } + + @Test + public void hasClosingClosingTest2() { + ParenChecker parenChecker = new ParenChecker(); + String str = "\"\""; + + Assert.assertTrue(parenChecker.hasClosing(str)); + } + + @Test + public void hasClosingClosingTest3() { + ParenChecker parenChecker = new ParenChecker(); + String str = "\'\'\'"; + + Assert.assertFalse(parenChecker.hasClosing(str)); + } + + @Test + public void hasPairTest1() { + ParenChecker parenChecker = new ParenChecker(); + String str = "()()()()"; + + Assert.assertTrue(parenChecker.hasPair(str, '(', ')')); + } + + } \ No newline at end of file From 66724fc2a07a6bd57bf8edac6ac97c55c1fbca62 Mon Sep 17 00:00:00 2001 From: mbowe4 Date: Tue, 13 Mar 2018 21:16:09 -0400 Subject: [PATCH 2/2] lab complete --- .DS_Store | Bin 0 -> 6148 bytes pom.xml | 12 +++++ src/.DS_Store | Bin 0 -> 6148 bytes src/main/.DS_Store | Bin 0 -> 6148 bytes src/main/java/io/zipcoder/ParenChecker.java | 1 - src/main/java/io/zipcoder/WC.java | 47 ++++++++++++++++++-- src/main/resources/someTextFile.txt | 0 src/main/resources/test | 3 ++ src/test/java/io/zipcoder/WCTest.java | 43 +++++++++++++++++- 9 files changed, 99 insertions(+), 7 deletions(-) create mode 100644 .DS_Store create mode 100644 src/.DS_Store create mode 100644 src/main/.DS_Store delete mode 100644 src/main/resources/someTextFile.txt create mode 100644 src/main/resources/test diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9a874b5768f336915163bb88cd434575b859f936 GIT binary patch literal 6148 zcmeH~Jr2S!425ml0g0s}V-^m;4I%_5-~tF3k&vj^b9A16778<}(6eNJu~Vz<8=6`~ zboab&MFtUB!i}=AFfm2m$tVxGT*u4pe81nUlA49C} z?O@64YO)2RT{MRe%{!}2F))pG(Sih~)xkgosK7*lF7m<7{{#Hn{6A@7N(HFEpDCdI z{io.zipcoder collections 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..7b0d36729e2ee777a660f9e8c6709dd97bc2fb68 GIT binary patch literal 6148 zcmeH~F>V4u3`M`g7D#EfOgRk)$PGpaPQV2S&>%r5BKkQx-ySzvsH0W%E!l7ES!;KH zv9ktX>-*^w7y&HlPOLmk%$N_j;tOYdpMH*)!|itQBJHgMp3+B5_H$d10#ZNMM4T9irw zDe%t}uwk>?toc%Twm!X{*Y{cVb)%DUIm54?049DEf6&9YUwlE si; @@ -20,4 +18,45 @@ public WC(String fileName) { public WC(Iterator si) { this.si = si; } + + + public Map getWordCount() { + Map wordOccurences = new HashMap<>(); + while (si.hasNext()) { + String currentLine = si.next(); + String[] words = currentLine.split(" "); + + for (String word : words) { + word = word.toLowerCase(); + word = word.replaceAll("[^a-zA-Z']", ""); + + if (!word.trim().isEmpty()) { + if (wordOccurences.containsKey(word)) { + int count = wordOccurences.get(word); + wordOccurences.put(word, count + 1); + } else { + wordOccurences.put(word, 1); + } + } + } + } + return entriesSortedByValues(wordOccurences); + } + + + private static > Map entriesSortedByValues(Map map) { + // ArrayList is initialized with all entries of passed in map + List> sortedEntries = new ArrayList<>(map.entrySet()); + + // used lambda expression to sort values in descending order and return result + sortedEntries.sort((e1, e2) -> e2.getValue().compareTo(e1.getValue())); + + Map result = new LinkedHashMap<>(); + + for (Map.Entry entry : sortedEntries) { + result.put(entry.getKey(), entry.getValue()); + } + return result; + } } + diff --git a/src/main/resources/someTextFile.txt b/src/main/resources/someTextFile.txt deleted file mode 100644 index e69de29..0000000 diff --git a/src/main/resources/test b/src/main/resources/test new file mode 100644 index 0000000..bfb2bf4 --- /dev/null +++ b/src/main/resources/test @@ -0,0 +1,3 @@ +The quick brown fox jumped over the lazy dog. +The the the the. +the The. Quick Brown. \ 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..8bc77cd 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -3,9 +3,48 @@ import org.junit.Assert; import org.junit.Test; -import java.util.ArrayList; -import java.util.Arrays; +import javax.xml.bind.Element; +import java.util.*; public class WCTest { + @Test + public void wcTest() { + WC wc = new WC("/Users/madelinebowe/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/test"); + + String expected = "{the=8, quick=2, brown=2, over=1, lazy=1, jumped=1, dog=1, fox=1}"; + String actual = wc.getWordCount().toString(); + + Assert.assertEquals(expected, actual); + } + + @Test + public void wcTest2() { + WC wc = new WC("/Users/madelinebowe/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/test"); + Map test = wc.getWordCount(); + + Integer expected = 8; + Integer actual = test.get("the"); + + Assert.assertEquals(expected, actual); + } + + @Test + public void wcTest3() { + WC wc = new WC("/Users/madelinebowe/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/test"); + Map test = wc.getWordCount(); + + Integer expected = 2; + Integer actual = test.get("quick"); + + Assert.assertEquals(expected, actual); + } + + @Test + public void wcTest4() { + WC wc = new WC("/Users/madelinebowe/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/test"); + Map test = wc.getWordCount(); + + Assert.assertFalse(test.containsKey("The")); + } } \ No newline at end of file