Skip to content

Finished #31

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

import java.util.Stack;

public class ParenChecker {

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

public ParenChecker() {
}

public ParenChecker(Stack<Character> stack) {
this.stack = stack;
}

public boolean checkParen(String str)
{
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < str.length(); i++)
{
char current = str.charAt(i);
if (current == '{' || current == '(' || current == '[' || current == '<' || current == '"' || current == '\'')
{
stack.push(current);
}

if (current == '}' || current == ')' || current == ']' || current == '>' || current == '"' || current == '\'')
{
if (stack.isEmpty())
return false;

char last = stack.peek();
if (current == '}' && last == '{' || current == ')' && last == '(' || current == ']' && last == '[' || current == '>' && last == '<' || current == '"' && last == '"' || current == '\'' && last == '\'')
stack.pop();
else
return false;
}
}
return stack.isEmpty();
}

}
// public boolean checkParen(String str) {
// int opening = 0;
// int closing = 0;
// int total;
// for (int i = 0; i < str.length(); i++) {
// char current = str.charAt(i);
// if (current == '(') {
// opening++;
// }
// if (current == ')') {
// closing++;
// }
// total = opening - closing;
// if (total == 0) {
// return true;
// }
// }
// return false;
// }
37 changes: 35 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,15 @@

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 {
private Iterator<String> si;
public Map<String, Integer> wordMap = new LinkedHashMap<String, Integer>();


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

public Map<String, Integer> wordCountStorage() {

while (si.hasNext()) {
String current = si.next().toLowerCase().replaceAll("[^a-z0-9]", "");
if (!wordMap.containsKey(current)) {
wordMap.put(current, 1);
} else {
wordMap.put(current, wordMap.get(current) + 1);
}
}
return wordMap;
}

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

return sortByDesc;
}
}





//TreeMap<String, Integer> uniqueWordCount = new TreeMap<String, Integer>();
23 changes: 23 additions & 0 deletions src/main/resources/robertFrost.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Two roads diverged in a yellow wood,
And sorry I could not travel both
And be one traveler, long I stood
And looked down one as far as I could
To where it bent in the undergrowth;

Then took the other, as just as fair,
And having perhaps the better claim,
Because it was grassy and wanted wear;
Though as for that the passing there
Had worn them really about the same,

And both that morning equally lay
In leaves no step had trodden black.
Oh, I kept the first for another day!
Yet knowing how way leads on to way,
I doubted if I should ever come back.

I shall be telling this with a sigh
Somewhere ages and ages hence:
Two roads diverged in a wood, and I--
I took the one less traveled by,
And that has made all the difference.
1 change: 1 addition & 0 deletions src/main/resources/someTextFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello my name is Keith. I am a person. I am 26.
62 changes: 62 additions & 0 deletions src/test/java/io/zipcoder/ParenCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,66 @@

public class ParenCheckerTest {

@Test
public void testCheckParenOne(){
ParenChecker parenChecker = new ParenChecker();
String str = "(This is a test) string with some (parenthesis)";

boolean expected = true;
boolean actual = parenChecker.checkParen(str);
Assert.assertEquals(expected, actual);
}


@Test
public void testCheckParenTwo(){
ParenChecker parenChecker = new ParenChecker();
String str = ")This is a test( string with some (parenthesis)";

boolean expected = false;
boolean actual = parenChecker.checkParen(str);
Assert.assertEquals(expected, actual);
}

@Test
public void testCheckParenThree(){
ParenChecker parenChecker = new ParenChecker();
String str = "(This is( a test (string with some parenthesis)";

boolean expected = false;
boolean actual = parenChecker.checkParen(str);
Assert.assertEquals(expected, actual);
}

@Test
public void testCheckParenFour(){
ParenChecker parenChecker = new ParenChecker();
String str = "<This {[is( a test string with} some parenthesis)>]";

boolean expected = false;
boolean actual = parenChecker.checkParen(str);
Assert.assertEquals(expected, actual);
}

@Test
public void testCheckParenFive(){
ParenChecker parenChecker = new ParenChecker();
String str = " <{[( )]} >";

boolean expected = true;
boolean actual = parenChecker.checkParen(str);
Assert.assertEquals(expected, actual);
}

@Test
public void testCheckParenSix(){
ParenChecker parenChecker = new ParenChecker();
String str = "< This ' is a ' test \"String\" with some parenthesis>";

boolean expected = true;
boolean actual = parenChecker.checkParen(str);
Assert.assertEquals(expected, actual);
}


}
34 changes: 33 additions & 1 deletion src/test/java/io/zipcoder/WCTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,39 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.TreeMap;

public class WCTest {

}
@Test
public void testWordCounter() {

WC testWC = new WC(WC.class.getResource("/someTextFile.txt").getFile());
String expected = "{26=1, a=1, am=2, hello=1, i=2, is=1, keith=1, my=1, name=1, person=1}";
String actual = testWC.wordCountStorage().toString();

Assert.assertEquals(expected, actual);
}

@Test
public void testWordCounterTwo() {

WC testWC = new WC(WC.class.getResource("/robertFrost.txt").getFile());
String expected = "{two=2, roads=2, diverged=2, in=4, a=3, yellow=1, wood=2, and=9, sorry=1, i=9, could=2, not=1, travel=1, both=2, be=2, one=3, traveler=1, long=1, stood=1, looked=1, down=1, as=5, far=1, to=2, where=1, it=2, bent=1, the=8, undergrowth=1, then=1, took=2, other=1, just=1, fair=1, having=1, perhaps=1, better=1, claim=1, because=1, was=1, grassy=1, wanted=1, wear=1, though=1, for=2, that=3, passing=1, there=1, had=2, worn=1, them=1, really=1, about=1, same=1, morning=1, equally=1, lay=1, leaves=1, no=1, step=1, trodden=1, black=1, oh=1, kept=1, first=1, another=1, day=1, yet=1, knowing=1, how=1, way=2, leads=1, on=1, doubted=1, if=1, should=1, ever=1, come=1, back=1, shall=1, telling=1, this=1, with=1, sigh=1, somewhere=1, ages=2, hence=1, less=1, traveled=1, by=1, has=1, made=1, all=1, difference=1}";

String actual = testWC.wordCountStorage().toString();

Assert.assertEquals(expected, actual);
}

@Test
public void testSortedWordCount(){
WC testWC = new WC(WC.class.getResource("/someTextFile.txt").getFile());
String expected = "{i=2, am=2, hello=1, my=1, name=1, is=1, keith=1, a=1, person=1, 26=1}";
String actual = testWC.sortInDescOrderByValue().toString();

Assert.assertEquals(expected, actual);

}

}