#32 dmjpub

Abierta
dmjpub desea fusionar 2 commits de dmjpub/ZCW-Arrays-StringArrayUtilities-BlueJ:master en master
Se han modificado 2 ficheros con 148 adiciones y 12 borrados
  1. 106
    12
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 106
- 12
StringArrayUtils.java Ver fichero

1
- 
1
+import java.util.Arrays;
2
 
2
 
3
 /**
3
 /**
4
  * Created by leon on 1/29/18.
4
  * Created by leon on 1/29/18.
5
  */
5
  */
6
 public class StringArrayUtils {
6
 public class StringArrayUtils {
7
+    
7
     /**
8
     /**
8
      * @param array array of String objects
9
      * @param array array of String objects
9
      * @return first element of specified array
10
      * @return first element of specified array
10
      */ // TODO
11
      */ // TODO
11
     public static String getFirstElement(String[] array) {
12
     public static String getFirstElement(String[] array) {
12
-        return null;
13
+        return array[0];
13
     }
14
     }
14
 
15
 
15
     /**
16
     /**
17
      * @return second element in specified array
18
      * @return second element in specified array
18
      */
19
      */
19
     public static String getSecondElement(String[] array) {
20
     public static String getSecondElement(String[] array) {
20
-        return null;
21
+        return array[1];
21
     }
22
     }
22
 
23
 
23
     /**
24
     /**
25
      * @return last element in specified array
26
      * @return last element in specified array
26
      */ // TODO
27
      */ // TODO
27
     public static String getLastElement(String[] array) {
28
     public static String getLastElement(String[] array) {
28
-        return null;
29
+        return array[array.length-1];
29
     }
30
     }
30
 
31
 
31
     /**
32
     /**
33
      * @return second to last element in specified array
34
      * @return second to last element in specified array
34
      */ // TODO
35
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
36
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
37
+        return array[array.length-2];
37
     }
38
     }
38
 
39
 
39
     /**
40
     /**
42
      * @return true if the array contains the specified `value`
43
      * @return true if the array contains the specified `value`
43
      */ // TODO
44
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
45
     public static boolean contains(String[] array, String value) {
46
+        for(int i = 0; i < array.length; i++) {
47
+            if(array[i].equals(value)) return true;
48
+        }
45
         return false;
49
         return false;
46
     }
50
     }
47
 
51
 
50
      * @return an array with identical contents in reverse order
54
      * @return an array with identical contents in reverse order
51
      */ // TODO
55
      */ // TODO
52
     public static String[] reverse(String[] array) {
56
     public static String[] reverse(String[] array) {
53
-        return null;
57
+        String[] revArray = new String[array.length];
58
+        for (int i = 0; i < array.length; i++) {
59
+            revArray[i] = array[array.length - 1 - i];
60
+        }
61
+        return revArray;
54
     }
62
     }
55
 
63
 
56
     /**
64
     /**
58
      * @return true if the order of the array is the same backwards and forwards
66
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
67
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
68
     public static boolean isPalindromic(String[] array) {
61
-        return false;
69
+
70
+        for ( int i = 0; i < array.length / 2; i++) {
71
+            if (array[i] != array[array.length - 1 - i]) {
72
+                return false;
73
+            }
74
+        }    
75
+        return true;
62
     }
76
     }
63
 
77
 
64
     /**
78
     /**
66
      * @return true if each letter in the alphabet has been used in the array
80
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
81
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
82
     public static boolean isPangramic(String[] array) {
83
+        //boolean result= true;
84
+        String words = String.join("", array).toLowerCase();
85
+
86
+      int counterTrue = 0;
87
+        for(char c = 'a';c <= 'z'; c++) {
88
+
89
+            if(words.contains(String.valueOf(c))) {
90
+                //result = false;
91
+                counterTrue++;
92
+                //return true;
93
+            } 
94
+        }
95
+
96
+        if (counterTrue == 26) return true;
69
         return false;
97
         return false;
70
     }
98
     }
99
+        
100
+    
71
 
101
 
72
     /**
102
     /**
73
      * @param array array of String objects
103
      * @param array array of String objects
74
      * @param value value to check array for
104
      * @param value value to check array for
75
      * @return number of occurrences the specified `value` has occurred
105
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
106
      */ // TODO
107
+
77
     public static int getNumberOfOccurrences(String[] array, String value) {
108
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
109
+        int num = 0;
110
+        for (int i=0;i<array.length;i++){
111
+            if (array[i]== value){
112
+                num++;
113
+            }
114
+
115
+        }
116
+
117
+        return num;
79
     }
