Skip to content

first method good, second needs to account for the spaces in between … #11

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

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class ParenChecker {
//Create a class with a method that verifies all parens () are paired. HINT: Use a stack.
private Stack theStack;
private char charAt;


public ParenChecker() {
this.theStack = new Stack();
}

public boolean verifyParens(String theString) {
// char[] theChar = theString.toCharArray();
char parOpen = '(';
char parClosed = ')';
for (int i = 0; i < theString.length(); i++) {
charAt = theString.charAt(i);
if (theStack.contains(parOpen) && charAt == parClosed) {
theStack.pop();
} else if (charAt == parOpen || charAt == parClosed) {
theStack.push(charAt);
}
}

return theStack.empty();
}


public boolean verifyAllOpensHaveEnds(String theString) {
boolean allOpensHavePairs = false;
theStack.clear();
Map<Character, Character> bracketPairs = new HashMap<Character, Character>();
bracketPairs.put('(', ')');
bracketPairs.put('{', '}');
bracketPairs.put('[', ']');
bracketPairs.put('<', '>');
bracketPairs.put('"', '"');
bracketPairs.put('\'', '\'');

for (int i = 0; i < theString.length(); i++) {
char theChar = theString.charAt(i);
if (bracketPairs.containsKey(theChar)) {
theStack.push(theChar);
}

//how to account all things in between the open brackets.
//theStackisempty;

else if (bracketPairs.get(theStack.peek()) == theChar){
theStack.pop();
}
}
return theStack.isEmpty();
}
}


54 changes: 52 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 Map<String, Integer> words;

public WC(String fileName) {
try {
Expand All @@ -17,7 +17,57 @@ public WC(String fileName) {
}
}

public static void main(String[] args) {
WC theWC = new WC("src/main/resources/someTextFile.txt");
theWC.readsStringFromfileAddstoMap();
theWC.print();
}

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

public void readsStringFromfileAddstoMap() {
words = new TreeMap<String, Integer>(Collections.<String>reverseOrder());
while (this.si.hasNext()) {
String[] wordsArray = this.si.next().split("[^\\w]+");
putsStringKeyInMap(wordsArray);
}

}

public void putsStringKeyInMap(String[] theWords) {
for (int i = 0; i < theWords.length; i++) {
String word = theWords[i];
increaseValueOfWord(word);
}
}

public void increaseValueOfWord(String theWord) {
int count = 1;
if (words.containsKey(theWord)) {
count = getCount(theWord);
words.put(theWord, count + 1);
} else {
words.put(theWord, count);
}
}

public int getCount(String theWord) {
return words.get(theWord);
}

// public void reverseOrderOfMap(){
// Map<String, Integer> newMap = new TreeMap(Collections.reverseOrder());
// newMap.putAll(words);
// }


public void print() {
// need to print in decending order.
for (Map.Entry<String, Integer> entry : words.entrySet())
System.out.println(entry.getValue() + " : " + entry.getKey());
}

}

3 changes: 3 additions & 0 deletions src/main/resources/someTextFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Yo My name is Joshua.
theNextPerson Yo,,,,, My name is Joshua.&*&(%^&*#$%^#$%@^$%&^&(%^*(%%$^#@$#$&^%$#(&*^%$#@99
the next word in the book will be the next person who tells the next thing that akes the word that goes
92 changes: 92 additions & 0 deletions src/test/java/io/zipcoder/AllOpensHaveEndsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package io.zipcoder;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.*;

public class AllOpensHaveEndsTest {
private ParenChecker theCheck;
@Before
public void setUp() throws Exception {
theCheck = new ParenChecker();
}

@Test
public void verifyAllOpensHaveEndsTest() {
String testString = "()";
boolean expect = true;
boolean actual = theCheck.verifyAllOpensHaveEnds(testString);

Assert.assertEquals(expect, actual);
}
@Test
public void verifyAllOpensHaveEnds1Test() {
String testString = "()(/////////////////////////////////)";
boolean expect = true;
boolean actual = theCheck.verifyAllOpensHaveEnds(testString);

Assert.assertEquals(expect, actual);
}

@Test
public void verifyAllOpensHaveEndsFalseTEst() {
String testString = "((";
boolean expect = false;
boolean actual = theCheck.verifyAllOpensHaveEnds(testString);

Assert.assertEquals(expect, actual);
}

@Test
public void verifyAllOpensHaveEndsFalse1Test() {
String testString = "((/////////////////////////////)";
boolean expect = false;
boolean actual = theCheck.verifyAllOpensHaveEnds(testString);

Assert.assertEquals(expect, actual);
}



@Test
public void verifyAllOpensHaveEndsFalse2Test() {
String testString = "((/////////////////////////////)}}}}}}}}}}}}}}}}}}}}}}}}{{{{{{{{{{{{{{{}}}";
boolean expect = false;
boolean actual = theCheck.verifyAllOpensHaveEnds(testString);

Assert.assertEquals(expect, actual);
}

@Test
public void verifyAllOpensHaveEndsTrueTest() {
String testString = "((/////////////////////////////))<>";
boolean expect = true;
boolean actual = theCheck.verifyAllOpensHaveEnds(testString);

Assert.assertEquals(expect, actual);
}

@Test
public void verifyAllOpensHaveEndsfalseTest() {
String testString = "((/////////////////////////////))<";
boolean expect = false;
boolean actual = theCheck.verifyAllOpensHaveEnds(testString);

Assert.assertEquals(expect, actual);
}
@Test
public void verifyAllOpensHaveEnds2Test() {
String testString = "sfsdfkjdsljfl";
boolean expect = true;
boolean actual = theCheck.verifyAllOpensHaveEnds(testString);

Assert.assertEquals(expect, actual);
}





}
73 changes: 73 additions & 0 deletions src/test/java/io/zipcoder/ParenCheckerTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,81 @@
package io.zipcoder;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Stack;

public class ParenCheckerTest {
private ParenChecker checker;
private String[] theString;
@Before
public void setUp() throws Exception {
checker = new ParenChecker();
theString = new String[]{"the (yo)", "the(,()", "the)", "the))", "the()()(", "the)()(", "()()()*()"};
}

@Test
public void parenClosedCheckerTest(){
String testString = theString[0];
boolean expected = true;
boolean actual = checker.verifyParens(testString);
Assert.assertEquals(expected, actual);
}

@Test
public void parenOneOpenCheckerTest(){
String testString = theString[1];
boolean expected = false;
boolean actual = checker.verifyParens(testString);
Assert.assertEquals(expected, actual);
}

@Test
public void parenOneClosedCheckerTest(){
String testString = theString[2];
boolean expected = false;
boolean actual = checker.verifyParens(testString);
Assert.assertEquals(expected, actual);
}

@Test
public void parenTwoClosedCheckerTest(){
String testString = theString[3];
boolean expected = false;
boolean actual = checker.verifyParens(testString);
Assert.assertEquals(expected, actual);
}

@Test
public void parenTwClosedOneOpenCheckerTest(){
String testString = theString[4];
boolean expected = false;
boolean actual = checker.verifyParens(testString);
Assert.assertEquals(expected, actual);
}

@Test
public void parenOneClosedTwoOpenCheckerTest(){
String testString = theString[5];
boolean expected = false;
boolean actual = checker.verifyParens(testString);
Assert.assertEquals(expected, actual);
}

@Test
public void parenFourClosedCheckerTest(){
String testString = theString[6];
boolean expected = true;
boolean actual = checker.verifyParens(testString);
Assert.assertEquals(expected, actual);
}








}