Skip to content

part2 #15

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.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,55 @@
package io.zipcoder;


import java.util.Stack;

//1.Create a class with a method that verifies all parens () are paired. HINT: Use a stack.
public class ParenChecker {

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

public ParenChecker() {

}

public boolean parenChecker(String value) {

for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (c == '(') {
stack.push(c);
} else if (c == ')') {
if (stack.isEmpty()) {
return false;
}
stack.pop();
}

}
return stack.isEmpty();
}


public boolean pairParen(String value) {
for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (c == '(' || c == '[' || c == '<' || c == '{' || c == '\'' || c == '\"') {
stack.push(c);
} else if (c == ')' || c == ']' || c == '>' || c == '}' || c == '\'' || c == '\"') {
if (stack.isEmpty()) {
return false;
}
char lastValue = stack.peek();
if (c == ')' && lastValue == '(' || c == '>' && lastValue == '<' || c == '"' && lastValue == '"' || c == '\'' && lastValue == '\'') {
stack.pop();
}
}
}
return stack.isEmpty();
}
}

//PseudoCode
//the lastValue equals the stack.peek(); the peek: Looks at the object at the top of this stack without removing it
//from the stack.

44 changes: 42 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,11 @@

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 TreeMap<String, Integer> map = new TreeMap<String, Integer>();

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

public WC(Iterator<String> si) {

this.si = si;
}

public Map<String, Integer> count(){
while(si.hasNext()){
String[] characters = si.next().replaceAll("\\W", "").toLowerCase().split(" ");
for(int i = 0; i<characters.length; i++){
increment(map, characters[i]);
}
}
return map;
}

public void increment(Map<String, Integer> map, String index){
Integer counter = map.get(index);
if(counter==null){
counter = 0;
}
map.put(index, counter + 1);
}

public String displayMap(){
Collections.sort(Map,(o1, o2) -> o2.getValue().compareTo(o1.getValue()));


StringBuilder sb = new StringBuilder();
sb.append("Count in descending order: \n");
for(String index : map.keySet()){
sb.append("\t"+index + ": " + map.get(index) +"\n");
}
return sb.toString()

}
}
//PseudoCode
//create a counter
//loop through si
//and make all words characters except symbols and numbers and all lowercase

//Actual Code Explanation
//Create a treemap up top, it holds the keys (index) and the value(word)
//
14 changes: 14 additions & 0 deletions src/main/resources/someTextFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
TWO TRAMPS


A man and a boy were ascending a steep street in a country town in
eastern New York. The man was tall and dark-complexioned, with a
sinister look which of itself excited distrust. He wore a slouch hat,
which, coming down over his forehead, nearly concealed from view his
low, receding brow. A pair of black, piercing eyes looked out from
beneath the brim. The first impression produced upon those who met him
was that he was of gypsy blood, and the impression was a correct one.
Where he was born no one seemed to know; perhaps he did not himself
know, for all his life he had been a wanderer, but English was the
tongue which he spoke, and, apart from the gypsy dialect, he knew no
other.
1 change: 1 addition & 0 deletions src/main/resources/testFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yolo yolo rolly polo
41 changes: 41 additions & 0 deletions src/test/java/io/zipcoder/ParenCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,47 @@
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.*;

public class ParenCheckerTest {

@Test
public void testParenChecker(){
//Given
ParenChecker parenChecker = new ParenChecker();
String value = "I rock (rough) and stuff with my (afro) puffs!";
//When
boolean pairParen = parenChecker.parenChecker(value);
Assert.assertTrue(pairParen);
}

@Test
public void testParenChecker2(){
//Given
ParenChecker parenChecker = new ParenChecker();
String value = "{(/)}<";
//When
boolean pairParen = parenChecker.parenChecker(value);
Assert.assertTrue(pairParen);
}

@Test
public void testPairParen(){
//Given
ParenChecker parenTest = new ParenChecker();
String value = "I love <TDD> so much";
//When
Assert.assertTrue(parenTest.pairParen(value));
}

@Test
public void testPairParen2(){
//Given
ParenChecker parenTest = new ParenChecker();
String value = "<Zip Code Wilmington> pow (pow)";
//When
Assert.assertTrue(parenTest.pairParen(value));
}


}
116 changes: 116 additions & 0 deletions src/test/java/io/zipcoder/WCTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,121 @@
import java.util.Arrays;

