Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 123
Good morning! Here's your coding interview problem for today.

This problem was asked by LinkedIn.

Given a string, return whether it represents a number. Here are the different kinds of numbers:

"10", a positive integer
"-10", a negative integer
"10.1", a positive real number
"-10.1", a negative real number
"1e5", a number in scientific notation
And here are examples of non-numbers:

"a"
"x 1"
"a -2"
"-"

Pull request description:
Day123
Resolves #253.

Patch:
diff --git a/README.md b/README.md
index b2c7ff2..3f1fc11 100644
--- a/README.md
+++ b/README.md
@@ -128,3 +128,4 @@ problems from
 * [Day 116](https://github.com/vaskoz/dailycodingproblem-go/issues/242)
 * [Day 118](https://github.com/vaskoz/dailycodingproblem-go/issues/246)
 * [Day 120](https://github.com/vaskoz/dailycodingproblem-go/issues/248)
+* [Day 123](https://github.com/vaskoz/dailycodingproblem-go/issues/253)
diff --git a/day123/problem.go b/day123/problem.go
new file mode 100644
index 0000000..ab70c2c
--- /dev/null
+++ b/day123/problem.go
@@ -0,0 +1,30 @@
+package day123
+
+// IsANumber answers if the given string
+// can be converted to a number.
+func IsANumber(str string) bool {
+	var seenNeg, seenDecimal, seenE, seenDigit bool
+	for _, r := range str {
+		if r == '-' {
+			if seenNeg || seenDigit {
+				return false
+			}
+			seenNeg = true
+		} else if r == '.' {
+			if seenDecimal || !seenDigit {
+				return false
+			}
+			seenDecimal = true
+		} else if r == 'e' || r == 'E' {
+			if seenE || !seenDigit {
+				return false
+			}
+			seenE = true
+		} else if r >= '0' && r <= '9' {
+			seenDigit = true
+		} else {
+			return false
+		}
+	}
+	return seenDigit
+}


Test patch:
diff --git a/day123/problem_test.go b/day123/problem_test.go
new file mode 100644
index 0000000..506c2cf
--- /dev/null
+++ b/day123/problem_test.go
@@ -0,0 +1,40 @@
+package day123
+
+import "testing"
+
+var testcases = []struct {
+	input    string
+	isNumber bool
+}{
+	{"10", true},
+	{"-10", true},
+	{"10.1", true},
+	{"-10.1", true},
+	{"1e5", true},
+	{"a", false},
+	{"x 1", false},
+	{"a -2", false},
+	{"-", false},
+	{"1-2", false},
+	{".12", false},
+	{"0.12", true},
+	{"1eE2", false},
+	{"1E2", true},
+}
+
+func TestIsANumber(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := IsANumber(tc.input); result != tc.isNumber {
+			t.Errorf("For input %v expected %v got %v", tc.input, tc.isNumber, result)
+		}
+	}
+}
+
+func BenchmarkIsANumber(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			IsANumber(tc.input)
+		}
+	}
+}