Skip to content

Finished parts 1, 2, 3. #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<groupId>io.zipcoder</groupId>
<artifactId>collections</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
235 changes: 235 additions & 0 deletions src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
@@ -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<String> stack;
private EnumSet<ParenStrings> parenStrings;


public ParenChecker() {
this.stack = new Stack<String>();
this.parenStrings = EnumSet.allOf(ParenStrings.class);
}

public ParenChecker(String inputExpression) {
this.inputExpression = inputExpression;
this.stack = new Stack<String>();
this.parenStrings = EnumSet.allOf(ParenStrings.class);
}

public Stack<String> 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();


}
}
37 changes: 37 additions & 0 deletions src/main/java/io/zipcoder/ParenStrings.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
65 changes: 63 additions & 2 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> si;
private LinkedHashMap<String, Integer> linkedHashMap;
private final String DELIMITER = "[^a-zA-Z0-9]+";

public WC(String fileName) {
try {
Expand All @@ -15,9 +19,66 @@ public WC(String fileName) {
System.out.println(fileName + " Does Not Exist");
System.exit(-1);
}
linkedHashMap = new LinkedHashMap<String, Integer>();
}

public WC(Iterator<String> si) {
this.si = si;
linkedHashMap = new LinkedHashMap<String, Integer>();
}

public LinkedHashMap<String, Integer> 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<Entry<String, Integer>> wordCount = new ArrayList<>(linkedHashMap.entrySet());
StringBuilder sb = new StringBuilder();

Collections.sort(wordCount, new Comparator<Entry>() {
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());

}
}
7 changes: 7 additions & 0 deletions src/main/resources/aeneid.txt
Original file line number Diff line number Diff line change
@@ -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 ...
Empty file.
Loading