Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions hello/reverse/int.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package reverse

import "strconv"

// Int returns its integer argument with its decimal digits reversed,
// preserving the sign.
func Int(i int) int {
neg := i < 0
if neg {
i = -i
}
n, _ := strconv.Atoi(String(strconv.Itoa(i)))
if neg {
n = -n
}
return n
}
26 changes: 26 additions & 0 deletions hello/reverse/int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package reverse_test

import (
"testing"

"golang.org/x/example/hello/reverse"
)

func TestInt(t *testing.T) {
for _, c := range []struct {
in, want int
}{
{0, 0},
{1, 1},
{12, 21},
{123, 321},
{100, 1},
{-123, -321},
{-1, -1},
} {
got := reverse.Int(c.in)
if got != c.want {
t.Errorf("Int(%d) == %d, want %d", c.in, got, c.want)
}
}
}
Loading