Ctrl K

Is It a Number Literal

Hard
You are given a string text. Decide whether it is a well-formed number. The rules are: - **Digits**: one or more of the digits 0–9. - **Sign**: an optional + or -. - **Mantissa**: a sign followed by one of: digits (42); digits and a dot (42.); digits, a dot and digits (4.2); a dot and digits (.42). - **Exponent**: the letter e or E, then a sign and digits (e7, E-3). - **Number**: a mantissa, optionally followed by an exponent. For example, "0", "007", "-3.", "+.25", "6e5" and "1.5E-2" are numbers, while "e9", "9e", "1-2", "++1", "1.2.3" and "x1" are not. Return True if text is a number and False otherwise.
Example 1
text='-.5E+12' → True

A sign, a fraction with no integer part (.5) and the exponent E+12: everything follows the rules.

Example 2
text='7e1.5' → False

The exponent after e must be an integer, and 1.5 is not.

Example 3
text='+.' → False

A dot without a single digit is not a number.

Constraints
  • 1 ≤ len(text) ≤ 100
  • text consists of English letters (upper and lower case), digits, +, - and .
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPattern With Dot and Star HardWord Blocks in Any Order HardLongest Balanced Bracket Stretch HardDoes the Text Fit the Pattern HardJustify the Lines Hard
loading Python…
Input
text='-.5E+12'
Expected
True

Run checks the first 3 tests in your browser. Submit checks all 308 tests on the server, hidden ones included.