-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNiceNumber.java
70 lines (59 loc) · 1.62 KB
/
NiceNumber.java
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
63
64
65
66
67
68
69
70
package XL;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.pig.EvalFunc;
import org.apache.pig.data.Tuple;
import org.apache.pig.data.TupleFactory;
public class NiceNumber extends EvalFunc<Tuple> {
@Override
public Tuple exec(Tuple input) throws IOException {
if(input == null || input.size() == 0)
return null;
try {
String value = (String) input.get(0);
HashMap<Character,Integer> m = repeatedSequences(value);
Iterator it = m.entrySet().iterator();
int maxCount = -1;
char maxChar = ' ';
int nPairs= 0;
while(it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
if((Integer)pair.getValue() > maxCount) {
maxCount = (Integer)pair.getValue();
maxChar =(Character)pair.getKey();
if(maxCount >= 2)
nPairs ++;
}
}
Tuple t = TupleFactory.getInstance().newTuple(3);
t.set(0,""+maxChar);
t.set(1,(float)maxCount);
t.set(2,value.contains("4"));
return t;
} catch(Exception e) {
System.err.println("Failed to process the input, is it a string?\n"+e.getMessage());
throw e;
}
}
public static HashMap<Character,Integer> repeatedSequences(String s) {
HashMap<Character,Integer> counts = new HashMap<Character,Integer>();
char[] charArr = s.toCharArray();
char lastChar = '$';
int count = 1;
for(int i = 0; i < charArr.length;i++) {
if(charArr[i] != lastChar) {
lastChar = new Character(charArr[i]);
count = 1;
}
else {
count++;
counts.put(lastChar,count);
}
}
return counts;
}
public static void main(String[] args) {
}
}