Skip to content

Commit b7c0147

Browse files
committed
Solve problem 9
1 parent c4df897 commit b7c0147

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

9.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// https://leetcode.com/problems/palindrome-number/
2+
package main
3+
4+
import (
5+
"strconv"
6+
)
7+
8+
func isPalindrome(x int) bool {
9+
// Convert the int to a string
10+
x_str := strconv.Itoa(x)
11+
length_of_x := len(x_str)
12+
13+
halfway := length_of_x / 2
14+
15+
for i := 0; i < halfway; i++ {
16+
if x_str[i] != x_str[length_of_x-i-1] {
17+
return false
18+
}
19+
}
20+
21+
return true
22+
}

9_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestIsPalindrome1(t *testing.T) {
8+
const expected = true
9+
result := isPalindrome(121)
10+
11+
if result != expected {
12+
t.Errorf("Expected %t but got %t", expected, result)
13+
}
14+
}
15+
16+
func TestIsPalindrome2(t *testing.T) {
17+
const expected = false
18+
result := isPalindrome(-121)
19+
20+
if result != expected {
21+
t.Errorf("Expected %t but got %t", expected, result)
22+
}
23+
}
24+
25+
func TestIsPalindrome3(t *testing.T) {
26+
const expected = false
27+
result := isPalindrome(10)
28+
29+
if result != expected {
30+
t.Errorf("Expected %t but got %t", expected, result)
31+
}
32+
}
33+
34+
func BenchmarkIsPalindrome(b *testing.B) {
35+
for i := 0; i < b.N; i++ {
36+
isPalindrome(b.N)
37+
}
38+
}

0 commit comments

Comments
 (0)