Created
March 19, 2021 14:25
-
-
Save saka1029/08fbf9895366b894452480fb5f517435 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.puzzle; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
import org.junit.jupiter.api.Test; | |
class TestEmojiReverse { | |
static String reverse0(String s) { | |
return new StringBuilder(s).reverse().toString(); | |
} | |
@Test | |
void testReverse0() { | |
System.out.println(reverse0("Hello worldπ©βπ¦°π©βπ©βπ¦βπ¦ π΄σ §σ ’σ ·σ ¬σ ³σ Ώ")); | |
} | |
static void reverse(int[] ints) { | |
for (int i = 0, j = ints.length - 1; i < j; ++i, --j) { | |
int temp = ints[i]; | |
ints[i] = ints[j]; | |
ints[j] = temp; | |
} | |
} | |
static String reverse1(String s) { | |
int[] codePoints = s.codePoints().toArray(); | |
reverse(codePoints); | |
return new String(codePoints, 0, codePoints.length); | |
} | |
@Test | |
void testReverse1() { | |
System.out.println(reverse0("Hello worldπ©βπ¦°π©βπ©βπ¦βπ¦ π΄σ §σ ’σ ·σ ¬σ ³σ Ώ")); | |
} | |
@Test | |
void testPrintHex() { | |
"Hello worldπ©βπ¦°π©βπ©βπ¦βπ¦ π΄σ §σ ’σ ·σ ¬σ ³σ Ώ".codePoints() | |
.forEach(c -> System.out.print(Integer.toHexString(c) + " ")); | |
System.out.println(); | |
} | |
@Test | |
void testJoiner() { | |
System.out.println("π¨\u200dπ©\u200dπ§\u200dπ¦"); | |
System.out.println("π¦\u200dπ§\u200dπ©\u200dπ¨"); | |
} | |
static final Pattern JOINED = Pattern.compile(".(\u200D.)+|."); | |
static String reverse2(String s) { | |
List<String> list = new ArrayList<>(); | |
Matcher m = JOINED.matcher(s); | |
while (m.find()) | |
list.add(m.group()); | |
Collections.reverse(list); | |
return String.join("", list); | |
} | |
@Test | |
void testReverse2() { | |
System.out.println(reverse2("Hello worldπ©βπ¦°π©βπ©βπ¦βπ¦ π΄σ §σ ’σ ·σ ¬σ ³σ Ώ")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment