Sunday, 2 October 2022

Sample task and solutions error exception - incorrect regex from HackerRank Task

On this chance, I wanna share sample task and solutions error exception - incorrect regex from HackerRank Task. 

Following explanation and scripts solution :

 You are given a string .

Your task is to find out whether  is a valid regex or not.

Input Format

The first line contains integer , the number of test cases.
The next  lines contains the string .

Constraints

Output Format

Print "True" or "False" for each test case without quotes.

Sample Input

2
.*\+
.*+

Sample Output

True
False

Explanation

.*\+ : Valid regex.
.*+: Has the error multiple repeat. Hence, it is invalid.


Source Code of my solution :

import re
for _ in range(int(input())):
    try:
        re.compile(input())
        print(True)
    except re.error:
        print(False)


Source : HackerRank

No comments:

Post a Comment