Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 372
Good morning! Here's your coding interview problem for today.

This problem was asked by Amazon.

Write a function that takes a natural number as input and returns the number of digits the input has.

Constraint: don't use any loops.

Pull request description:
Day372
Resolves #748.

Patch:
diff --git a/README.md b/README.md
index 89e7ddc..8aadd78 100644
--- a/README.md
+++ b/README.md
@@ -365,4 +365,5 @@ problems from
 * [Day 367](https://github.com/vaskoz/dailycodingproblem-go/issues/740)
 * [Day 368](https://github.com/vaskoz/dailycodingproblem-go/issues/743)
 * [Day 369](https://github.com/vaskoz/dailycodingproblem-go/issues/745)
+* [Day 372](https://github.com/vaskoz/dailycodingproblem-go/issues/748)
 
diff --git a/day372/problem.go b/day372/problem.go
new file mode 100644
index 0000000..c2ac1c8
--- /dev/null
+++ b/day372/problem.go
@@ -0,0 +1,17 @@
+package day372
+
+import "math"
+
+// NumberOfDigits returns the number of digits
+// for a natural number input.
+// Runs in O(1) time and space and doesn't use loops.
+// https://en.wikipedia.org/wiki/Natural_number
+func NumberOfDigits(n int) int {
+	if n == 0 {
+		return 1
+	} else if n < 0 {
+		n = -n
+	}
+
+	return int(math.Log10(float64(n))) + 1
+}


Test patch:
diff --git a/day372/problem_test.go b/day372/problem_test.go
new file mode 100644
index 0000000..a24f365
--- /dev/null
+++ b/day372/problem_test.go
@@ -0,0 +1,36 @@
+package day372
+
+import "testing"
+
+// nolint
+var testcases = []struct {
+	n         int
+	numDigits int
+}{
+	{0, 1},
+	{3, 1},
+	{9, 1},
+	{10, 2},
+	{-10, 2},
+	{-100, 3},
+	{-99, 2},
+	{99, 2},
+}
+
+func TestNumberOfDigits(t *testing.T) {
+	t.Parallel()
+
+	for _, tc := range testcases {
+		if numDigits := NumberOfDigits(tc.n); numDigits != tc.numDigits {
+			t.Errorf("Given %v, expected %v, got %v", tc.n, tc.numDigits, numDigits)
+		}
+	}
+}
+
+func BenchmarkNumberOfDigits(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			NumberOfDigits(tc.n)
+		}
+	}
+}