Skip to content

Complete #8

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

import java.util.Stack;

public class ParenChecker {

private String sentence = "";

static boolean isMatchingPair(char character1, char character2)
{
if (character1 == '(' && character2 == ')')
return true;
else if (character1 == '{' && character2 == '}')
return true;
else if (character1 == '[' && character2 == ']')
return true;
else if (character1 == '<' && character2 == '>'){
return true;
}
else if (character1 == '\"' && character2 == '\"'){
return true;
}
else if (character1 == '\'' && character2 == '\''){
return true;
}
else
return false;
}

public boolean parenChecker(String sentence){

Stack<Character> stack = new Stack<>();
char[] characters = sentence.toCharArray();
for (int i = 0; i < characters.length; i++){
if (characters[i] == '(' || characters[i] == '{' || characters[i] == '[' ||
characters[i] == '<' || characters[i] == '\"' || characters[i] == '\''){
stack.push(characters[i]);
continue;
}
if (characters[i] == ')' || characters[i] == '}' || characters[i] == ']' ||
characters[i] == '>' || characters[i] == '\"' || characters[i] == '\''){
if (stack.isEmpty()){
return false;
} else {
if (!isMatchingPair(stack.peek(), characters[i]))
return false;
else
stack.pop();
}
}
}
if (stack.isEmpty()){
return true;
}
return false;
}
}
49 changes: 46 additions & 3 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package io.zipcoder;


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 LinkedHashMap<String, Integer> wordCount;
private String separator = "[^\\w]+";

public WC(String fileName) {
try {
@@ -15,9 +17,50 @@ public WC(String fileName) {
System.out.println(fileName + " Does Not Exist");
System.exit(-1);
}
wordCount = new LinkedHashMap<>();
}

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

public void textIterator(){
wordCount = new LinkedHashMap<>();
while (si.hasNext()){
//put all the words in the text into a string[].
String[] allTextSeparated = si.next().toLowerCase().split(separator);
for (String word : allTextSeparated){
if (wordCount.containsKey(word)){
int value = wordCount.get(word) + 1;
wordCount.put(word, value);
}
else {
wordCount.put(word, 1);
}
}
}
}
public List <Map.Entry<String, Integer>> sorted(){
List<Map.Entry<String, Integer>> sortedMap = new ArrayList<>(wordCount.entrySet());
Collections.sort(sortedMap, new Comparator<Map.Entry<String, Integer>>() {
@Override
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o2.getValue()).compareTo(o1.getValue());
}

});
for(Map.Entry<String, Integer> entry: sortedMap){
System.out.println(entry.getKey() + " : " + entry.getValue());
}
return sortedMap;
}
public String toString(){
StringBuilder usedForTest = new StringBuilder();
for (Map.Entry<String, Integer> entry : sorted()){
usedForTest.append(entry.getKey() + " : " + entry.getValue() + "\n");
}
return usedForTest.toString();
}


}
10,290 changes: 10,290 additions & 0 deletions src/main/resources/NineteenEightyFour.txt

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions src/main/resources/jabberwocky.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Jabberwocky
BY LEWIS CARROLL
’Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.

“Beware the Jabberwock, my son!
The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun
The frumious Bandersnatch!”

He took his vorpal sword in hand;
Long time the manxome foe he sought—
So rested he by the Tumtum tree
And stood awhile in thought.

And, as in uffish thought he stood,
The Jabberwock, with eyes of flame,
Came whiffling through the tulgey wood,
And burbled as it came!

One, two! One, two! And through and through
The vorpal blade went snicker-snack!
He left it dead, and with its head
He went galumphing back.

“And hast thou slain the Jabberwock?
Come to my arms, my beamish boy!
O frabjous day! Callooh! Callay!”
He chortled in his joy.

’Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.
Empty file.
4 changes: 4 additions & 0 deletions src/main/resources/superShort.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Dogs love to eat dog food, not because they are dogs, but because it is dog food.

Eye cannot believe how easy it is to go to Wendy's and get a 4 for 4.

