From ec091d08aaa71f3c48b910d0610af7bd860ef849 Mon Sep 17 00:00:00 2001 From: Enrique Vazquez Date: Mon, 21 Sep 2026 12:52:16 -0600 Subject: [PATCH] hello/reverse: add Int example and its test Int returns the decimal representation of an integer with its digits reversed, preserving the sign. Includes a table-driven test covering positive, negative and zero inputs. --- hello/reverse/int.go | 17 +++++++++++++++++ hello/reverse/int_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 hello/reverse/int.go create mode 100644 hello/reverse/int_test.go diff --git a/hello/reverse/int.go b/hello/reverse/int.go new file mode 100644 index 00000000..179408a3 --- /dev/null +++ b/hello/reverse/int.go @@ -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 +} diff --git a/hello/reverse/int_test.go b/hello/reverse/int_test.go new file mode 100644 index 00000000..973a41a2 --- /dev/null +++ b/hello/reverse/int_test.go @@ -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) + } + } +}