Detect Floating Point Number in Python - HackerRank Solution
Problem :
Tutorial :
reA regular expression (or RegEx) specifies a set of strings that matches it.
A regex is a sequence of characters that defines a search pattern, mainly for the use of string pattern matching.
The re.search() 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(re.search(r"ly","similarly")) True
The
re.match()
expression only matches at the beginning of the string.
It either returns a
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")) False >>> print bool(re.match(r"ly","ly should be in the beginning")) True
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.50
✔-1.0
✔.5
✔-.7
✔+.4
✖ -+4.5
For example:
✔+4.50
✔-1.0
✔.5
✔-.7
✔+.4
✖ -+4.5
> Number must contain at least
decimal value.
For example:
12.0
For example:
12.
12.0
> Number must have exactly one . symbol.
> Number must not give any exceptions when converted using float(N).
> Number must not give any exceptions when converted using float(N).
Input Format :
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 :
4 4.0O0 -1.00 +4.54 SomeRandomStuff
Sample Output :
False True True False
Explanation :
40O0 :O is not a digit.
- 1.00: is valid.
+4.54: is valid.
SomeRandomStuff: is not a number.
- 1.00: is valid.
+4.54: is valid.
SomeRandomStuff: is not a number.
Solution :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | # Detect Floating Point Number in Python - Hacker Rank Solution # Python 3 # Enter your code here. Read input from STDIN. Print output to STDOUT # Detect Floating Point Number in Python - Hacker Rank Solution START 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() # Detect Floating Point Number in Python - Hacker Rank Solution END |
Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.