Skip to content

Complete #21

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 2 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
Binary file added .DS_Store
Binary file not shown.
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
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
Binary file added src/main/java/.DS_Store
Binary file not shown.
67 changes: 67 additions & 0 deletions src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,71 @@
package io.zipcoder;

import java.util.Stack;

public class ParenChecker {
public Stack<Character> stack = new Stack<Character>();


public ParenChecker(){

}
public ParenChecker(Stack<Character> stack) {
this.stack = stack;
}

public boolean isPairedParenthesis(String str){
for (int i = 0; i <str.length() ; i++) {
char current = str.charAt(i);
if (current == '(') {
stack.push(current);
} else if (current == ')') {
if (stack.isEmpty()){
return false;
}
else if (stack.peek() == '(') {
stack.pop();
}
}
}
return stack.isEmpty();
}



public boolean isBalancedBrackets(String str){
for (int i = 0; i <str.length() ; i++) {
char current = str.charAt(i);
if (current == '(' ||current == '{' ||current == '[' ||current == '<' ||
current == '\"' ||current == '\'' ) {
stack.push(current);
} else if (current == ')'||current == '}' ||current == ']' ||current == '>' ||
current == '"' ||current == '\'' ) {
if (stack.isEmpty()){
return false;
}
else if (stack.peek() == '(') {
stack.pop();
}
else if (stack.peek() == '{') {
stack.pop();
}
else if (stack.peek() == '[') {
stack.pop();
}
else if (stack.peek() == '<') {
stack.pop();
}
else if (stack.peek() == '\"') {
stack.pop();
}
else if (stack.peek() == '\'') {
stack.pop();
}
}
}
return stack.isEmpty();



}
}
69 changes: 66 additions & 3 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package io.zipcoder;

import jdk.nashorn.internal.runtime.regexp.joni.Regex;

import javax.swing.text.View;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class WC {
public class WC extends TreeMap<String, Integer>{
private Iterator<String> si;
private NavigableMap<String, Integer> wordCountMap = new TreeMap<String, Integer>();

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


public Map<String, Integer> wordCount() {
//iterator - hasNext() - returns true if there is something after
while (si.hasNext()) {
String[] words = si.next().replaceAll("\\W", "").toLowerCase().split(" ");
for (int i = 0; i < words.length; i++) {
incrementValue(wordCountMap, words[i]);
}

}

return descendingSortByValue(wordCountMap);

}

public void incrementValue(Map<String, Integer> map, String key){
Integer count = map.get(key);
if (count == null){
count = 0;
}
map.put(key, count +1);
}


public Map<String, Integer> descendingSortByValue(Map<String, Integer> map){
List<Map.Entry<String, Integer>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, ((o1, o2) -> o2.getValue().compareTo(o1.getValue())));

Map<String, Integer> temp = new LinkedHashMap<>();

for(Map.Entry<String, Integer> entry : list){
temp.put(entry.getKey(), entry.getValue());
}
return temp;

}

public String display() {
StringBuilder sb = new StringBuilder();
sb.append("Word Count in descending order:\n");
for(String key : descendingSortByValue(wordCountMap).keySet()){
sb.append("\t"+key + ": " + descendingSortByValue(wordCountMap).get(key) +"\n");
}
return sb.toString();
}


public static void main(String[] args) {
// WC.class.getResource("/Users/bolee/Dev/Week_6/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/AmericanFairyTale.txt").getFile();
// WC wc = new WC(WC.class.getResource("/Users/bolee/Dev/Week_6/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/AmericanFairyTale.txt").getFile());
//



}


}
Loading