-
Notifications
You must be signed in to change notification settings - Fork 0
/
The12DaysOfChristmas.java
34 lines (30 loc) · 1.36 KB
/
The12DaysOfChristmas.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
package com.smlnskgmail.jaman.codewarsjava.kyu7;
import java.util.Comparator;
// https://www.codewars.com/kata/57dd3a06eb0537b899000a64
public class The12DaysOfChristmas implements Comparator<String> {
@Override
public int compare(final String line1, final String line2) {
char line1FirstSymbol = line1.charAt(0);
char line2FirstSymbol = line2.charAt(0);
if (Character.isLetter(line1FirstSymbol)
&& Character.isLetter(line2FirstSymbol)) {
return Character.compare(line1FirstSymbol, line2FirstSymbol);
} else if (Character.isLetter(line1FirstSymbol)) {
return Character.isLowerCase(line1FirstSymbol) ? 1 : -1;
} else if (Character.isLetter(line2FirstSymbol)) {
return Character.isLowerCase(line2FirstSymbol) ? -1 : 1;
} else {
int line1Number = Integer.parseInt(
Character.getNumericValue(line1FirstSymbol) == 1
? line1.substring(0, 2)
: line1.substring(0, 1)
);
int line2Number = Integer.parseInt(
Character.getNumericValue(line2FirstSymbol) == 1
? line2.substring(0, 2)
: line2.substring(0, 1)
);
return -Integer.compare(line1Number, line2Number);
}
}
}