-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package day202 | ||
|
||
// IsIntegerPalindrome returns true if the integer is a | ||
// palindrome. | ||
// Runs in O(digits) time and O(digits) space. | ||
func IsIntegerPalindrome(i int) bool { | ||
var digits []int | ||
for i != 0 { | ||
digits = append(digits, i%10) | ||
i /= 10 | ||
} | ||
for i := 0; i < len(digits)/2; i++ { | ||
if digits[i] != digits[len(digits)-1-i] { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// IsIntegerPalindromeFaster returns true if the integer is a | ||
// palindrome. | ||
// Runs in O(digits) time and O(1) space. | ||
func IsIntegerPalindromeFaster(i int) bool { | ||
divisor := 1 | ||
for temp := i; temp/divisor > 10; divisor = divisor * 10 { | ||
} | ||
for i >= 10 { | ||
highest := i / divisor | ||
lowest := i % 10 | ||
if highest != lowest { | ||
return false | ||
} | ||
i %= divisor | ||
i /= 10 | ||
divisor /= 100 | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package day202 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
num int | ||
expected bool | ||
}{ | ||
{888, true}, | ||
{121, true}, | ||
{678, false}, | ||
{123124234323, false}, | ||
{123454321, true}, | ||
{23455432, true}, | ||
} | ||
|
||
func TestIsIntegerPalindrome(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := IsIntegerPalindrome(tc.num); result != tc.expected { | ||
t.Errorf("For input %v, expected %v, got %v", tc.num, tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkIsIntegerPalindrome(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
IsIntegerPalindrome(tc.num) | ||
} | ||
} | ||
} | ||
|
||
func TestIsIntegerPalindromeFaster(t *testing.T) { | ||
t.Parallel() | ||
for _, tc := range testcases { | ||
if result := IsIntegerPalindromeFaster(tc.num); result != tc.expected { | ||
t.Errorf("For input %v, expected %v, got %v", tc.num, tc.expected, result) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkIsIntegerPalindromeFaster(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
IsIntegerPalindromeFaster(tc.num) | ||
} | ||
} | ||
} |