-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.java
More file actions
62 lines (49 loc) · 1.71 KB
/
Utilities.java
File metadata and controls
62 lines (49 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.dj;
public class Utilities {
//Returns a char containing every nth char. When
// sourceArray.length() < n, returns sourceArray
public char[] everyNthChar(char[] sourceArray, int n) {
if (sourceArray == null || sourceArray.length < n) {
return sourceArray;
}
int returnedLength = sourceArray.length / n;
char[] result = new char[returnedLength];
int index = 0;
for (int i = n-1; i < sourceArray.length; i += n) {
result[index++] = sourceArray[i];
}
return result;
}
//Removes pairs of the same character that are next
// to each other, by removing one occurrence of the character.
// "ABBCDEEF" -> "ABCDEF"
public String removePairs(String source) {
//If the length is less than 2, there won't be any pairs
if (source == null || source.length() < 2) {
return source;
}
StringBuilder sb = new StringBuilder();
char[] string = source.toCharArray();
for (int i = 0; i < string.length - 1; i++) {
System.out.println(string[i]);
if (string[i] != string[i + 1]) {
sb.append(string[i]);
}
}
System.out.println(string[string.length -1]);
// Add final character that will always be safe
sb.append(string[string.length - 1]);
return sb.toString();
}
// perform a conversion based on some internal
// business rule
public int converter(int a, int b) {
return (a/b) + (a * 30) - 2;
}
public String nullIfOddLength(String source) {
if (source.length() % 2 == 0) {
return source;
}
return null;
}
}