public class WCTest {
@Test
public void count(){
WC wc = new WC("/Users/katricewilliams-dredden/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/someTextFile.txt");
wc.count();
String expected = "Count in descending order: \n" +

"\ta: 9\n"+
"\tall: 1\n" +
"\tand: 3\n" +
"\tand,: 1\n" +
"\tapart: 1\n" +
"\tascending: 1\n" +
"\tbeen: 1\n" +
"\tbeneath: 1\n" +
"\tblack,: 1\n" +
"\tblood,: 1\n" +
"\tborn: 1\n" +
"\tboy: 1\n" +
"\tbrim.: 1\n" +
"\tbrow.: 1\n" +
"\tbut: 1\n" +
"\tcoming: 1\n" +
"\tconcealed: 1\n" +
"\tcorrect: 1\n" +
"\tcountry: 1\n" +
"\tdark-complexioned,: 1\n" +
"\tdialect,: 1\n" +
"\tdid: 1\n" +
"\tdistruct.: 1\n" +
"\tdown: 1\n" +
"\teastern: 1\n" +
"\tenglish: 1\n" +
"\texcited: 1\n" +
"\teyes: 1\n" +
"\tfirst: 1\n" +
"\tfor: 1\n" +
"\tforehead: 1\n" +
"\tfrom: 3\n" +
"\tgypsy: 2\n" +
"\thad: 1\n" +
"\that,: 1\n" +
"\the: 7\n" +
"\thim: 1\n" +
"\thimself: 1\n" +
"\this: 3\n" +
"\timpression: 2\n" +
"\tin: 2\n" +
"\titself: 1\n" +
"\tknew: 1\n" +
"\tknow,: 1\n" +
"\tknow;: 1\n" +
"\tlife: 1\n" +
"\tlook: 1\n" +
"\tlooked: 1\n" +
"\tlow,: 1\n" +
"\tman: 2\n" +
"\tmet: 1\n" +
"\tnearly: 1\n" +
"\tnew: 1\n" +
"\tno: 2\n" +
"\tnot: 1\n" +
"\tof: 3\n" +
"\tone: 1\n" +
"\tother.: 1\n" +
"\tout: 1\n" +
"\tover: 1\n" +
"\tpair: 1\n" +
"\tperhaps: 1\n" +
"\tpiercing: 1\n" +
"\tproduced: 1\n" +
"\treceding: 1\n" +
"\tseemed: 1\n" +
"\tsinister: 1\n" +
"\tslouch: 1\n" +
"\tspoke,: 1\n" +
"\tsteep: 1\n" +
"\tstreet: 1\n" +
"\ttall: 1\n" +
"\tthat: 1\n" +
"\tthe: 6\n" +
"\tthose: 1\n" +
"\tto: 1\n" +
"\ttongue: 1\n" +
"\ttown: 1\n" +
"\ttramps: 1\n" +
"\ttwo: 1\n" +
"\tupon: 1\n" +
"\tview: 1\n" +
"\twanderer,: 1\n" +
"\twas: 6\n" +
"\twere: 1\n" +
"\twhere: 1\n" +
"\twhich: 2\n" +
"\twhich,: 1\n" +
"\twho: 1\n" +
"\twith: 1\n" +
"\twore: 1\n" +
"\tyork: 1\n";


String actual = wc.displayMap();
Assert.assertEquals(expected, actual);
}

@Test
public void displayMap(){
WC wc = new WC("/Users/katricewilliams-dredden/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/testFile.txt");
wc.count();
String expected = "Count in descending order: \n" +
"\tpolo: 1\n" +
"\trolly: 1\n" +
"\tyolo: 2\n";
String actual = wc.displayMap();
Assert.assertEquals(expected, actual);

}

}