Skip to content

one and two #28

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

public class ParenChecker {

Stack<Character> stack = new Stack<>();

public boolean parenCheck(String string) {
for (int i = 0; i < string.length(); i++) {
char character = string.charAt(i);

if (stack.isEmpty() && character == ')') {
return false;
} else if (character == '(') {
stack.push(character);
}
char last = stack.peek(); // last element added
if (character == ')' && last == '(') {
stack.pop();
}
}
return stack.isEmpty(); // when returning stack isEmpty it is true
}

public boolean isPair(String string) {
for (int i = 0; i < string.length(); i++) {
char current = string.charAt(i);
if (current == '(' ||
current == '{' ||
current == '<' ||
current == '[' ||
current == '\'' ||
current == '\"') {
stack.push(current); // adding left of parentheses to stack
}
char last = stack.peek();
if (current == ')' && last == '(' ||
current == '}' && last == '{' ||
current == '>' && last == '<' ||
current == ']' && last == '[' ||
current == '\'' && last == '\'' ||
current == '\"' && last == '\"') { // if contains right, delete it
stack.pop();
}
}
return stack.isEmpty();
}

}



48 changes: 45 additions & 3 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,64 @@

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 Map <String, Integer> wordsWithCounts = new LinkedHashMap<>();

public WC(String fileName) {
try {
this.si = new Scanner(new FileReader(fileName));
} catch (FileNotFoundException e) {
System.out.println(fileName + " Does Not Exist");
System.out.println(fileName + "Does Not Exist");
System.exit(-1);
}
}

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

public Map<String, Integer> readWords() {

while (si.hasNext()) {
String current = si.next().toLowerCase().replaceAll("[^a-zñáéíóú]", ""); // deletes all other special characters and replaces them w blank space
if (wordsWithCounts.containsKey(current)) {
wordsWithCounts.put(current, wordsWithCounts.get(current) + 1); // if we have this word -> get the int value at that key and add one
} else
wordsWithCounts.put(current, 1); // if we do not have the word, add it and set the count to one
}
return wordsWithCounts;
}

public void reverseOrder(){
// Map<String, Integer> sorted = readWords().entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
// .collect(toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
// return sorted;
this.wordsWithCounts.entrySet()
.stream()
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
.forEach(System.out::println);
}

// public String display(){
// readWords();
// reverseOrder();
// StringBuilder sb = new StringBuilder();
// for(String key : wordsWithCounts.keySet()){
// sb.append("\nWord: " + key + "\nCount:" + wordsWithCounts.get(key) + "\n__________");
// }
// System.out.println(sb.toString());
// return sb.toString();
// }

public static void main(String[] args) {
WC wc = new WC("/Users/jessicacampbell/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/DonQuijote.txt");
wc.readWords();
wc.reverseOrder();
}

}
Loading