Skip to content

Finished Lab #12

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 5 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
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@
<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>
<groupId>junit</groupId>
Expand Down
28 changes: 26 additions & 2 deletions src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
package io.zipcoder;

public class ParenChecker {
}
import java.util.Stack;

public class ParenChecker {

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

public boolean verifyOpeningCharactersHaveAClosingOne(String input) {
String changedInput = input.replaceAll("[\\(\\{\\[\\<]","(");
changedInput = changedInput.replaceAll("[\\)\\}\\]\\>]", ")");
changedInput = changedInput.replaceAll("[\\\"\\']", "^");
for (int i = 0; i < input.length(); i++) {
char inputCharacter = changedInput.charAt(i);
if (! parensStack.isEmpty() && ((inputCharacter == ')' || inputCharacter == '^') && (parensStack.peek() == '(' || parensStack.peek() == '^'))) {
parensStack.pop();
}
else if (inputCharacter == '(' || inputCharacter == '^') {
parensStack.push(inputCharacter);
} else if (inputCharacter == ')' || parensStack.peek() != '(') {
return false;
}
}
return parensStack.isEmpty();

}
}

57 changes: 55 additions & 2 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

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

import static java.util.stream.Collectors.toMap;

public class WC {

Map<String, Integer> wordsAndTheirCounts = new LinkedHashMap<String, Integer>();
private Iterator<String> si;

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

//Sort values into map method
public Map<String, Integer> countNumberOfTimesWordsOccur(){
Integer wordCount = 0;
while(si.hasNext()){
wordCount++;
String word = si.next().toLowerCase().replaceAll("[^a-z]", "");
if(wordsAndTheirCounts.containsKey(word)){
wordsAndTheirCounts.put(word, wordsAndTheirCounts.get(word) +1);
} else{
wordsAndTheirCounts.put(word, 1);
}
}
wordsAndTheirCounts.put("Total Words", wordCount);
return wordsAndTheirCounts;
}

//Order map in descending order method
public Map<String, Integer> sortMapIntoDescendingOrder() {
List<Map.Entry<String, Integer>> toSort = new ArrayList<>();
for (Map.Entry<String, Integer> stringIntegerEntry : countNumberOfTimesWordsOccur().entrySet()) {
toSort.add(stringIntegerEntry);
}
toSort.sort(Map.Entry.<String, Integer>
comparingByValue().reversed().thenComparing(Map.Entry.comparingByKey()));
Map<String, Integer> wordsAndTheirCountsDesc = new LinkedHashMap<>();
for (Map.Entry<String, Integer> stringIntegerEntry : toSort) {
wordsAndTheirCountsDesc.putIfAbsent(stringIntegerEntry.getKey(), stringIntegerEntry.getValue());
}
return wordsAndTheirCountsDesc;
}

//Print map method
public String printMap(){
StringBuilder printedList = new StringBuilder();
for (Map.Entry<String, Integer> entry : sortMapIntoDescendingOrder().entrySet()){
printedList.append(entry.getKey());
printedList.append(":");
printedList.append(" appears ");
printedList.append(entry.getValue());
printedList.append(" times\n");
}
return printedList.toString();

}


/// ^ reads like does not equal
// ::

}
Loading