Skip to content

Lab Complete #17

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

import java.util.Stack;

public class ParenChecker {

Stack<Character> checker = new Stack();

public boolean parenCheck(String check) {

for (int i = 0; i < check.length(); i++) {
if (check.charAt(i) == ')' && checker.isEmpty())
return false;
if (check.charAt(i) == '(') {
checker.push(check.charAt(i));
}
if (check.charAt(i) == ')' ) {
checker.pop();
}
}
return checker.isEmpty();
}

public boolean allCheck(String check) {
// Pair input = new Pair('&', '&');
for (int i = 0; i < check.length(); i++) {
char current = check.charAt(i);
if (current == '(' || current == '{'
|| current == '[' || current == '<' || current == '"' || current == '\'') {
checker.push(current);
}
if ((current == ')' || current == '}' || current == ']' || current == '>') || current == '"' || current == '\'') {
if (checker.isEmpty()) {
return false;
}
char last = checker.peek();
if (current == ')' && last == '(' || current == '}' && last == '{'
|| current == ']' && last == '[' || current == '>' && last == '<' || current == '"' && last == '"'
|| current == '\'' && last == '\'') {
checker.pop();
}
}
}
return checker.isEmpty();
}

// for (int i = 0; i < check.length(); i++) {
// char current = check.charAt(i);
// if(check.charAt(i) == input.getOpen())
// checker.push(check.charAt(i));
// char top = checker.peek();
// if(check.charAt(i) == input.getClose(top) && top == input.getOpen()) {
// checker.pop();
// }
//
//
//
// if ((current == ')' || current == '}' || current == ']' || current == '>') && checker.isEmpty()) {
// return false;
// }
// if (current == '(' || current == '{' || current == '[' ||
// current == '<' || current == '\'' || current == '"') {
// checker.push(current);
// }
// char top = checker.peek();
// if (current == ')' && top == '(' || current == '}' && top == '{' ||
// current == ']' && top == '[' || current == '>' && top == '<' ||
// current == '\'' && top == '\'' || current == '"' && top == '"') {
// checker.pop();
// }
// }
// return checker.isEmpty();
// }

}







58 changes: 55 additions & 3 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@

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

public class WC {
import static java.util.stream.Collectors.toMap;



public class WC{
private Iterator<String> si;
Map<String, Integer> reader = new LinkedHashMap<String, Integer>();


public WC(String fileName) {
try {
Expand All @@ -18,6 +24,52 @@ public WC(String fileName) {
}

public WC(Iterator<String> si) {

this.si = si;
}

public Map<String, Integer> wordCollector() {

int count = 0;
while (si.hasNext()) {
count++;
String nextWord = si.next().toLowerCase().replaceAll("[^a-z]", "");
if(reader.containsKey(nextWord)) {
reader.put(nextWord, reader.get(nextWord)+1);
} else {
reader.put(nextWord, 1);
}
} reader.put("Total Words", count);

return reader;
}


public Map<String, Integer> sortInDescOrderByValue() {
Map<String, Integer> sortByDesc = wordCollector().entrySet()
.stream()
.sorted(Map.Entry.<String, Integer> comparingByValue().reversed().thenComparing(Map.Entry.comparingByKey()))
.collect(toMap(Map.Entry::getKey,
Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));

return sortByDesc;
}

public String printMap(){
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Integer> entry: sortInDescOrderByValue().entrySet()) {
sb.append(entry.getKey() + ":" + " appears " + entry.getValue() + " times\n");
}
System.out.println(sb.toString());
return sb.toString();
}


public static void main(String[] args) {
WC wc = new WC("/Users/danielhorowitz/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/1400-0.txt");
wc.printMap();


}

}
Binary file added src/main/resources/.DS_Store
Binary file not shown.
Loading