Skip to content

parts 1 & 2 complete #7

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 2 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
Binary file added .DS_Store
Binary file not shown.
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
Binary file added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
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.*;

public class ParenChecker {

/**
* Create a class with a method that verifies all parens () are paired.
* HINT: Use a stack.
**/
public boolean isPaired(String input) {
return hasPair(input, '(', ')');
}


/**
* Create a method that checks that all opening characters have a closing one.
* Characters include () {} [] <> "" ''
**/
public boolean hasClosing(String input) {
return(hasPair(input, '(', ')')
&& hasPair(input, '{', '}')
&& hasPair(input, '[', ']')
&& hasPair(input, '<', '>')
&& hasPair(input, '\"', '\"')
&& hasPair(input, '\'', '\''));
}


public boolean hasPair(String input, Character openingChar, Character closingChar) {
Stack<Character> stack = new Stack<Character>();

for(int i = 0; i < input.length(); i++) {
char currentChar = input.charAt(i);

// if currentChar is opening, add to stack
if (currentChar == openingChar) {
stack.push(currentChar);
continue;
}
// if currentChar is closing but there's no opening in stack, return false
if (currentChar == closingChar) {
if(stack.isEmpty()) {
return false;
} else if(stack.peek() == openingChar) {
stack.pop();
} else {
return false;
}
}
}
// if stack is empty then all parentheses were paired
return stack.empty() || (stack.size() % 2 == 0);
}
}
47 changes: 43 additions & 4 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package io.zipcoder;

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

public class WC {
private Iterator<String> si;
Expand All @@ -20,4 +18,45 @@ public WC(String fileName) {
public WC(Iterator<String> si) {
this.si = si;
}


public Map<String, Integer> getWordCount() {
Map<String, Integer> wordOccurences = new HashMap<>();
while (si.hasNext()) {
String currentLine = si.next();
String[] words = currentLine.split(" ");

for (String word : words) {
word = word.toLowerCase();
word = word.replaceAll("[^a-zA-Z']", "");

if (!word.trim().isEmpty()) {
if (wordOccurences.containsKey(word)) {
int count = wordOccurences.get(word);
wordOccurences.put(word, count + 1);
} else {
wordOccurences.put(word, 1);
}
}
}
}
return entriesSortedByValues(wordOccurences);
}


private static <K, V extends Comparable<? super V>> Map<K, V> entriesSortedByValues(Map<K, V> map) {
// ArrayList is initialized with all entries of passed in map
List<Map.Entry<K, V>> sortedEntries = new ArrayList<>(map.entrySet());

// used lambda expression to sort values in descending order and return result
sortedEntries.sort((e1, e2) -> e2.getValue().compareTo(e1.getValue()));

Map<K, V> result = new LinkedHashMap<>();

for (Map.Entry<K, V> entry : sortedEntries) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
}

Empty file.
3 changes: 3 additions & 0 deletions src/main/resources/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The quick brown fox jumped over the lazy dog.
The the the the.
the The. Quick Brown.
57 changes: 57 additions & 0 deletions src/test/java/io/zipcoder/ParenCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,61 @@

public class ParenCheckerTest {

@Test
public void isPairedTest1() {
ParenChecker parenChecker = new ParenChecker();
String str = "()()()()";

Assert.assertTrue(parenChecker.isPaired(str));
}

@Test
public void isPairedTest2() {
ParenChecker parenChecker = new ParenChecker();
String str = ")()()()()((";

Assert.assertFalse(parenChecker.isPaired(str));
}

@Test
public void isPairedTest3() {
ParenChecker parenChecker = new ParenChecker();
String str = ")((()";

Assert.assertFalse(parenChecker.isPaired(str));
}

@Test
public void hasClosingTest1() {
ParenChecker parenChecker = new ParenChecker();
String str = "<<>>(){}[]";

Assert.assertTrue(parenChecker.hasClosing(str));
}

@Test
public void hasClosingClosingTest2() {
ParenChecker parenChecker = new ParenChecker();
String str = "\"\"";

Assert.assertTrue(parenChecker.hasClosing(str));
}

@Test
public void hasClosingClosingTest3() {
ParenChecker parenChecker = new ParenChecker();
String str = "\'\'\'";

Assert.assertFalse(parenChecker.hasClosing(str));
}

@Test
public void hasPairTest1() {
ParenChecker parenChecker = new ParenChecker();
String str = "()()()()";

Assert.assertTrue(parenChecker.hasPair(str, '(', ')'));
}


}
43 changes: 41 additions & 2 deletions src/test/java/io/zipcoder/WCTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,48 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import javax.xml.bind.Element;
import java.util.*;

public class WCTest {

@Test
public void wcTest() {
WC wc = new WC("/Users/madelinebowe/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/test");

String expected = "{the=8, quick=2, brown=2, over=1, lazy=1, jumped=1, dog=1, fox=1}";
String actual = wc.getWordCount().toString();

Assert.assertEquals(expected, actual);
}

@Test
public void wcTest2() {
WC wc = new WC("/Users/madelinebowe/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/test");
Map<String ,Integer> test = wc.getWordCount();

Integer expected = 8;
Integer actual = test.get("the");

Assert.assertEquals(expected, actual);
}

@Test
public void wcTest3() {
WC wc = new WC("/Users/madelinebowe/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/test");
Map<String ,Integer> test = wc.getWordCount();

Integer expected = 2;
Integer actual = test.get("quick");

Assert.assertEquals(expected, actual);
}

@Test
public void wcTest4() {
WC wc = new WC("/Users/madelinebowe/Dev/CR-MesoLabs-Collections-EncapsulativeCharacters/src/main/resources/test");
Map<String ,Integer> test = wc.getWordCount();

Assert.assertFalse(test.containsKey("The"));
}
}