Przeglądaj źródła

passed all tests

Eric Foster 6 lat temu
rodzic
commit
845d241ffa

+ 27
- 6
src/main/java/io/zipcoder/StringsAndThings.java Wyświetl plik

@@ -39,9 +39,7 @@ public class StringsAndThings {
39 39
      *           withoutString("Hello there", "x") // Should return "Hello there"
40 40
      */
41 41
     public String withoutString(String base, String remove){
42
-        for (int i=0; i<base.length(); i++){
43
-            
44
-        }
42
+        return base.replace(remove,"");
45 43
     }
46 44
 
47 45
     /**
@@ -75,8 +73,25 @@ public class StringsAndThings {
75 73
      *           gHappy("xxgxx") // Should return  false
76 74
      *           gHappy("xxggyygxx") // Should return  false
77 75
      */
78
-    public Boolean gIsHappy(String input){
79
-        return null;
76
+    public boolean gIsHappy(String input){
77
+        int numOfG = numberOfOccurrences(input, 'g');
78
+        int numOfHappyG = 0;
79
+        for (int i=0; i<input.length()-1; i++){
80
+            if (input.charAt(i) == 'g' && (input.charAt(i-1) == 'g' || input.charAt(i+1) == 'g')){
81
+                numOfHappyG++;
82
+            }
83
+        }
84
+        return (numOfG == numOfHappyG);
85
+    }
86
+
87
+    public int numberOfOccurrences(String input, char a){
88
+        int numberOfOccurrences = 0;
89
+        for (int i = 0; i<input.length(); i++){
90
+            if (input.charAt(i) == a){
91
+                numberOfOccurrences++;
92
+            }
93
+        }
94
+        return numberOfOccurrences;
80 95
     }
81 96
 
82 97
 
@@ -88,6 +103,12 @@ public class StringsAndThings {
88 103
      *            countTriple("a") // Should return 0
89 104
      */
90 105
     public Integer countTriple(String input){
91
-        return null;
106
+        int numOfTriples = 0;
107
+        for (int i=1; i<input.length()-1; i++){
108
+            if (input.charAt(i) == input.charAt(i-1) && input.charAt(i) == input.charAt(i+1)){
109
+                numOfTriples++;
110
+            }
111
+        }
112
+        return numOfTriples;
92 113
     }
93 114
 }

+ 1
- 1
src/test/java/io/zipcoder/StringsAndThingsTest.java Wyświetl plik

@@ -92,7 +92,7 @@ public class StringsAndThingsTest {
92 92
     @Test
93 93
     public void gIsHappyTest3(){
94 94
         Boolean actual = stringsAndThings.gIsHappy("xxggyygxx");
95
-        Assert.assertTrue(actual);
95
+        Assert.assertFalse(actual);
96 96
     }
97 97
 
98 98
     @Test