Skip to content

Commit 56966e8

Browse files
author
alessandro_gsantos
committed
Added Palindrome algorithm
1 parent b585cc7 commit 56966e8

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/main/kotlin/other/Palindrome.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package other
2+
3+
import java.text.Normalizer
4+
import java.util.regex.Pattern
5+
6+
7+
fun isPalindrome(text: String): Boolean {
8+
9+
val normalizedText = text.normalize()
10+
for(i in normalizedText.indices)
11+
if(normalizedText[i] != normalizedText[normalizedText.length - (i + 1)])
12+
return false
13+
return true;
14+
}
15+
16+
17+
fun String.normalize(): String {
18+
val nfdNormalizedString = Normalizer.normalize(this, Normalizer.Form.NFD)
19+
return Pattern
20+
.compile("\\p{InCombiningDiacriticalMarks}+")
21+
.matcher(nfdNormalizedString)
22+
.replaceAll("")
23+
.toLowerCase()
24+
.replace(" ", "")
25+
26+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package other
2+
3+
import org.junit.Assert.assertTrue
4+
import org.junit.Test
5+
6+
class PalindromeTest {
7+
8+
@Test
9+
fun testPalindromePortuguesePhrase() {
10+
val text = "A mãe te ama"
11+
assertTrue(isPalindrome(text))
12+
}
13+
14+
@Test
15+
fun testPalindromeEnglishPhrase() {
16+
val text = "Mr Owl ate my metal worm"
17+
assertTrue(isPalindrome(text))
18+
}
19+
20+
@Test
21+
fun testPalindromeName() {
22+
val text = "Hannah"
23+
assertTrue(isPalindrome(text))
24+
}
25+
}

0 commit comments

Comments
 (0)