File tree Expand file tree Collapse file tree 2 files changed +60
-0
lines changed
Expand file tree Collapse file tree 2 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments