Skip to content

Finished lab #16

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

import com.sun.xml.internal.fastinfoset.util.CharArray;

import java.util.Stack;

public class ParenChecker {
private Stack<Character> stackOfChars = new Stack();

public boolean parenChecker(String inputString) {
char[] arrayAsChar = inputString.toCharArray();
for (int i = 0; i < arrayAsChar.length; i++) {
characterChecker(arrayAsChar[i]);
}

if (stackOfChars.isEmpty()) {
return true;
} else {
return false;
}
}

private void characterChecker(char inputChar) {
switch (inputChar) {
case '(':
stackOfChars.push(inputChar);
break;
case '{':
stackOfChars.push(inputChar);
break;
case '[':
stackOfChars.push(inputChar);
break;
case '<':
stackOfChars.push(inputChar);
break;
case ')':
checkClosingParen(inputChar);
break;
case '}':
checkClosingBracket(inputChar);
break;
case ']':
checkClosingSquareBracket(inputChar);
break;
case '>':
checkClosingAngleBracket(inputChar);
break;
case '\"':
checkDoubleQuotes(inputChar);
break;
case '\'':
checkSingleQuotes(inputChar);
break;
}
}

private void checkSingleQuotes(char inputChar) {
if (!stackOfChars.empty()) {
if (stackOfChars.peek().equals('\'')) {
stackOfChars.pop();
} else {
stackOfChars.push(inputChar);
}
} else {
stackOfChars.push(inputChar);
}
}

private void checkDoubleQuotes(char inputChar) {
if (!stackOfChars.empty()) {
if (stackOfChars.peek().equals('\"')) {
stackOfChars.pop();
} else {
stackOfChars.push(inputChar);
}
} else {
stackOfChars.push(inputChar);
}
}

private void checkClosingAngleBracket(char inputChar) {
if (!stackOfChars.empty()) {
if (stackOfChars.peek().equals('<')) {
stackOfChars.pop();
} else {
stackOfChars.push(inputChar);
}
} else {
stackOfChars.push(inputChar);
}
}

private void checkClosingSquareBracket(char inputChar) {
if (!stackOfChars.empty()) {
if (stackOfChars.peek().equals('[')) {
stackOfChars.pop();
} else {
stackOfChars.push(inputChar);
}
} else {
stackOfChars.push(inputChar);
}
}

private void checkClosingBracket(char inputChar) {
if (!stackOfChars.empty()) {
if (stackOfChars.peek().equals('{')) {
stackOfChars.pop();
} else {
stackOfChars.push(inputChar);
}
} else {
stackOfChars.push(inputChar);
}
}

private void checkClosingParen(char inputChar) {
if (!stackOfChars.empty()) {
if (stackOfChars.peek().equals('(')) {
stackOfChars.pop();
} else {
stackOfChars.push(inputChar);
}
} else {
stackOfChars.push(inputChar);
}
}
}
55 changes: 53 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,12 @@

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> wordMap = new TreeMap();


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

public void wordCounter(){
splitIntoSingleWords();
sortMap();
}

private void splitIntoSingleWords() {
String[] words;
for(String fileLine = si.next(); true; fileLine = si.next()) {
words = fileLine.split(" ");
words = stripUnwantedChars(words);
placeWordIntoMap(words);
if (!si.hasNext()){
break;
}
}
}

private void placeWordIntoMap(String[] words) {
for (String word: words) {
if (!wordMap.containsKey(word.toLowerCase())){
wordMap.put(word.toLowerCase(), 1);
} else {
wordMap.put(word.toLowerCase(), wordMap.get(word)+ 1);
}
}
}

private void sortMap(){
ArrayList<Map.Entry<String, Integer>> listOfEntries = new ArrayList<Map.Entry<String, Integer>>(wordMap.entrySet());
listOfEntries.sort((entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue()));
printAllResults(listOfEntries);
}

private void printAllResults(ArrayList<Map.Entry<String, Integer>> inputList){
for (Map.Entry entry: inputList) {
System.out.println(entry);
}
}

public String[] stripUnwantedChars(String[] inputArray){
for (int i = 0; i < inputArray.length; i++) {
inputArray[i]= inputArray[i].replaceAll("[^a-zA-Z']", "");
}
return inputArray;
}

public TreeMap<String, Integer> getWordMap() {
return wordMap;
}
}
6 changes: 6 additions & 0 deletions src/main/resources/someTextFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
case test
call
apple apple apple test
test
case sweep
sweep!
33 changes: 32 additions & 1 deletion src/test/java/io/zipcoder/ParenCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,37 @@
import org.junit.Assert;
import org.junit.Test;

import javax.xml.ws.RequestWrapper;

public class ParenCheckerTest {

}
@Test
public void parenCheckerTest1(){
ParenChecker testChecker = new ParenChecker();
boolean actual = testChecker.parenChecker("()()<>\"\"[]{}\'\'");
Assert.assertTrue(actual);
}

@Test
public void parenCheckerTest2(){
ParenChecker testChecker = new ParenChecker();
boolean actual = testChecker.parenChecker("ufeuifbbifeiu><<()");
Assert.assertFalse(actual);
}

@Test
public void parenCheckerTest3(){
ParenChecker testChecker = new ParenChecker();
boolean actual = testChecker.parenChecker("\"(({}))\"");
Assert.assertTrue(actual);
}

@Test
public void parenCheckerTest4(){
ParenChecker testChecker = new ParenChecker();
boolean actual = testChecker.parenChecker("fsf(kfbf{}jnje)<>\"dsdjhsjdhj\"");
Assert.assertTrue(actual);
}

}

51 changes: 50 additions & 1 deletion src/test/java/io/zipcoder/WCTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,60 @@
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 {
ArrayList<String> stringList = new ArrayList<String>();

}
@Before
public void setUp(){
stringList.add("test");
stringList.add("apple");
stringList.add("test");
stringList.add("case");
stringList.add("case");
stringList.add("case");
}

@Test
public void wordCheckerTest1(){
WC wcTest = new WC(stringList.iterator());
wcTest.wordCounter();
int actual = wcTest.getWordMap().get("apple");
int expected = 1;
Assert.assertEquals(actual, expected);
}

@Test
public void wordCheckerTest2(){
WC wcTest = new WC(stringList.iterator());
wcTest.wordCounter();
int actual = wcTest.getWordMap().get("case");
int expected = 3;
Assert.assertEquals(actual, expected);
}

@Test
public void wordCheckerTest3(){
ArrayList<String> testArray2 = new ArrayList<String>();
testArray2.add("apple test test case case case");
WC wcTest = new WC(testArray2.iterator());
wcTest.wordCounter();
int actual = wcTest.getWordMap().get("case");
int expected = 3;
Assert.assertEquals(actual, expected);
}

@Test
public void wordCheckerTest4(){
WC wcTest = new WC(WC.class.getResource("/someTextFile.txt").getFile());
wcTest.wordCounter();
int actual = wcTest.getWordMap().get("test");
int expected = 3;
Assert.assertEquals(actual,expected);
}
}