Skip to content

Part 1 and 2 #10

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

import java.util.Stack;

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

public ParenChecker() {
}

public boolean parenChecker(String s){
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(c == '(' ){
stack.push(c);
}else if(c == ')'){
if(stack.isEmpty() || stack.pop() != '('){
return false;
}
}
}
return stack.isEmpty();
}
public boolean pairChecker(String s){
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
if(c == '(' || c == '{' || c == '[' || c == '<' || c == '\'' || c == '\"') {
stack.push(c);
}else if(c == ')') {
if (stack.isEmpty() || stack.pop() != '(') return false;
}
else if(c == '}'){
if(stack.isEmpty() || stack.pop() != '{') return false;
}
else if(c == ']'){
if(stack.isEmpty() || stack.pop() != '[') return false;
}
else if(c == '>'){
if(stack.isEmpty() || stack.pop() != '<') return false;
}
else if(c == '\''){
if(stack.isEmpty() || stack.pop() != '\'') return false;
}
else if(c == '\"'){
if(stack.isEmpty() || stack.pop() != '\"') return false;
}
}
return stack.isEmpty();
}
}
45 changes: 42 additions & 3 deletions src/main/java/io/zipcoder/WC.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

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

public class WC {
private Iterator<String> si;

Map<String, Integer> wordMap = new HashMap<>();
ArrayList<String> words = new ArrayList<>();

public WC(String fileName) {
try {
this.si = new Scanner(new FileReader(fileName));
this.si = new Scanner(new FileReader(fileName)).useDelimiter("[^A-Za-z]+");
} catch (FileNotFoundException e) {
System.out.println(fileName + " Does Not Exist");
System.exit(-1);
Expand All @@ -20,4 +23,40 @@ public WC(String fileName) {
public WC(Iterator<String> si) {
this.si = si;
}

public void splitFile() {
String eachWord;
while (si.hasNext()) {
eachWord = si.next();
words.add(eachWord.toLowerCase());
}
}

public Map<String, Integer> wordCount() {
for (String s : words) {
Integer freq = wordMap.get(s);
wordMap.put(s, (freq == null) ? 1 : freq + 1);
}
return wordMap;
}

public Map<String, Integer> sortedMap() {
Map<String, Integer>
myMap = wordMap
.entrySet()
.stream()
.sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));

return myMap;
}


public String getSi() {
while (si.hasNext()) {
String currentString = si.next();
return currentString;
}
return null;
}
}
1 change: 1 addition & 0 deletions src/main/resources/SomeOtherTextFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hiya! This this is is a test.
51 changes: 51 additions & 0 deletions src/main/resources/someTextFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
But this murder--was it to dog him all his life? Was he always to be
burdened by his past? Was he really to confess? Never. There was
only one bit of evidence left against him. The picture itself--that
was evidence. He would destroy it. Why had he kept it so long? Once
it had given him pleasure to watch it changing and growing old. Of
late he had felt no such pleasure. It had kept him awake at night.
When he had been away, he had been filled with terror lest other eyes
should look upon it. It had brought melancholy across his passions.
Its mere memory had marred many moments of joy. It had been like
conscience to him. Yes, it had been conscience. He would destroy it.

He looked round and saw the knife that had stabbed Basil Hallward. He
had cleaned it many times, till there was no stain left upon it. It
was bright, and glistened. As it had killed the painter, so it would
kill the painter's work, and all that that meant. It would kill the
past, and when that was dead, he would be free. It would kill this
monstrous soul-life, and without its hideous warnings, he would be at
peace. He seized the thing, and stabbed the picture with it.

There was a cry heard, and a crash. The cry was so horrible in its
agony that the frightened servants woke and crept out of their rooms.
Two gentlemen, who were passing in the square below, stopped and looked
up at the great house. They walked on till they met a policeman and
brought him back. The man rang the bell several times, but there was
no answer. Except for a light in one of the top windows, the house was
all dark. After a time, he went away and stood in an adjoining portico
and watched.

"Whose house is that, Constable?" asked the elder of the two gentlemen.

"Mr. Dorian Gray's, sir," answered the policeman.

They looked at each other, as they walked away, and sneered. One of
them was Sir Henry Ashton's uncle.

Inside, in the servants' part of the house, the half-clad domestics
were talking in low whispers to each other. Old Mrs. Leaf was crying
and wringing her hands. Francis was as pale as death.

After about a quarter of an hour, he got the coachman and one of the
footmen and crept upstairs. They knocked, but there was no reply.
They called out. Everything was still. Finally, after vainly trying
to force the door, they got on the roof and dropped down on to the
balcony. The windows yielded easily--their bolts were old.

When they entered, they found hanging upon the wall a splendid portrait
of their master as they had last seen him, in all the wonder of his
exquisite youth and beauty. Lying on the floor was a dead man, in
evening dress, with a knife in his heart. He was withered, wrinkled,
and loathsome of visage. It was not till they had examined the rings
that they recognized who it was.
50 changes: 50 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,54 @@

public class ParenCheckerTest {

@Test
public void parenCheckerTest(){
//Given
ParenChecker parenChecker = new ParenChecker();
String s = ")()(";
//When
boolean allPair = parenChecker.parenChecker(s);
//Then
Assert.assertFalse(allPair);
}
@Test
public void parenCheckerTest2(){
//Given
ParenChecker parenChecker = new ParenChecker();
String s = "()()";
//When
boolean hasAllPair = parenChecker.parenChecker(s);
//Then
Assert.assertTrue(hasAllPair);
}
@Test
public void pairCheckerTest(){
//Given
ParenChecker parenChecker = new ParenChecker();
String s = "{true}";
//When
boolean hasPair = parenChecker.pairChecker(s);
//Then
Assert.assertTrue(hasPair);
}
@Test
public void pairCheckTest2(){
//Given
ParenChecker parenChecker = new ParenChecker();
String s = "<False pacman mouth<";
//When
boolean hasPair = parenChecker.pairChecker(s);
//Then
Assert.assertFalse(hasPair);
}
@Test
public void pairCheckerTest3(){
//Given
ParenChecker parenChecker = new ParenChecker();
String s = "[size]";
//When
boolean hasPair = parenChecker.pairChecker(s);
//Then
Assert.assertTrue(hasPair);
}
}
91 changes: 90 additions & 1 deletion src/test/java/io/zipcoder/WCTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,100 @@
package io.zipcoder;

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class WCTest {

private WC wc;

@Before
public void setup(){
wc = new WC(WC.class.getResource("/someOtherTextFile.txt").getFile());
wc.getSi();
}

@Test
public void splitFileTest(){
//Given
wc = new WC(WC.class.getResource("/someOtherTextFile.txt").getFile());
ArrayList<String> expected = new ArrayList<>();
expected.add("hiya");
expected.add("this");
expected.add("this");
expected.add("is");
expected.add("is");
expected.add("a");
expected.add("test");
//When
wc.splitFile();
//Then
Assert.assertEquals(expected, wc.words);

}
@Test
public void wordCountTest(){
//Given
wc = new WC(WC.class.getResource("/someOtherTextFile.txt").getFile());
ArrayList<String> expected1 = new ArrayList<>();
expected1.add("hiya");
expected1.add("this");
expected1.add("this");
expected1.add("is");
expected1.add("is");
expected1.add("a");
expected1.add("test");

Map<String, Integer> expected = new HashMap<>();
expected.put("hiya", 1);
expected.put("this", 2);
expected.put("is", 2);
expected.put("a", 1);
expected.put("test", 1);
//When
wc.splitFile();
wc.wordCount();
//Then
Assert.assertEquals(expected, wc.wordMap);

}
@Test
public void sortedMapTest(){
//Given
wc = new WC(WC.class.getResource("/someOtherTextFile.txt").getFile());
ArrayList<String> expected1 = new ArrayList<>();
expected1.add("hiya");
expected1.add("this");
expected1.add("this");
expected1.add("is");
expected1.add("is");
expected1.add("a");
expected1.add("test");

Map<String, Integer> expected2 = new HashMap<>();
expected2.put("hiya", 1);
expected2.put("this", 2);
expected2.put("is", 2);
expected2.put("a", 1);
expected2.put("test", 1);

Map<String, Integer> expected = new LinkedHashMap<>();
expected.put("hiya", 1);
expected.put("this", 2);
expected.put("is", 2);
expected.put("a", 1);
expected.put("test", 1);

//When
wc.splitFile();
wc.wordCount();
wc.sortedMap();
//Then
Assert.assertEquals(expected, wc.sortedMap());
}
}