-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
aeac477
commit a4b4243
Showing
3 changed files
with
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
A **valid number** can be split up into these components (in order): | ||
|
||
1. A **decimal number** or an **integer**. | ||
2. (Optional) An `'e'` or `'E'`, followed by an **integer**. | ||
|
||
A **decimal number** can be split up into these components (in order): | ||
|
||
1. (Optional) A sign character (either `'+'` or `'-'`). | ||
2. One of the following formats: | ||
1. One or more digits, followed by a dot `'.'`. | ||
2. One or more digits, followed by a dot `'.'`, followed by one or more digits. | ||
3. A dot `'.'`, followed by one or more digits. | ||
|
||
An **integer** can be split up into these components (in order): | ||
|
||
1. (Optional) A sign character (either `'+'` or `'-'`). | ||
2. One or more digits. | ||
|
||
For example, all the following are valid numbers: [`"2"`, `"0089"`, `"-0.1"`, `"+3.14"`, `"4."`, `"-.9"`, `"2e10"`, `"-90E3"`, `"3e+7"`, `"+6e-1"`, `"53.5e93"`, `"-123.456e789"`], while the following are not valid numbers: [`"abc"`, `"1a"`, `"1e"`, `"e3"`, `"99e2.5"`, `"--6"`, `"-+3"`, `"95a54e53"`]. | ||
|
||
Given a string s, return true if s is a ***valid number***. | ||
|
||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: s = "0" | ||
Output: true | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: s = "e" | ||
Output: false | ||
``` | ||
|
||
**Example 3:** | ||
|
||
``` | ||
Input: s = "." | ||
Output: false | ||
``` | ||
|
||
**Constraints:** | ||
|
||
- `1 <= s.length <= 20` | ||
- `s` consists of only English letters (both uppercase and lowercase), digits (`0-9`), plus `'+'`, minus `'-'`, or dot `'.'`. |
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,64 @@ | ||
import { isNumber } from "./index"; | ||
|
||
describe("Valid Number", () => { | ||
test("'2' is valid number", () => { | ||
expect(isNumber("2")).toBe(true); | ||
}); | ||
test("'0089' is valid number", () => { | ||
expect(isNumber("0089")).toBe(true); | ||
}); | ||
test("'-0.1' is valid number", () => { | ||
expect(isNumber("-0.1")).toBe(true); | ||
}); | ||
test("'+3.14' is valid number", () => { | ||
expect(isNumber("+3.14")).toBe(true); | ||
}); | ||
test("'4.' is valid number", () => { | ||
expect(isNumber("4.")).toBe(true); | ||
}); | ||
test("'-.9' is valid number", () => { | ||
expect(isNumber("-.9")).toBe(true); | ||
}); | ||
test("'2e10' is valid number", () => { | ||
expect(isNumber("2e10")).toBe(true); | ||
}); | ||
test("'-90E3' is valid number", () => { | ||
expect(isNumber("-90E3")).toBe(true); | ||
}); | ||
test("'3e+7' is valid number", () => { | ||
expect(isNumber("3e+7")).toBe(true); | ||
}); | ||
test("'+6e-1' is valid number", () => { | ||
expect(isNumber("+6e-1")).toBe(true); | ||
}); | ||
test("'53.5e93' is valid number", () => { | ||
expect(isNumber("53.5e93")).toBe(true); | ||
}); | ||
test("'-123.456e789' is valid number", () => { | ||
expect(isNumber("-123.456e789")).toBe(true); | ||
}); | ||
test("'abc' is NOT a valid number", () => { | ||
expect(isNumber("abc")).toBe(false); | ||
}); | ||
test("'1a' is NOT a valid number", () => { | ||
expect(isNumber("1a")).toBe(false); | ||
}); | ||
test("'1e' is NOT a valid number", () => { | ||
expect(isNumber("1e")).toBe(false); | ||
}); | ||
test("'e3' is NOT a valid number", () => { | ||
expect(isNumber("e3")).toBe(false); | ||
}); | ||
test("'99e2.5' is NOT a valid number", () => { | ||
expect(isNumber("99e2.5")).toBe(false); | ||
}); | ||
test("'--6' is NOT a valid number", () => { | ||
expect(isNumber("--6")).toBe(false); | ||
}); | ||
test("'-+3' is NOT a valid number", () => { | ||
expect(isNumber("-+3")).toBe(false); | ||
}); | ||
test("'95a54e53' is NOT a valid number", () => { | ||
expect(isNumber("95a54e53")).toBe(false); | ||
}); | ||
}); |
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,31 @@ | ||
import { Logger } from "../utils"; | ||
|
||
export const isNumber = (s: string): boolean => { | ||
const sign = `[+-]`; | ||
const digit = `\\d`; | ||
const digits = `(${digit}+)`; | ||
const integer = `(${sign}?${digits})`; | ||
const exponent = `([eE]${integer})`; | ||
const decimal = `(${sign}?((${digits}[.])|(${digits}[.]${digits})|([.]${digits})))`; | ||
|
||
const validNumber = `(${integer}|${decimal})${exponent}?`; | ||
|
||
const fullRegex = new RegExp(`^${validNumber}$`); | ||
|
||
return fullRegex.test(s); | ||
}; | ||
|
||
if (require.main === module) { | ||
const args = process.argv; | ||
const s = args[2] ?? null; | ||
|
||
if (!s) { | ||
Logger.warn( | ||
"Assumed watch mode; Please provide an argument if not intended" | ||
); | ||
const ss = "-.5"; | ||
Logger.info(`[watch-mode] Input: "${ss}", Output: ${isNumber(ss)}`); | ||
} else { | ||
Logger.info(`Input: "${s}", Output: ${isNumber(args[1])}`); | ||
} | ||
} |