DEV Community

michael-2509
michael-2509

Posted on

Valid Number

Challenge
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 digit

Check leetcode for full code challenge

Approach
The challenge looked so simple until I started solving it. My initial approach was to check the data type and return the appropriate result whether the conditions were true or false. However, this approach won't work for all type cases, so I had to use another approach which lead to using the regex or regular expression in python which helps to check a specified text pattern.

With this approach, I was able to check the validity of the input based on the expressions I specified.

Top comments (0)