Detect Floating Point Number in Python – HackerRank Solution

Detect Floating Point Number in Python Hacker Rank Solution
Detect Floating Point Number in Python – HackerRank Solution

A regular expression (or RegEx) specifies a set of strings that matches it.
regex is a sequence of characters that defines a search pattern, mainly for the use of string pattern matching.
The expression scans through a string looking for the first location where the regex pattern produces a match.
It either returns a MatchObject instance or returns None if no position in the string matches the pattern.
Code :

>>> import re
>>> print bool("ly","similarly"))

The re.match() expression only matches at the beginning of the string.
It either returns a MatchObject instance or returns None if the string does not match the pattern.

Code :

>>> import re
>>> print bool(re.match(r"ly","similarly"))
>>> print bool(re.match(r"ly","ly should be in the beginning"))

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  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.

Constraints :

  • 0 < T < N

Output Format :

Output True or False for each test case.

Sample Input :


Sample Output :


Explanation :

40O0 :O is not a digit.
– 1.00: is valid.
+4.54: is valid.
SomeRandomStuff: is not a number.

Detect Floating Point Number in Python – HackerRank Solution

import re
class Main():
    def __init__(self):
        self.n = int(input())
        for i in range(self.n):
            self.s = input()
            print(bool(re.match(r'^[-+]?[0-9]*\.[0-9]+$', self.s)))
if __name__ == '__main__':
    obj = Main()

Disclaimer: The above Problem (Detect Floating Point Number in Python ) is generated by Hackerrank but the Solution is Provided by Chase2Learn. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the following contact form thank you.

Leave a Comment