added advanced algorithm for palindrome
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
34
src/main/kotlin/other/PalindromeAdvanced.kt
Normal file
34
src/main/kotlin/other/PalindromeAdvanced.kt
Normal 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()
|
||||
|
||||
}
|
||||
40
src/test/kotlin/other/PalindromeAdvancedTest.kt
Normal file
40
src/test/kotlin/other/PalindromeAdvancedTest.kt
Normal 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)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -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"
|
||||
|
||||
Reference in New Issue
Block a user