浏览代码

completed the Encapsulative Charecters

thulasi 6 年前
父节点
当前提交
2e35c37228

二进制
.DS_Store 查看文件


二进制
src/.DS_Store 查看文件


二进制
src/main/.DS_Store 查看文件


二进制
src/main/java/.DS_Store 查看文件


二进制
src/main/java/io/.DS_Store 查看文件


+ 47
- 0
src/main/java/io/zipcoder/ParenChecker.java 查看文件

@@ -1,4 +1,51 @@
1 1
 package io.zipcoder;
2 2
 
3
+import java.util.List;
4
+import java.util.Stack;
5
+
3 6
 public class ParenChecker {
7
+
8
+    static Stack<String> stack = new Stack<String>();
9
+
10
+    static boolean areParenthesisBalanced(String s)
11
+    {
12
+
13
+        boolean check = false;
14
+        String a[] = s.split("");
15
+
16
+        for(String c: a){
17
+            if(c.equals("(") || c.equals("{") || c.equals("[") || c.equals("<")) {
18
+                stack.add(c);
19
+
20
+            } else if(c.equals(")") || c.equals("}") || c.equals("]") || c.equals(">")){
21
+                String last = stack.peek();
22
+                if(stack.empty()) {
23
+                    return check;
24
+
25
+                } else if((last.equals("(") && c.equals(")")) ||
26
+                    (last.equals("{") && c.equals("}")) ||
27
+                     (last.equals("[") && c.equals("]")) ||
28
+                    (last.equals("<") && c.equals(">"))) {
29
+                    stack.pop();
30
+                    check = true;
31
+
32
+                } else {
33
+                    return false;
34
+                }
35
+            }
36
+        }
37
+
38
+        return check;
39
+    }
40
+
41
+   /* public static void main(String[] args) {
42
+        String s = "(x+y)";
43
+        String arr[] = s.split("");
44
+        System.out.println(arr);
45
+    }*/
46
+
4 47
 }
48
+
49
+
50
+
51
+

+ 52
- 4
src/main/java/io/zipcoder/WC.java 查看文件

@@ -2,22 +2,70 @@ package io.zipcoder;
2 2
 
3 3
 import java.io.FileNotFoundException;
4 4
 import java.io.FileReader;
5
-import java.util.Iterator;
6
-import java.util.Scanner;
5
+import java.util.*;
6
+
7
+import static java.util.Map.Entry.comparingByValue;
8
+import static java.util.stream.Collectors.toList;
9
+import static java.util.stream.Collectors.toMap;
7 10
 
