Skip to content

Part 1 & 2 Done #26

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

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

import java.util.ArrayList;
import java.util.Stack;

public class ParenChecker {
}
public ParenChecker() {
}

public boolean braceChecker(String str) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < str.length(); i++) {
char openCharacter = str.charAt(i);
if (openCharacter == '[' || openCharacter == '(' || openCharacter == '{' || openCharacter == '<') {
stack.push(openCharacter);
} else if (openCharacter == ']') {
if (stack.isEmpty() || stack.pop() != '[') {
return false;
}
} else if (openCharacter == ')') {
if (stack.isEmpty() || stack.pop() != '(') {
return false;
}
} else if (openCharacter == '}') {
if (stack.isEmpty() || stack.pop() != '{') {
return false;
}
} else if (openCharacter == '>') {
if (stack.isEmpty() || stack.pop() != '<') {
return false;
}
} else if (openCharacter == '\"') {
if (stack.isEmpty() || stack.peek() != '\"') {
stack.push(openCharacter);
} else {
stack.pop();
}
} else if (openCharacter == '\'') {
if (stack.isEmpty() || stack.peek() != '\'') {
stack.push(openCharacter);
} else {
stack.pop();
}
}
}
return stack.isEmpty();
}
}
51 changes: 42 additions & 9 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,55 @@

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Iterator;
import java.util.Scanner;
import java.util.*;
import java.util.TreeMap;

/**
@TODO
*@1.)Create a parenthesis verifier method, that can (check if all parenthesis are ('paired')
* @2.)Create a method that checks all opening characters have a closing one
* - These Characters () {} [] <> "" ''
* This program should be able to:
* 1 - count all the words in a file
* 2 - Print out all the words
* 3 - Print the word counts in descending order
*/

public class WC {
private Iterator<String> si;
private TreeMap<String, Integer> book;

public static void main(String[] args) {
WC wordCounts = new WC("/Users/kieranthomas/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/aGoodBookFile.txt");
wordCounts.descendAndPrintText();
}

public WC(String fileName) {
try {
this.si = new Scanner(new FileReader(fileName));
public WC(String fileName) { //nullary constructor
try { //As soon as the scanner receives my book(map) it will get
this.book = new TreeMap<>();
this.si = new Scanner(new FileReader(fileName)).useDelimiter("[^a-zA-Z]+");
this.metaCharRemover();
} catch (FileNotFoundException e) {
System.out.println(fileName + " Does Not Exist");
System.out.println(fileName + "Does Not Exist");
System.exit(-1);
}
}

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

public void metaCharRemover() {
while (si.hasNext()) { //while the iterator has the next line do this stuff below it
String word = si.next().toLowerCase().replaceAll("[\"^${}().+&~!@#%*]", ""); //change all characters within file to lower case and replace all metacharacters with nothing so that only the words are counted when its time to count
Integer wordCount = book.getOrDefault(word, 0); //set my default value for word count to 0
book.put(word, (wordCount + 1)); //insert my word and word count into my map
}
}

private void descendAndPrintText() { //stream is pulling out JUST my words in sequential order first before sorting them in descending order, reverse is so that the highest number will print first and get lower accordingly
String newBook = "";
newBook += book.entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue().reversed()) + "\n";
System.out.println(newBook);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/aGoodBookFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Mary had a little lamb. Mary is 24 years old.
156 changes: 156 additions & 0 deletions src/test/java/io/zipcoder/ParenCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,161 @@
import org.junit.Test;

public class ParenCheckerTest {
ParenChecker parenChecker = new ParenChecker();

@Test
public void isPaired1Test(){
//Given
String strToCheck = "(UltraInstinct)";
Boolean expected = true;

//When
boolean actual = parenChecker.braceChecker(strToCheck);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void isNotPaired1Test(){
//Given
String strToCheck = "(UltraInstinct";
Boolean expected = false;

//When
boolean actual = parenChecker.braceChecker(strToCheck);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void isPaired2Test(){
//Given
String strToCheck = "{}UltraInstinct";
Boolean expected = true;

//When
boolean actual = parenChecker.braceChecker(strToCheck);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void isNotPaired2Test(){
//Given
String strToCheck = "UltraInstinct}";
Boolean expected = false;

//When
boolean actual = parenChecker.braceChecker(strToCheck);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void isPaired3Test(){
//Given
String strToCheck = "[Ultra]Instinct";
Boolean expected = true;

//When
boolean actual = parenChecker.braceChecker(strToCheck);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void isNotPaired3Test(){
//Given
String strToCheck = "UltraInstin]ct";
Boolean expected = false;

//When
boolean actual = parenChecker.braceChecker(strToCheck);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void isPaired4Test(){
//Given
String strToCheck = "Ultra<>Instinct";
Boolean expected = true;

//When
boolean actual = parenChecker.braceChecker(strToCheck);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void isNotPaired4Test(){
//Given
String strToCheck = "UltraInstinc>>t";
Boolean expected = false;

//When
boolean actual = parenChecker.braceChecker(strToCheck);

//Then
Assert.assertEquals(expected,actual);
}
@Test
public void isPaired5Test(){
//Given
String strToCheck = "Ultra\"Instinc\"t";
Boolean expected = true;

//When
boolean actual = parenChecker.braceChecker(strToCheck);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void isNotPaired5Test(){
//Given
String strToCheck = "Ultra\"Instinct";
Boolean expected = false;

//When
boolean actual = parenChecker.braceChecker(strToCheck);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void isPaired6Test(){
//Given
String strToCheck = "Ult\'raIn\'stinct";
Boolean expected = true;

//When
boolean actual = parenChecker.braceChecker(strToCheck);

//Then
Assert.assertEquals(expected,actual);
}

@Test
public void isNotPaired6Test(){
//Given
String strToCheck = "Ultra\'Inst\'inct\'";
Boolean expected = false;

//When
boolean actual = parenChecker.braceChecker(strToCheck);

//Then
Assert.assertEquals(expected,actual);
}

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

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

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

public class WCTest {

}
//package io.zipcoder;
//
//import org.junit.Assert;
//import org.junit.Test;
//
//import java.util.ArrayList;
//import java.util.Arrays;
//
//public class WCTest {
//
// @Test
// public void test() {
// WC wc = new WC("/Users/kieranthomas/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/aGoodBookFile.txt");
//
// String exptected = "";
// String actual = wc.descendAndPrintText();
//
// Assert.assertEquals(exptected, actual);
//
// }
//}