Validating phone numbers Problem

This Python program is designed to validate phone numbers. It takes an integer input, which represents the number of phone numbers to be validated, and then for each phone number, it checks if it matches a specific pattern. The pattern is defined such that the phone number must start with a digit between 7 and 9 and must be exactly 10 digits long.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Validating phone numbers Problem HackerRank solution using CodeRankGPT:

The solution works by using Python's built-in 're' module for regular expressions. It compiles a regular expression that matches any string that starts with a digit between 7 and 9 and is followed by exactly 9 more digits. This compiled regular expression is then used to match the input phone numbers. If a phone number matches the pattern, the program prints 'YES', otherwise it prints 'NO'.


from re import compile, match

n = int(input())
for _ in range(n):
    phone_number = input()
    condition = compile(r"^[7-9]\d{9}$")
    if bool(match(condition, phone_number)):
        print("YES")
    else:
        print("NO")

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform