Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3c17ef1

Browse files
Update Validatingphonenumbers.py
Regex solution added as suggested.
1 parent 9f2f9a3 commit 3c17ef1

File tree

1 file changed

+10
-23
lines changed

1 file changed

+10
-23
lines changed

‎RegexandParsing/Validatingphonenumbers.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,14 @@
77
Updated : 3 April 2021
88
Problem : https://www.hackerrank.com/challenges/validating-the-phone-number/problem
99
'''
10-
# Enter your code here. Read input from STDIN. Print output to STDOUT
11-
n=int(input())
12-
for i in range(0,n):
13-
tmp_str=input()
14-
len_tmp_str=len(tmp_str)
15-
if(len_tmp_str!=10):
16-
##print "LENGTH PROBLEM"
17-
print ("NO")
18-
elif(tmp_str[0]!="7" and tmp_str[0]!="8" and tmp_str[0]!="9"):
19-
##print "START PROBLEM"
20-
print ("NO")
10+
11+
from re import compile, match
12+
13+
n = int(input())
14+
for _ in range(n):
15+
phone_number = input()
16+
condition = compile(r'^[7-9]\d{9}$')
17+
if bool(match(condition, phone_number)):
18+
print("YES")
2119
else:
22-
check=1
23-
for i in tmp_str:
24-
if(i>="0" and i<="9"):
25-
continue
26-
else:
27-
check=0
28-
break
29-
if(check==1):
30-
print ("YES")
31-
else:
32-
##print "NUMBER PROBLEM"
33-
print ("NO")
20+
print("NO")

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /