Detect Floating Point Number in Python | HackerRank Solution

Hello coders, today we are going to solve Detect Floating Point Number HackerRank Solution in Python.

Detect Floating Point Number in Python


You are given a string N.
Your task is to verify that N is a floating point number.

In this task, a valid float number must satisfy all of the following requirements:

>Number can start with +- or . symbol.
For example:
✖ -+4.5

>Number must contain at least 1 decimal value.
For example:
✖ 12.

 >Number must have exactly one . symbol.
 >Number must not give any exceptions when converted using float(N).

Input Format

The first line contains an integer T, the number of test cases.
The next T line(s) contains a string N.


  • 0 < T < 10

Output Format

Output True or False for each test case.

Sample Input 0


Sample Output 0


Explanation 0

4.0O0O is not a digit.
-1.00: is valid.
+4.54: is valid.
SomeRandomStuff: is not a number.

Solution – Detect Floating Point Number in Python

# Enter your code here. Read input from STDIN. Print output to STDOUT
from re import match, compile

pattern = compile('^[-+]?[0-9]*\.[0-9]+$')
for _ in range(int(input())):

Disclaimer: The above Problem (Detect Floating Point Number) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *