Skip to content

Done #27

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

Done #27

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
49 changes: 49 additions & 0 deletions src/main/java/io/zipcoder/PairedChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.zipcoder;
import java.util.EmptyStackException;
import java.util.Stack;

public class PairedChecker {

/**
* Check to see if opening and closing characters are balanced in String input
* @param opening token
* @param closing token
* @param input string to scan
* @return true if all opening tokens match all closing tokens
* Catch exception and return false if try to pop off an empty stack
*/
public boolean check(Character opening, Character closing, String input) {
Stack<Character> stack = new Stack<Character>();
for (int i = 0; i < input.length(); i++) {
Character value = input.charAt(i);
if (value.equals(opening)) {
stack.push(value);
} else if (value.equals(closing)) {
try {
stack.pop();
}
catch (EmptyStackException ex) {
return false;
}
}
}

return stack.isEmpty();
}

/**
* Specific for opening and closing chars that are the same char, ie "" and ''
* @param quote token used for both opening and closing
* @param input string to scan
* @return true if the tokens are evenly distributed in the string
*/
public boolean checkSame(Character quote, String input) {
int count = 0;
for(int i = 0; i < input.length(); i++){
if( quote.equals(input.charAt(i))){
count++;
}
}
return count%2 == 0;
}
}
28 changes: 27 additions & 1 deletion src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,30 @@
package io.zipcoder;

import java.util.Stack;

public class ParenChecker {
}

private PairedChecker checker;

public ParenChecker() {
this.checker = new PairedChecker();
}


/**
* Check to see if open and closing paren's are balanced
*
* @param inputString string that is iterated over
* @return true if all parenthesis in string are matched
*/
public boolean check(String inputString) {
PairedChecker checker = new PairedChecker();
return checker.check('(', ')', inputString) &&
checker.check('{', '}', inputString) &&
checker.check('[', ']', inputString) &&
checker.check('<', '>', inputString) &&
checker.checkSame('"', inputString) &&
checker.checkSame('\'', inputString);
}

}
52 changes: 46 additions & 6 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,62 @@

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 Iterator<String> stringIterator;
private TreeMap<String, Integer> map;

public WC(String fileName) {
try {
this.si = new Scanner(new FileReader(fileName));
this.stringIterator = new Scanner(new FileReader(fileName));
this.map = new TreeMap<String, Integer>();
this.parseAndCountWords();
} catch (FileNotFoundException e) {
System.out.println(fileName + " Does Not Exist");
System.exit(-1);
}
}

public WC(Iterator<String> si) {
this.si = si;
private void parseAndCountWords() {
while (this.stringIterator.hasNext()) {
String s = this.stringIterator.next().toLowerCase().replaceAll("[^\\w]", "");
Integer count = this.map.getOrDefault(s, 0);
this.map.put(s, count+1);
}
}


public String toString() {
ArrayList<Map.Entry<String, Integer>> sortedValues = new ArrayList<>();
this.map.entrySet()
.stream()
.sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
.forEach(sortedValues::add);
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, Integer> entry : sortedValues) {
sb.append(entry.getKey()).append(" = ").append(entry.getValue()).append("\n");
}

return sb.toString();
}

public void print() {
System.out.println(this.toString());
}

public static void main(String[] args) {
//WC wc = new WC("/Users/kaitrinahigh/Downloads/47366-0.txt");
WC testFile = new WC("/Users/kaitrinahigh/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/someTextFile.txt");
testFile.print();

}
}








7 changes: 7 additions & 0 deletions src/main/resources/someTextFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Strive for progress, not perfection.
The expert in everything was once a beginner.
Start where you are. Use what you have. Do what you can.




67 changes: 65 additions & 2 deletions src/test/java/io/zipcoder/ParenCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,70 @@

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

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class ParenCheckerTest {

}
private ParenChecker checker;

@Before
public void setup() {
this.checker = new ParenChecker();
}

@Test //happyPath
public void checkBaseCase() {
boolean result = this.checker.check("()");
Assert.assertTrue(result);
}

@Test
public void checkBraceBaseCase() {
boolean result = this.checker.check("[[<'cats'>''''][{}][()]]");
Assert.assertTrue(result);
}

@Test //happy path even quote
public void checkDoubleQuoteBaseCase() {
boolean result = this.checker.check("\"\"\"\"\"\"");
Assert.assertTrue(result);
}

@Test //happy path even single quote
public void checkSingleQuoteBaseCase() {
boolean result = this.checker.check("\'\'\'\'\'\'");
Assert.assertTrue(result);
}

@Test //happy path even mixed quote
public void checkMixedQuoteBaseCase() {
boolean result = this.checker.check("\"\'\'\'\'\'\'\"\"\"\"\"");
Assert.assertTrue(result);
}

@Test
public void checkExtraBraceBaseCase() {
boolean result = this.checker.check("[[<cats>>][{}][()]]");
Assert.assertFalse(result);
}
@Test //left char left paren, final check checks to see if stack is empty
public void checkFailingBaseCase() {
boolean result = this.checker.check("()(");
Assert.assertFalse(result);
}

@Test //tried to pop a second time on an emptyStack
public void checkExceptionCaughtAndThrowsFalseBaseCase() {
boolean result = this.checker.check("())");
Assert.assertFalse(result);
}

@Test //ignore other char checks
public void checkOtherCharsIgnoredBaseCase() {
boolean result = this.checker.check("(jklm86&)");
Assert.assertTrue(result);
}
}


32 changes: 31 additions & 1 deletion src/test/java/io/zipcoder/WCTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
package io.zipcoder;

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

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

public class WCTest {

}
@Test
public void testWordCountBaseCase() {
WC testFile = new WC("/Users/kaitrinahigh/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/someTextFile.txt");
String actual = testFile.toString();
String expected = "you = 3\n" +
"what = 2\n" +
"a = 1\n" +
"are = 1\n" +
"beginner = 1\n" +
"can = 1\n" +
"do = 1\n" +
"everything = 1\n" +
"expert = 1\n" +
"for = 1\n" +
"have = 1\n" +
"in = 1\n" +
"not = 1\n" +
"once = 1\n" +
"perfection = 1\n" +
"progress = 1\n" +
"start = 1\n" +
"strive = 1\n" +
"the = 1\n" +
"use = 1\n" +
"was = 1\n" +
"where = 1\n";
Assert.assertEquals(expected, actual);
}
}