## DEV Community 👩‍💻👨‍💻 is a community of 968,547 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Posted on

# Day 10 I4G10DaysofCodeChallenge

Day 10.
Final day,
A valid number can be split up into these components (in order):

A decimal number or an integer.
(Optional) An 'e' or 'E', followed by an integer.
A decimal number can be split up into these components (in order):

(Optional) A sign character (either '+' or '-').
One of the following formats:
One or more digits, followed by a dot '.'.
One or more digits, followed by a dot '.', followed by one or more digits.
A dot '.', followed by one or more digits.
An integer can be split up into these components (in order):

(Optional) A sign character (either '+' or '-').
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

This problem was indeed hard you can tell by the number of dislike, the requirements are many and there are a lot of edge Cases I spent hours figuring how to solve it.

I first check for edge cases like when string is Empty, or has 1 non-Integer character and return false.
I decide to check whether the string is a valid integer, or a decimal number,
By writing two methods,
checkInteger, and checkDecimal,
CheckInteger convert the string to a number and if it's true return true.
check decimal checks the string and verify if its a decimal number, by verifying no characters, other than (+,-,e,E,.) and they don't appear twice.
No '.' after e.
This method works for 1438 test-cases but failed because the string can contain one of the (+,-,e,E,.) but still the are arrange in a way that makes the string invalid number e.g "4e+".

The solution is to split the input by the e or E and follow the validation rule that is check if it's integer or decimal.
"4e", "+" and apply the validation.

Leetcode problem

Implementation in python