Skip to content

Commit

Permalink
refactor(strings): is palindrome
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianLusina committed Dec 19, 2024
1 parent ecefc7e commit 4e36287
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .sdkmanrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Enable auto-env through the sdkman_auto_env config
# Add key=value pairs of SDKs to use below
java=16.0.0-zulu
java=23.0.1-zulu
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
object Versions {
const val PROJECT = "2.1.1"
const val KOTLIN = "2.1.0"
const val GRADLE = "6.8.3"
const val GRADLE = "8.11.1"

const val DETEKT = "1.9.1"

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ fun isPalindrome(s: String): Boolean {
var right = s.length - 1

while (left < right) {
if (s[left] != s[right]) {
while (left < right && !s[left].isLetterOrDigit()) {
left++
}
while (left < right && !s[right].isLetterOrDigit()) {
right--
}

if (s[left].lowercase() != s[right].lowercase()) {
return false
}

left++
right--
}

return true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.kotlinground.strings.palindromes

import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class IsPalindromeTest {

@Test
fun `should return true for anna`() {
val s = "anna"
val actual = isPalindrome(s)
assertTrue(actual)
}

@Test
fun `should return false for walter`() {
val s = "walter"
val actual = isPalindrome(s)
assertFalse(actual)
}

@Test
fun `should return true for 12321`() {
val s = "12321"
val actual = isPalindrome(s)
assertTrue(actual)
}

@Test
fun `should return false for 123456`() {
val s = "123456"
val actual = isPalindrome(s)
assertFalse(actual)
}

@Test
fun `should return true for Was it a cat I saw`() {
val s = "Was it a cat I saw?"
val actual = isPalindrome(s)
assertTrue(actual)
}

@Test
fun `should return true for Never odd or even`() {
val s = "Never odd or even"
val actual = isPalindrome(s)
assertTrue(actual)
}

@Test
fun `should return true for radar`() {
val s = "radar"
val actual = isPalindrome(s)
assertTrue(actual)
}

@Test
fun `should return true for Live on time, emit no evil`() {
val s = "Live on time, emit no evil"
val actual = isPalindrome(s)
assertTrue(actual)
}

}

0 comments on commit 4e36287

Please sign in to comment.