Skip to content

Commit 324fcc8

Browse files
authored
strings: add IsPalindrome (#50)
1 parent 890297e commit 324fcc8

3 files changed

Lines changed: 63 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@
3636

3737
## Strings
3838
- [Issubsequence](strings/issubsequence.jule)
39+
- [Palindrome](strings/palindrome.jule)
3940
- [Reverse](strings/reverse.jule)

strings/palindrome.jule

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn IsPalindrome(s: string): bool {
2+
mut runes := []rune(s)
3+
mut i, mut j := 0, len(runes)-1
4+
for i < j; i, j = i+1, j-1 {
5+
if runes[i] != runes[j] {
6+
return false
7+
}
8+
}
9+
return true
10+
}

strings/palindrome_test.jule

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#build test
2+
3+
use "std/testing"
4+
5+
struct palindromeTest {
6+
name: string
7+
s: string
8+
expected: bool
9+
}
10+
11+
let palindromeTests: []palindromeTest = [
12+
{
13+
"Simple palindrome",
14+
"madam",
15+
true,
16+
},
17+
{
18+
"Simple non-palindrome",
19+
"hello",
20+
false,
21+
},
22+
{
23+
"Empty string",
24+
"",
25+
true,
26+
},
27+
{
28+
"Single character",
29+
"a",
30+
true,
31+
},
32+
{
33+
"Even length palindrome",
34+
"abba",
35+
true,
36+
},
37+
{
38+
"Case sensitive mismatch",
39+
"Madam",
40+
false,
41+
},
42+
]
43+
44+
#test
45+
fn testIsPalindrome(t: &testing::T) {
46+
for _, test in palindromeTests {
47+
funcResult := IsPalindrome(test.s)
48+
if test.expected != funcResult {
49+
t.Errorf("expected: {}, got {}", test.expected, funcResult)
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)