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..261548c 100644 --- a/src/main/java/io/zipcoder/ParenChecker.java +++ b/src/main/java/io/zipcoder/ParenChecker.java @@ -1,4 +1,239 @@ package io.zipcoder; +import java.util.Arrays; +import java.util.EnumSet; +import java.util.Stack; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import java.util.regex.*; + public class ParenChecker { + + private String inputExpression; + private Stack stack; + private EnumSet parenStrings; + + + public ParenChecker() { + this.stack = new Stack(); + this.parenStrings = EnumSet.allOf(ParenStrings.class); + } + + public ParenChecker(String inputExpression) { + this.inputExpression = inputExpression; + this.stack = new Stack(); + this.parenStrings = EnumSet.allOf(ParenStrings.class); + } + + public Stack getStack() { + return stack; + } + + public boolean isInputLengthEven(String input) { + boolean isInputLengthEven = true; + if (input.length() % 2 != 0) { + isInputLengthEven = false; + } + return isInputLengthEven; + } + + public boolean isFirstCharCorrect(String input) { + boolean isFirstCharCorrect = true; + String incorrectFirstCharStringPattern = "^[)}\\]>]"; + Pattern incorrectFirstCharPattern = Pattern.compile(incorrectFirstCharStringPattern); + Matcher firstCharMatcher = incorrectFirstCharPattern.matcher(input); + + if (firstCharMatcher.find()) { + isFirstCharCorrect = false; + } + return isFirstCharCorrect; + } + + public boolean isLastCharCorrect(String input) { + boolean isLastCharCorrect = true; + String incorrectLastCharStringPattern = "[({\\[<]$"; + + Pattern incorrectLastCharPattern = Pattern.compile(incorrectLastCharStringPattern); + Matcher lastCharMatcher = incorrectLastCharPattern.matcher(input); + + if (lastCharMatcher.find()) { + isLastCharCorrect = false; + } + return isLastCharCorrect; + } + + public void addOnlyParenStringsToStack(String input) { + String[] inputStringArray = input.split(""); + + String correctStrings = "[(){}\\[\\]<>\"\']$"; + Pattern correctStringsPattern = Pattern.compile(correctStrings); +// System.out.println("String entering addOnlyParenStringsToStack: "); +// System.out.println(input); + + for (String string : inputStringArray) { + Matcher correctStringsMatcher = correctStringsPattern.matcher(string); + if (correctStringsMatcher.find()) { + stack.add(string); + } + } +// System.out.println("Stack leaving addOnlyParenStringsToStack: "); +// System.out.println(this.getStackElementsAsString()); + } + + public boolean areAllParensPaired(String input) { + boolean areAllParensPaired = true; + + this.addOnlyParenStringsToStack(input); + String cleanedInput = this.getStackElementsAsString(); + if (! (this.isFirstCharCorrect(cleanedInput) && this.isLastCharCorrect(cleanedInput)) ) { + areAllParensPaired = false; + return areAllParensPaired; + } +// System.out.println("Stack inside areAllParensPaired: "); +// System.out.println(this.getStackElementsAsString()); + + for (int i = 0; i < stack.size() - 1; i++) { + + // SQUIGGLY CHECK + if (stack.get(i).equals("{")) { + if (stack.get(i + 1).equals("}")) { + stack.remove(i); + stack.remove(i); + if (i == 0) i--; + else i -= 2; + continue; + } + if ( stack.get(i + 1).equals("]") || stack.get(i + 1).equals(")") + || stack.get(i + 1).equals(">") ) { + break; + } + } + + // BRACKET CHECK + if (stack.get(i).equals("[")) { + if (stack.get(i + 1).equals("]")) { + stack.remove(i); + stack.remove(i); + if (i == 0) i--; + else i -= 2; + continue; + } + if ( stack.get(i + 1).equals("}") || stack.get(i + 1).equals(")") + || stack.get(i + 1).equals(">") ) { + break; + } + } + + // PARENTHESIS CHECK + if (stack.get(i).equals("(")) { + if (stack.get(i + 1).equals(")")) { + stack.remove(i); + stack.remove(i); + if (i == 0) i--; + else i -= 2; + continue; + } + if ( stack.get(i + 1).equals("}") || stack.get(i + 1).equals("]") + || stack.get(i + 1).equals(">") ) { + break; + } + } + + // DIAMONDS CHECK + if (stack.get(i).equals("<")) { + if (stack.get(i + 1).equals(">")) { + stack.remove(i); + stack.remove(i); + if (i == 0) i--; + else i -= 2; + continue; + } + if ( stack.get(i + 1).equals("}") || stack.get(i + 1).equals("]") + || stack.get(i + 1).equals(")") ) { + break; + } + } + + // DOUBLE_QUOTE CHECK + if (stack.get(i).equals("\"")) { + if (stack.get(i + 1).equals("\"")) { + stack.remove(i); + stack.remove(i); + if (i == 0) i--; + else i -= 2; + continue; + } + if ( stack.get(i + 1).equals("}") || stack.get(i + 1).equals("]") + || stack.get(i + 1).equals(")") || stack.get(i + 1).equals(">") ) { + break; + } + } + + // SINGLE_QUOTE CHECK + if (stack.get(i).equals("\'")) { + if (stack.get(i + 1).equals("\'")) { + stack.remove(i); + stack.remove(i); + if (i == 0) i--; + else i -= 2; + continue; + } + if ( stack.get(i + 1).equals("}") || stack.get(i + 1).equals("]") + || stack.get(i + 1).equals(")") || stack.get(i + 1).equals(">") ) { + break; + } + } + + } // end of for loop + + if (stack.size() != 0) { + areAllParensPaired = false; + } + return areAllParensPaired; + } + + public String getStackElementsAsString() { + StringBuilder sb = new StringBuilder(); + for (String string : stack) { + sb.append(string); + } + return sb.toString(); + } + + public boolean isFinalStackSizeZero() { + boolean isFinalStackSizeZero = false; + if (stack.size() == 0) { + isFinalStackSizeZero = true; + } + return isFinalStackSizeZero; + } + + public void makeStackSizeZero() { + stack.clear(); + } + + public static void main(String[] args){ + ParenChecker parenChecker = new ParenChecker(); + + String inputExpression = "1918-203948-209{}"; + parenChecker.addOnlyParenStringsToStack(inputExpression); + String cleanedInput = parenChecker.getStackElementsAsString(); + parenChecker.makeStackSizeZero(); + + System.out.println("FINAL RESULT"); + System.out.println("Are parens paired?: " + parenChecker.areAllParensPaired(inputExpression)); + System.out.println("Original String: " + inputExpression); + System.out.println("Final Stack: " + parenChecker.getStackElementsAsString()); + System.out.println(); + + System.out.println("REQUIREMENTS (should all be true)"); + System.out.println("Cleaned String: " + cleanedInput); + System.out.println("Is Cleaned String even?: " + parenChecker.isInputLengthEven(cleanedInput)); + System.out.println("Is first String correct?: " + parenChecker.isFirstCharCorrect(cleanedInput)); + System.out.println("Is last String correct?: " + parenChecker.isLastCharCorrect(cleanedInput)); + System.out.println("Is Final Stack size = 0?: " + parenChecker.isFinalStackSizeZero()); + System.out.println(); + + + } } diff --git a/src/main/java/io/zipcoder/ParenStrings.java b/src/main/java/io/zipcoder/ParenStrings.java new file mode 100644 index 0000000..822e337 --- /dev/null +++ b/src/main/java/io/zipcoder/ParenStrings.java @@ -0,0 +1,37 @@ +package io.zipcoder; + +public enum ParenStrings { + + PARENTHESES("(", ")"), + BRACKETS("[", "]"), + SQUIGGLIES("{", "}"), + DIAMONDS("<", ">"), + DOUBLE_QUOTES("\"", "\""), + SINGLE_QUOTES("\'", "\'"); + + private final String openingString; + private final String closingString; + + ParenStrings(String openingString, String closingString) { + this.openingString = openingString; + this.closingString = closingString; + } + + public String getOpeningString() { + return openingString; + } + + public String getClosingString() { + return closingString; + } + + public String getOppositeString(String input) { + String oppositeString = ""; + if (input.equals(openingString)) { + oppositeString = closingString; + } else if (input.equals(closingString)) { + oppositeString = openingString; + } + return oppositeString; + } +} diff --git a/src/main/java/io/zipcoder/WC.java b/src/main/java/io/zipcoder/WC.java index babb68c..3ad24db 100644 --- a/src/main/java/io/zipcoder/WC.java +++ b/src/main/java/io/zipcoder/WC.java @@ -2,11 +2,15 @@ import java.io.FileNotFoundException; import java.io.FileReader; -import java.util.Iterator; -import java.util.Scanner; +import java.lang.reflect.Array; +import java.util.*; +import java.util.Map.Entry; public class WC { + private Iterator si; + private LinkedHashMap linkedHashMap; + private final String DELIMITER = "[^a-zA-Z0-9]+"; public WC(String fileName) { try { @@ -15,9 +19,66 @@ public WC(String fileName) { System.out.println(fileName + " Does Not Exist"); System.exit(-1); } + linkedHashMap = new LinkedHashMap(); } public WC(Iterator si) { this.si = si; + linkedHashMap = new LinkedHashMap(); + } + + public LinkedHashMap getLinkedHashMap() { + return linkedHashMap; + } + + public void addWordsToLinkedHashMap() { + while(si.hasNext()) { + String[] words = si.next().split(DELIMITER); + for (String word : words) { + word = word.toLowerCase(); + if (linkedHashMap.get(word) == null) { + linkedHashMap.put(word, 1); + } else { + linkedHashMap.put(word, linkedHashMap.get(word) + 1); + } + } + } + } + + public String getWordCountInDescendingOrder() { + List> wordCount = new ArrayList<>(linkedHashMap.entrySet()); + StringBuilder sb = new StringBuilder(); + + Collections.sort(wordCount, new Comparator() { + public int compare(Entry e1, Entry e2) { + if ( (int)e2.getValue() == (int)e1.getValue() ) { + return 0; + } else if ( (int)e1.getValue() > (int)e2.getValue() ) { + return -1; + } else + return 1; + } + }); + + sb.append("Count = Word" + "\n"); + for (Entry entry : wordCount) { + sb.append(entry.getValue() + " = " + entry.getKey() + "\n"); + } + + return sb.toString(); + } + + public static void main(String[] args) { + + String inputExpression = "first second third fourth"; + WC wc = new WC(WC.class.getResource("/aeneid.txt").getFile()); + wc.addWordsToLinkedHashMap(); + wc.getLinkedHashMap(); +// for (Entry entry : wc.getLinkedHashMap().entrySet()) { +// System.out.println(entry.toString()); +// } + + System.out.println(wc.getWordCountInDescendingOrder()); + } } diff --git a/src/main/resources/aeneid.txt b/src/main/resources/aeneid.txt new file mode 100644 index 0000000..5640ff2 --- /dev/null +++ b/src/main/resources/aeneid.txt @@ -0,0 +1,7 @@ +Then to his glance appeared the accurst swordbelt surmounting Turnus' shoulder, +shining with its familiar studs—the strap Young Pallas wore when Turnus wounded him +and left him dead upon the field; now Turnus bore that enemy token on his shoulder—enemy still. +For when the sight came home to him, Aeneas raged at the relic of his anguish worn by this man as trophy. +Blazing up and terrible in his anger, he called out: "You in your plunder, torn from one of mine, +shall I be robbed of you? This wound will come from Pallas: Pallas makes this offering, +and from your criminal blood exacts his due." He sank his blade in fury in Turnus' chest ... \ No newline at end of file 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/test/java/io/zipcoder/ParenCheckerTest.java b/src/test/java/io/zipcoder/ParenCheckerTest.java index 76aa3b6..63c1ab8 100644 --- a/src/test/java/io/zipcoder/ParenCheckerTest.java +++ b/src/test/java/io/zipcoder/ParenCheckerTest.java @@ -3,6 +3,107 @@ import org.junit.Assert; import org.junit.Test; +import org.junit.Assert; +import org.junit.Test; + public class ParenCheckerTest { + private ParenChecker pc; + + @Test + public void isInputLengthEvenFalseTest() { + // Given + String oddLengthString = "(()"; + boolean expectedResponse = false; + // When + pc = new ParenChecker(); + boolean actualResponse = pc.isInputLengthEven(oddLengthString); + // Then + Assert.assertEquals(expectedResponse, actualResponse); + } + + @Test + public void isInputLengthEvenTrueTest() { + // Given + String evenLengthString = ")(()"; + boolean expectedResponse = true; + // When + pc = new ParenChecker(); + boolean actualResponse = pc.isInputLengthEven(evenLengthString); + // Then + Assert.assertEquals(expectedResponse, actualResponse); + } + + @Test + public void isFirstCharCorrectTrueTest() { + // Given + String input = "asdfasd(()"; + boolean expectedFirstCharTrueResponse = true; + // When + pc = new ParenChecker(); + boolean actualResponse = pc.isFirstCharCorrect(input); + // Then + Assert.assertEquals(expectedFirstCharTrueResponse, actualResponse); + } + + @Test + public void isFirstCharCorrectFalseTest() { + // Given + String input = "}(()"; + boolean expectedFirstCharFalseResponse = false; + // When + pc = new ParenChecker(); + boolean actualResponse = pc.isFirstCharCorrect(input); + // Then + Assert.assertEquals(expectedFirstCharFalseResponse, actualResponse); + } + + @Test + public void isLastCharCorrectTrueTest() { + // Given + String input = "(()}"; + boolean expectedLastCharTrueResponse = true; + // When + pc = new ParenChecker(); + boolean actualResponse = pc.isLastCharCorrect(input); + // Then + Assert.assertEquals(expectedLastCharTrueResponse, actualResponse); + } + + @Test + public void isLastCharCorrectFalseTest() { + // Given + String input = "}((){"; + boolean expectedLastCharFalseResponse = false; + // When + pc = new ParenChecker(); + boolean actualResponse = pc.isLastCharCorrect(input); + // Then + Assert.assertEquals(expectedLastCharFalseResponse, actualResponse); + } + + @Test + public void areAllParensPairedTrueTest() { + // Given + String input = "()()()"; + boolean expectedAreAllParensPairedTrue = true; + // When + pc = new ParenChecker(); + boolean actualAreAllParensPaired = pc.areAllParensPaired(input); + // Then + Assert.assertEquals(expectedAreAllParensPairedTrue, actualAreAllParensPaired); + } + + @Test + public void areAllParensPairedFalseTest() { + // Given + String input = "((("; + boolean expectedAreAllParensPairedFalse = false; + // When + pc = new ParenChecker(); + boolean actualAreAllParensPaired = pc.areAllParensPaired(input); + // Then + Assert.assertEquals(expectedAreAllParensPairedFalse, actualAreAllParensPaired); + } + } \ 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..ae33199 100644 --- a/src/test/java/io/zipcoder/WCTest.java +++ b/src/test/java/io/zipcoder/WCTest.java @@ -1,11 +1,36 @@ 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.Iterator; +import java.util.List; public class WCTest { + private WC wc; + + @Before + public void setupConstructorIteratorTest() { + // Given + String expectedFirstWord = "first"; + String wordsAsString = "first second third fourth"; + String[] wordsArray = wordsAsString.split("[a-zA-Z0-9]+"); + List wordsArrayList = new ArrayList(Arrays.asList(wordsArray)); + Iterator wordsArrayListIterator = wordsArrayList.iterator(); + wc = new WC(wordsArrayListIterator); + // When + String actualFirstWord = "hold"; + // Then + } + + @Test + public void createWordsArrayTest() { + + } + + } \ No newline at end of file