8 11
 public class WC {
9 12
     private Iterator<String> si;
10 13
 
11
-    public WC(String fileName) {
14
+   /* public WC(String fileName) {
12 15
         try {
13 16
             this.si = new Scanner(new FileReader(fileName));
14 17
         } catch (FileNotFoundException e) {
15 18
             System.out.println(fileName + " Does Not Exist");
16 19
             System.exit(-1);
17 20
         }
21
+    }*/
22
+
23
+    public WC(String fileName) {
24
+        try {
25
+            Scanner scanner = new Scanner(new FileReader(fileName));
26
+            scanner.useDelimiter("[\\p{Punct}\\s]+");
27
+            this.si = scanner;
28
+        } catch (FileNotFoundException e) {
29
+            System.out.println(fileName + " Does Not Exist");
30
+            System.exit(-1);
31
+        }
18 32
     }
19 33
 
20
-    public WC(Iterator<String> si) {
34
+    /*public WC(Iterator<String> si) {
21 35
         this.si = si;
36
+    }*/
37
+
38
+    HashMap<String, Integer> map = new HashMap<String, Integer>();
39
+
40
+    public void addWords(){
41
+
42
+        while(si.hasNext()){
43
+            String word = si.next().toLowerCase();
44
+            if(!map.containsKey(word))
45
+                map.put(word, 1);
46
+            else{
47
+                map.put(word, map.get(word) + 1);
48
+            }
49
+        }
50
+    }
51
+
52
+    public List<Map.Entry<String, Integer>> sortTheMap() {
53
+        List<Map.Entry<String, Integer>> sorted = map
54
+                .entrySet()
55
+                .stream()
56
+                .sorted(comparingByValue((e1, e2) -> e2 - e1))
57
+                .collect(toList());
58
+//                .collect(toMap(e -> e.getKey(), e -> e.getValue(), (e1, e2) -> e2, LinkedHashMap::new));
59
+
60
+//        map = sorted;
61
+        return sorted;
62
+    }
63
+
64
+
65
+
66
+    public HashMap<String, Integer> getMap() {
67
+        List<Integer> list = new LinkedList<>();
68
+
69
+        return map;
22 70
     }
23 71
 }

+ 150
- 0
src/main/resources/book.txt 查看文件

@@ -0,0 +1,150 @@
1
+A Great Change
2
+
3
+
4
+"There's no doubt about it, we really must economize somehow!" sighed
5
+Mrs. Woodward helplessly, with her housekeeping book in one hand, and
6
+her bank pass-book in the other, and an array of bills spread out on the
7
+table in front of her. "Children, do you hear what I say? The war will
8
+make a great difference to our income, and we can't--simply _can't_--go
9
+on living in exactly the old way. The sooner we all realize it the
10
+better. I wish I knew where to begin."
11
+
12
+"Might knock off going to church, and save the money we give in
13
+collections!" suggested Percy flippantly. "It must tot up to quite a
14
+decent sum in the course of a year, not to mention pew rent!"
15
+
16
+His mother cast a reproachful glance at him.
17
+
18
+"Now, Percy, _do_ be serious for once! You and Winona are quite old
19
+enough to understand business matters. I must discuss them with
20
+somebody. As I said before, we shall really have to economize somehow,
21
+and the question is where to begin."
22
+
23
+"I saw some hints in a magazine the other day," volunteered Winona,
24
+hunting among a pile of papers, and fishing up a copy of _The
25
+Housewife's Journal_. "Here you are! There's a whole article on War
26
+Economies. It says you can halve your expenses if you only try. It gives
27
+ten different recipes. Number One, Dispense with Servants. Oh, goody! I
28
+don't know how the house would get along without Maggie and Mary! Isn't
29
+that rather stiff?"
30
+
31
+"It's impossible to be thought of for a moment! I should never dream of
32
+dismissing maids who have lived with me for years. I've read that
33
+article, and it may be practicable for other people, but certainly not
34
+for us. Oh, dear! Some of my friends recommend me to remove to the town,
35
+and others say 'Stay where you are, and keep poultry!'"
36
+
37
+"We can't leave Highfield! We were all born here!" objected Winona
38
+decisively.
39
+
40
+"And we tried keeping hens some time ago," said Percy. "They laid on an
41
+average three-quarters of an egg a year each, as far as I remember."
42
+
43
+"I'm afraid we didn't know how to manage them," replied Mrs. Woodward
44
+fretfully. "Percy, leave those papers alone! I didn't tell you to turn
45
+them over. You're mixing them all up, tiresome boy! Don't touch them
46
+again! It's no use trying to discuss business with you children! I shall
47
+write and consult Aunt Harriet. Go away, both of you, now! I want to
48
+have a quiet half-hour."
49
+
50
+Aunt Harriet stood to the Woodward family somewhat in the light of a
51
+Delphic oracle. To apply to her was always the very last resource.
52
+Matters must have reached a crisis, Winona thought, if they were
53
+obliged to appeal to Aunt Harriet's judgment. She followed Percy into
54
+the garden with a sober look on her face.
55
+
56
+"You don't think mother would really leave Highfield?" she asked her
57
+brother anxiously.
58
+
59
+"Bunkum!" replied that light-hearted youth. "We always have more or less
60
+of a fuss when my school bills come in. It'll soon fizzle out again!
61
+Don't you fret yourself. Things will jog on as they always have jogged
62
+on. There'll be nothing done, you'll see. Come on and bowl for me,
63
+that's a chubby one!"
64
+
65
+"But this time mother really seemed to be in earnest," said Winona
66
+meditatively, as she helped to put up the stumps.
67
+
68
+Mrs. Woodward had been left a widow three years before this story opens.
69
+She was a fair, fragile little woman, still pretty, and pathetically
70
+helpless. She had been accustomed to lean upon her husband, and now, for
71
+lack of firmer support, she leaned upon Winona. Winona was young to act
72
+as prop, and though it flattered her sense of importance, it had put a
73
+row of wrinkles on her girlish forehead. At fifteen she seemed much
74
+older than Percy at sixteen. No one ever dreamt of taking Percy
75
+seriously; he was one of those jolly, easy-going, happy-go-lucky,
76
+unreliable people who saunter through life with no other aim than to
77
+amuse themselves at all costs. To depend upon him was like trusting to a
78
+boat without a bottom. Though nominally the eldest, he had little more
79
+sense of responsibility than Ernie, the youngest. It was Winona who
80
+shouldered the family burdens.
81
+
82
+The Woodwards had always lived at Highfield, and in their opinion it was
83
+the most desirable residence in the whole of Rytonshire. The house was
84
+old enough to be picturesque, but modern enough for comfort. Its quaint
85
+gables, mullioned windows and Cromwellian porch were the joy of
86
+photographers, while the old-fashioned hall, when the big log fire was
87
+lighted, would be hard to beat for coziness. The schoolroom, on the
88
+ground floor, had a separate side entrance on to the lawn, leading
89
+through a small ante-room where boots and coats and cricket bats and
90
+tennis rackets could be kept; the drawing-room had a luxurious ingle
91
+nook with cushioned seats, and all the bedrooms but two had a southern
92
+aspect. As for the big rambling garden, it was full of delightful
93
+old-world flowers that came up year after year: daffodils and violets
94
+and snow-flakes, and clumps of pinks, and orange lilies and Canterbury
95
+bells, and tall Michaelmas daisies, and ribbon grass and royal Osmunda
96
+fern, the sort of flowers that people used to pick in days gone by, put
97
+a paper frill round, and call a nosegay or a posy. There was a lawn for
98
+tennis and cricket, a pond planted with irises and bulrushes, and a wild
99
+corner where crocuses and coltsfoot and golden aconite came up as they
100
+liked in the spring time.
101
+
102
+Winona loved this garden with somewhat the same attachment that a French
103
+peasant bears for the soil upon which he has been reared. She rejoiced
104
+in every yard of it. To go away and resign it to others would be
105
+tragedy unspeakable. The fear that Aunt Harriet might recommend the
106
+family to leave Highfield was sufficient to darken her horizon
107
+indefinitely. That her mother had written to consult the oracle she was
108
+well aware, for she had been sent to post the letter. She had an
109
+instinctive apprehension that the answer would prove a turning-point in
110
+her career.
111
+
112
+For a day or two everything went on as usual. Mrs. Woodward did not
113
+again allude to her difficulties, Percy had conveniently forgotten them,
114
+and the younger children were not aware of their existence. Winona lived
115
+with a black spot dancing before her mental eyes. It was continually
116
+rising up and blotting out the sunshine. On the fourth morning appeared
117
+a letter addressed in an old-fashioned slanting handwriting, and bearing
118
+the Seaton post mark. Mrs. Woodward read it in silence, and left her
119
+toast unfinished. Aunt Harriet's communications generally upset her for
120
+the day.
121
+
122
+"Come here, Winona," she said agitatedly, after breakfast. "Oh, dear, I
123
+wish I knew what to do! It's so very unexpected, but of course it would
124
+be a splendid thing for you. If only I could consult somebody! I suppose
125
+girls nowadays will have to learn to support themselves, and the war
126
+will alter everything, but I'd always meant you to stop at home and look
127
+after the little ones for me, and it's very--"
128
+
129
+"What does Aunt Harriet say, mother?" interrupted Winona, with a catch
130
+in her throat.
131
+
132
+"She says a great deal, and I dare say she's right. Oh, this terrible
133
+war! Things were so different when I was a girl! You might as well read
134
+the letter for yourself, as it concerns you. I always think she's hard
135
+on Percy, poor lad! I was afraid the children were too noisy the last
136
+time she was here, but they wouldn't keep quiet. I'm sure I try to do my
137
+best all round, and you know, Winona, how I said Aunt Harriet--"
138
+
139
+But Winona was already devouring the letter.
140
+
141
+
142
+                                        "10 Abbey Close,
143
+
144
+                                                  "Seaton,
145
+
146
+                                                    "August 26th.
147
+
148
+
149
+
150
+

+ 1
- 0
src/main/resources/someTextFile.txt 查看文件

@@ -0,0 +1 @@
1
+sreeram Jonnageddala, sreya jonnageddala

+ 30
- 0
src/test/java/io/zipcoder/ParenCheckerTest.java 查看文件

@@ -5,4 +5,34 @@ import org.junit.Test;
5 5
 
6 6
 public class ParenCheckerTest {
7 7
 
8
+    @Test
9
+    public void testParethesis1(){
10
+        Assert.assertTrue(ParenChecker.areParenthesisBalanced("(x+y)"));
11
+    }
12
+
13
+    @Test
14
+    public void testParethesis2(){
15
+        Assert.assertTrue(ParenChecker.areParenthesisBalanced("{a * (x+y)}"));
16
+    }
17
+
18
+    @Test
19
+    public void testParethesis3(){
20
+        Assert.assertTrue(ParenChecker.areParenthesisBalanced("<html>"));
21
+    }
22
+
23
+    @Test
24
+    public void testParethesis4(){
25
+        Assert.assertTrue(ParenChecker.areParenthesisBalanced("[a*b]"));
26
+    }
27
+
28
+    @Test
29
+    public void testParethesis5(){
30
+
31
+        Assert.assertEquals(false, ParenChecker.areParenthesisBalanced("<{)}>"));
32
+    }
33
+
34
+    @Test
35
+    public void testParethesis6(){
36
+        Assert.assertTrue(ParenChecker.areParenthesisBalanced("{a,b,c}"));
37
+    }
8 38
 }

+ 37
- 2
src/test/java/io/zipcoder/WCTest.java 查看文件

@@ -1,11 +1,46 @@
1 1
 package io.zipcoder;
2 2
 
3 3
 import org.junit.Assert;
4
+import org.junit.Before;
4 5
 import org.junit.Test;
5 6
 
6
-import java.util.ArrayList;
7
-import java.util.Arrays;
7
+import java.util.*;
8 8
 
9 9
 public class WCTest {
10 10
 
11
+   @Test
12
+    public void testAdd(){
13
+
14
+        String fileName = "./../../someTextFile.txt";
15
+        WC wc = new WC(WC.class.getResource(fileName).getFile());
16
+        wc.addWords();
17
+       // int actual =
18
+        Assert.assertTrue(wc.map.containsKey("sreeram"));
19
+   }
20
+
21
+   @Test
22
+   public void testCountWords(){
23
+       String fileName = "./../../someTextFile.txt";
24
+       WC wc = new WC(WC.class.getResource(fileName).getFile());
25
+       wc.addWords();
26
+
27
+       int expected = 2;
28
+       int actual = wc.map.get("jonnageddala");
29
+       Assert.assertEquals(expected,actual);
30
+   }
31
+
32
+   @Test
33
+    public void testSort(){
34
+       String fileName = "./../../someTextFile.txt";
35
+       WC wc = new WC(WC.class.getResource(fileName).getFile());
36
+       wc.addWords();
37
+
38
+       List<Map.Entry<String, Integer>> list = wc.sortTheMap();
39
+       String expected = "jonnageddala";
40
+       String actual = list.get(0).getKey();
41
+
42
+       Assert.assertEquals(actual, expected);
43
+   }
44
+
45
+
11 46
 }