Skip to content

Complete #8

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 3 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.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,57 @@
package io.zipcoder;

import java.util.Stack;

public class ParenChecker {

private String sentence = "";

static boolean isMatchingPair(char character1, char character2)
{
if (character1 == '(' && character2 == ')')
return true;
else if (character1 == '{' && character2 == '}')
return true;
else if (character1 == '[' && character2 == ']')
return true;
else if (character1 == '<' && character2 == '>'){
return true;
}
else if (character1 == '\"' && character2 == '\"'){
return true;
}
else if (character1 == '\'' && character2 == '\''){
return true;
}
else
return false;
}

public boolean parenChecker(String sentence){

Stack<Character> stack = new Stack<>();
char[] characters = sentence.toCharArray();
for (int i = 0; i < characters.length; i++){
if (characters[i] == '(' || characters[i] == '{' || characters[i] == '[' ||
characters[i] == '<' || characters[i] == '\"' || characters[i] == '\''){
stack.push(characters[i]);
continue;
}
if (characters[i] == ')' || characters[i] == '}' || characters[i] == ']' ||
characters[i] == '>' || characters[i] == '\"' || characters[i] == '\''){
if (stack.isEmpty()){
return false;
} else {
if (!isMatchingPair(stack.peek(), characters[i]))
return false;
else
stack.pop();
}
}
}
if (stack.isEmpty()){
return true;
}
return false;
}
}
49 changes: 46 additions & 3 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.zipcoder;


import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.Scanner;
import java.util.*;

public class WC {
private Iterator<String> si;
private LinkedHashMap<String, Integer> wordCount;
private String separator = "[^\\w]+";

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

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

public void textIterator(){
wordCount = new LinkedHashMap<>();
while (si.hasNext()){
//put all the words in the text into a string[].
String[] allTextSeparated = si.next().toLowerCase().split(separator);
for (String word : allTextSeparated){
if (wordCount.containsKey(word)){
int value = wordCount.get(word) + 1;
wordCount.put(word, value);
}
else {
wordCount.put(word, 1);
}
}
}
}
public List <Map.Entry<String, Integer>> sorted(){
List<Map.Entry<String, Integer>> sortedMap = new ArrayList<>(wordCount.entrySet());
Collections.sort(sortedMap, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o2.getValue()).compareTo(o1.getValue());
}

});
for(Map.Entry<String, Integer> entry: sortedMap){
System.out.println(entry.getKey() + " : " + entry.getValue());
}
return sortedMap;
}
public String toString(){
StringBuilder usedForTest = new StringBuilder();
for (Map.Entry<String, Integer> entry : sorted()){
usedForTest.append(entry.getKey() + " : " + entry.getValue() + "\n");
}
return usedForTest.toString();
}


}
Loading