Skip to content

Finished Lab #9

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
45 changes: 44 additions & 1 deletion src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,47 @@
package io.zipcoder;

import java.util.Stack;

public class ParenChecker {
}

Stack<Character> parenStack = new Stack();

public boolean parenCheck(String string){

for(int i = 0; i < string.length(); i++){
if(string.charAt(i)==')' && parenStack.empty()){
break;
}
if(string.charAt(i)=='('){
parenStack.push(string.charAt(i));
}
if(string.charAt(i)==')'){
parenStack.pop();
}
}

return parenStack.empty();
}

public boolean parenPairs(String string) {
for (int i = 0; i < string.length(); i++) {
char current = string.charAt(i);
if (current == '(' || current == '{'
|| current == '[' || current == '<' || current == '"' || current == '\'') {
parenStack.push(current);
}
if ((current == ')' || current == '}' || current == ']' || current == '>') || current == '"' || current == '\'') {
if (parenStack.isEmpty()) {
return false;
}
char last = parenStack.peek();
if (current == ')' && last == '(' || current == '}' && last == '{'
|| current == ']' && last == '[' || current == '>' && last == '<' || current == '"' && last == '"'
|| current == '\'' && last == '\'') {
parenStack.pop();
}
}
}
return parenStack.isEmpty();
}
}
59 changes: 57 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,13 @@

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 int counter = 0;

Map<String, Integer> reader = new HashMap<String,Integer>();

public WC(String fileName) {
try {
Expand All @@ -19,5 +21,58 @@ public WC(String fileName) {

public WC(Iterator<String> si) {
this.si = si;

}

public Map<String, Integer> wordCollector(){
while(si.hasNext()){
String nextWord = si.next().toLowerCase().replaceAll("[^a-z']", "");
if(reader.containsKey(nextWord)){
reader.put(nextWord, reader.get(nextWord) + 1);
counter++;
} else {
reader.put(nextWord, 1);
counter++;
}
}
return reader;
}

public Map<String, Integer> sortByCount(){

List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(reader.entrySet());

//sort the list in descending order with a comparator
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o2.getValue()).compareTo(o1.getValue());
}
});

//convert sortedMap back to Map
Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> entry : list) {
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
}

public int countWord() {
return counter;
}

public String printMap(Map<String, Integer> map){

StringBuilder mapBuilder = new StringBuilder();

for(String key : map.keySet()){
mapBuilder.append(String.format("%-2s", reader.get(key)));
mapBuilder.append(String.format("%-2s", ":"));
mapBuilder.append(key);
mapBuilder.append("\n");
}

return mapBuilder.toString();
}
}

8 changes: 8 additions & 0 deletions src/main/resources/testFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
I love love cheese.
I love every type of


cheese cheese cheese's!


I love apples.
Loading