71 changes: 71 additions & 0 deletions src/test/java/io/zipcoder/ParenCheckerTest.java
Original file line number Diff line number Diff line change
@@ -5,4 +5,75 @@

public class ParenCheckerTest {

@Test
public void testParens(){
ParenChecker testChecker = new ParenChecker();
String parens = "Hello, I am method()";
boolean expected = true;
boolean actual = testChecker.parenChecker(parens);
Assert.assertEquals(expected, actual);
}
@Test
public void testParensNotPaired(){
ParenChecker testChecker = new ParenChecker();
String parens = "Unpaired )";
boolean expected = false;
boolean actual = testChecker.parenChecker(parens);
Assert.assertEquals(expected, actual);
}
@Test
public void testParensWordIn(){
ParenChecker testChecker = new ParenChecker();
String parens = "(Hello)";
boolean expected = true;
boolean actual = testChecker.parenChecker(parens);
Assert.assertEquals(expected, actual);
}
@Test
public void testParensHalf(){
ParenChecker testChecker = new ParenChecker();
String parens = "(e))";
boolean expected = false;
boolean actual = testChecker.parenChecker(parens);
Assert.assertEquals(expected, actual);
}
@Test
public void testParensRandom(){
ParenChecker testChecker = new ParenChecker();
String parens = "asl(kAsdlajsldjadasdj())dljsa";
boolean expected = true;
boolean actual = testChecker.parenChecker(parens);
Assert.assertEquals(expected, actual);
}
@Test
public void testOthers(){
ParenChecker testChecker = new ParenChecker();
String parens = "das\"dad\"sda\'sda\"\"sdad";
boolean expected = false;
boolean actual = testChecker.parenChecker(parens);
Assert.assertEquals(expected, actual);
}
@Test
public void testFailOther(){
ParenChecker testChecker = new ParenChecker();
String parens = "\"Hello' I am not right\"";
boolean expected = false;
boolean actual = testChecker.parenChecker(parens);
Assert.assertEquals(expected, actual);
}
@Test
public void testMulti(){
ParenChecker testChecker = new ParenChecker();
String parens = "RR{}R[R]R>";
boolean expected = false;
boolean actual = testChecker.parenChecker(parens);
Assert.assertEquals(expected, actual);
}
@Test
public void testDontKnow(){
ParenChecker testChecker = new ParenChecker();
String parens = "alsjdalkjs\"\"d<()(A)D(ASD()[]}[";
boolean actual = testChecker.parenChecker(parens);
Assert.assertFalse(actual);
}
}
50 changes: 50 additions & 0 deletions src/test/java/io/zipcoder/WCTest.java
Original file line number Diff line number Diff line change
@@ -5,7 +5,57 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class WCTest {

@Test
public void testShort(){
WC test = new WC(WC.class.getResource("/superShort.txt").getFile());
test.textIterator();
String expected = test.toString();
String actual = "to : 3\n" +
"dogs : 2\n" +
"dog : 2\n" +
"food : 2\n" +
"because : 2\n" +
"it : 2\n" +
"is : 2\n" +
"4 : 2\n" +
"love : 1\n" +
"eat : 1\n" +
"not : 1\n" +
"they : 1\n" +
"are : 1\n" +
"but : 1\n" +
"eye : 1\n" +
"cannot : 1\n" +
"believe : 1\n" +
"how : 1\n" +
"easy : 1\n" +
"go : 1\n" +
"wendy : 1\n" +
"s : 1\n" +
"and : 1\n" +
"get : 1\n" +
"a : 1\n" +
"for : 1\n";
Assert.assertEquals(expected, actual);


}
@Test
public void testMedium(){
WC test = new WC(WC.class.getResource("/jabberwocky.txt").getFile());
test.textIterator();
test.toString();
}
@Test
public void testLong(){
WC test = new WC(WC.class.getResource("/NineteenEightyFour.txt").getFile());
test.textIterator();
test.toString();

}
}