118
     }
80
 
119
 
81
     /**
120
     /**
84
      * @return array with identical contents excluding values of `value`
123
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
124
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
125
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
126
+        int newLength = array.length;
127
+        String[] result = new String[array.length-1];
128
+        String[] removedValue = new String[1];
129
+        int count1 = 0; // count tracks the current index of the output array
130
+        int count2 = 0; // count tracks the current index of the output array
131
+        
132
+        for(int i = 0; i < array.length; i++) // i tracks the current index of the input array
133
+        {
134
+            if(!array[i].contains(valueToRemove)) {
135
+                result[count1] = array[i]; 
136
+                count1++;
137
+            }else {
138
+                removedValue[count2] = array[i];
139
+                count2++;
140
+            }
141
+        }
142
+        return result;
88
     }
143
     }
89
 
144
 
90
     /**
145
     /**
92
      * @return array of Strings with consecutive duplicates removes
147
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
148
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
149
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
150
+        String[] resultArray = new String[array.length];
151
+        int resultIndex = 0;
152
+        for(int i = 0; i < array.length; i++) {
153
+            
154
+            if(i == 0) {
155
+                resultArray[resultIndex] = array[i];
156
+                resultIndex++;
157
+            }
158
+            else if((i > 0 && i < array.length-2) && (!array[i].equals(array[i-1]) || !array[i].equals(array[i+1])))
159
+            {
160
+                resultArray[resultIndex] = array[i];
161
+                resultIndex++;
162
+            }else if (i == array.length-1 && !array[i].equals(array[i-1])) {
163
+                resultArray[resultIndex] = array[i];
164
+                resultIndex++;
165
+            }
166
+        }
167
+        String[] realAnswer = Arrays.copyOf(resultArray, resultIndex);
168
+        return realAnswer;
96
     }
169
     }
97
 
170
 
98
     /**
171
     /**
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
173
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
174
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
175
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
176
+        String[] resultArray = new String[array.length];
177
+        StringBuilder dups = new StringBuilder();
178
+        int resultIndex = 0;
179
+        for(int i = 0; i < array.length; i++) {
180
+            
181
+            if(i == 0) {
182
+                resultArray[resultIndex] = array[i];
183
+                dups.append(array[i].toString());
184
+                resultIndex++;
185
+            }
186
+            else if((i > 0 && i < array.length-2) && (array[i].equals(array[i-1]) || array[i].equals(array[i+1])))
187
+            {
188
+                resultArray[resultIndex] = array[i];
189
+                dups.append(array[i].toString());
190
+                resultIndex++;
191
+            }else if (i == array.length-1 && array[i].equals(array[i-1])) {
192
+                resultArray[resultIndex] = array[i];
193
+                dups.append(array[i].toString());
194
+                resultIndex++;
195
+            }
196
+        }
197
+        String[] realAnswer = Arrays.copyOf(resultArray, resultIndex);
198
+        return realAnswer;
104
     }
199
     }
105
 
200
 
106
-
107
 }
201
 }

+ 42
- 0
package.bluej Ver fichero

1
+#BlueJ package file
2
+dependency1.from=StringArrayUtilsTest
3
+dependency1.to=StringArrayUtils
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=714
6
+editor.fx.0.width=800
7
+editor.fx.0.x=442
8
+editor.fx.0.y=33
9
+objectbench.height=101
10
+objectbench.width=461
11
+package.divider.horizontal=0.6
12
+package.divider.vertical=0.8007380073800738
13
+package.editor.height=427
14
+package.editor.width=674
15
+package.editor.x=267
16
+package.editor.y=85
17
+package.frame.height=600
18
+package.frame.width=800
19
+package.numDependencies=1
20
+package.numTargets=2
21
+package.showExtends=true
22
+package.showUses=true
23
+project.charset=UTF-8
24
+readme.height=58
25
+readme.name=@README
26
+readme.width=47
27
+readme.x=10
28
+readme.y=10
29
+target1.height=50
30
+target1.name=StringArrayUtilsTest
31
+target1.showInterface=false
32
+target1.type=UnitTestTargetJunit4
33
+target1.width=150
34
+target1.x=130
35
+target1.y=10
36
+target2.height=50
37
+target2.name=StringArrayUtils
38
+target2.showInterface=false
39
+target2.type=ClassTarget
40
+target2.width=120
41
+target2.x=70
42
+target2.y=70