diff --git a/DIRECTORY.md b/DIRECTORY.md index 48983d6..fa055de 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -36,4 +36,5 @@ ## Strings - [Issubsequence](strings/issubsequence.jule) +- [Palindrome](strings/palindrome.jule) - [Reverse](strings/reverse.jule) diff --git a/strings/palindrome.jule b/strings/palindrome.jule new file mode 100644 index 0000000..b8e78b5 --- /dev/null +++ b/strings/palindrome.jule @@ -0,0 +1,10 @@ +fn IsPalindrome(s: string): bool { + mut runes := []rune(s) + mut i, mut j := 0, len(runes)-1 + for i < j; i, j = i+1, j-1 { + if runes[i] != runes[j] { + return false + } + } + return true +} \ No newline at end of file diff --git a/strings/palindrome_test.jule b/strings/palindrome_test.jule new file mode 100644 index 0000000..ad4d1d1 --- /dev/null +++ b/strings/palindrome_test.jule @@ -0,0 +1,52 @@ +#build test + +use "std/testing" + +struct palindromeTest { + name: string + s: string + expected: bool +} + +let palindromeTests: []palindromeTest = [ + { + "Simple palindrome", + "madam", + true, + }, + { + "Simple non-palindrome", + "hello", + false, + }, + { + "Empty string", + "", + true, + }, + { + "Single character", + "a", + true, + }, + { + "Even length palindrome", + "abba", + true, + }, + { + "Case sensitive mismatch", + "Madam", + false, + }, +] + +#test +fn testIsPalindrome(t: &testing::T) { + for _, test in palindromeTests { + funcResult := IsPalindrome(test.s) + if test.expected != funcResult { + t.Errorf("expected: {}, got {}", test.expected, funcResult) + } + } +} \ No newline at end of file