Skip to content

gotta learn more about linked lists i guess #24

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 14 commits into
base: master
Choose a base branch
from
17 changes: 17 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.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand All @@ -17,4 +29,9 @@
</dependency>
</dependencies>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

</project>
29 changes: 29 additions & 0 deletions src/main/java/io/zipcoder/Enclosers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.zipcoder;

import java.util.ArrayList;
import java.util.Arrays;

public enum Enclosers {
PAREN("(", ")"),
CURLY("{", "}"),
BRACKET("[", "]"),
CAROT("<", ">"),
;

private String open;
private String close;

Enclosers(String open, String close){
this.open = open;
this.close = close;
}

public String getOpen() {
return open;
}

public String getClose() {
return close;
}

}
97 changes: 97 additions & 0 deletions src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,101 @@
package io.zipcoder;

import java.util.*;

public class ParenChecker {
public String string;
public Stack<String> parens;
private Stack<Enclosers> enclosers;
private Stack<String> quotes;
private Stack<String> doublequotes;

public ParenChecker(String entry){
this.string = entry;
this.parens = new Stack<String>();
this.enclosers = new Stack<Enclosers>();
this.quotes = new Stack<String>();
this.doublequotes = new Stack<String>();
}

public boolean checkParen(){
String[] tempArray = string.split("");
for (String ch:tempArray){
if (ch.equals("(")) parens.push(ch);
if (ch.equals(")")){
try {
parens.pop();
} catch (Exception exception){
return false;
}
}
}
if (parens.size() > 0) return false;
return true;
}

/**push opener
* iterate, peek
* it it isn't the corresponding close character, move to next
* if it IS, pop
* if your stack at the end is > 0, return false
* fallout return true
*/

public boolean checkCharacters(){
String[] tempArray = string.split("");
ArrayList<Enclosers> enclosersList = new ArrayList<Enclosers>(Arrays.asList(Enclosers.values()));
ArrayList<String> closerList = new ArrayList<String>();
for (Enclosers encloser:enclosersList) {
closerList.add(encloser.getClose());
}
for (String ch:tempArray){
checkOpener(enclosersList, ch);
if (checkCloser(closerList, ch)) return false;
checkSingleQuote(ch);
checkDoubleQuote(ch);
}
if (enclosers.size() > 0) return false;
if (quotes.size() > 0) return false;
if (doublequotes.size() > 0) return false;
return true;
}

private void checkDoubleQuote(String ch) {
if (ch.equals("\'")){
if (doublequotes.size() == 1) {
doublequotes.pop();
} else if (doublequotes.size() == 0){
doublequotes.push(ch);
}
}
}

private void checkSingleQuote(String ch) {
if (ch.equals("'")){
if (quotes.size() == 1) {
quotes.pop();
} else if (quotes.size() == 0){
quotes.push(ch);
}
}
}

private boolean checkCloser(ArrayList<String> closerList, String ch) {
if (closerList.contains(ch)){
try {
if (ch.equals(enclosers.peek().getClose())) enclosers.pop();
else return true;
} catch (EmptyStackException exception) {
return true;
}
}
return false;
}

private void checkOpener(ArrayList<Enclosers> enclosersList, String ch) {
for (Enclosers encloser:enclosersList) {
if (ch.equals(encloser.getOpen())) enclosers.push(encloser);
}
}

}
28 changes: 26 additions & 2 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.Scanner;
import java.util.*;
import static java.util.stream.Collectors.toMap;

public class WC {
private Iterator<String> si;
Expand All @@ -20,4 +20,28 @@ public WC(String fileName) {
public WC(Iterator<String> si) {
this.si = si;
}

public ArrayList<String> removePunctuation() {
ArrayList<String> answer = new ArrayList<>();
while (si.hasNext()) {
answer.add(si.next().toLowerCase().replaceAll("[^a-zA-Z`]", ""));
}
return answer;
}

public Map<String, Integer> wordCount(){
ArrayList<String> filtered = removePunctuation();
Map<String, Integer> answer = new LinkedHashMap<>();
for (String word:filtered) {
if (!answer.containsKey(word)) answer.put(word, 1);
else answer.put(word, answer.get(word) + 1);
}
Map<String, Integer> sorted = answer.entrySet().stream().sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
for (Map.Entry entry:sorted.entrySet()) {
System.out.println(entry.getKey() + " | " + entry.getValue());
}
return sorted;
}

}
1 change: 1 addition & 0 deletions src/main/resources/duckduck2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
duck duck duck ,duc{k goose }\heron c@hicken heron heron chicken
1 change: 1 addition & 0 deletions src/main/resources/duckduckgoose.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
duck duck, Goose
Loading