added advanced algorithm for palindrome

This commit is contained in:
Dmitry
2022-02-03 12:03:56 +07:00
parent f3b7a7aff0
commit 062f75e2f1
8 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,34 @@
package other
/**
* Algorithm for checking a string for a palindrome
*
*/
class PalindromeAdvanced(private val text: String) {
/**
* checks a string for a palindrome
*
* @return returns true if the string is a palindrome
*/
fun isYes() : Boolean {
if (text.length <= 1) {
return true
}
for (i in 0 until text.length / 2) {
if (text[i] != text[text.length - 1 - i]) {
return false
}
}
return true
}
/**
* checks if a string is not a palindrome
*
* @return returns true if the string is not a palindrome
*/
fun isNot() = !isYes()
}

View File

@ -0,0 +1,40 @@
package other
import org.junit.Test
import org.junit.jupiter.api.Assertions.*
class PalindromeAdvancedTest {
@Test
fun test_empty_string() {
val text1 = ""
assertEquals(PalindromeAdvanced(text1).isYes(), true)
}
@Test
fun test_one_length() {
val text1 = "a"
assertEquals(PalindromeAdvanced(text1).isYes(), true)
}
@Test
fun test_is_palindrome() {
val text1 = "tenet"
assertEquals(PalindromeAdvanced(text1).isYes(), true)
val text2 = "friend"
assertEquals(PalindromeAdvanced(text2).isYes(), false)
}
@Test
fun test_is_not_palindrome() {
val text1 = "white"
assertEquals(PalindromeAdvanced(text1).isNot(), true)
val text2 = "tenet"
assertEquals(PalindromeAdvanced(text2).isNot(), false)
}
}

View File

@ -1,10 +1,23 @@
package other
import org.junit.Test
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.*
internal class PalindromeTest {
@Test
fun test_empty_string() {
val text1 = ""
assertEquals(Palindrome(text1).isYes(), true)
}
@Test
fun test_one_length() {
val text1 = "a"
assertEquals(Palindrome(text1).isYes(), true)
}
@Test
fun test_is_palindrome() {
val text1 = "tenet"