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 1 commit
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
Prev Previous commit
lab complete
  • Loading branch information
mbowe4 committed Mar 14, 2018
commit 66724fc2a07a6bd57bf8edac6ac97c55c1fbca62
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.
1 change: 0 additions & 1 deletion src/main/java/io/zipcoder/ParenChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public boolean hasPair(String input, Character openingChar, Character closingCha
stack.push(currentChar);
continue;
}

// if currentChar is closing but there's no opening in stack, return false
if (currentChar == closingChar) {
if(stack.isEmpty()) {
Expand Down
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.
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"));
}
}