물음표 살인마의 개발블로그

알고리즘 문제/CodeWar

Valid Phone Number

BEstyle 2022. 10. 18. 12:27

DESCRIPTION:

Write a function that accepts a string, and returns true if it is in the form of a phone number.
Assume that any integer from 0-9 in any of the spots will produce a valid phone number.

Only worry about the following format:
(123) 456-7890 (don't forget the space after the close parentheses)

Examples:

"(123) 456-7890"  => true
"(1111)555 2345"  => false
"(098) 123 4567"  => false

def valid_phone_number(pn):
    if len(pn)!=14:
        return False
    if pn[0]!="(" or pn[4]!=")":
        return False
    if pn[9]!="-":
        return False
    if pn[5]!=" ":
        return False
    for i in range(0,3):
        if pn[i+1] not in '0123456789':
            print(i+1)
            return False
        if pn[i+6] not in '0123456789':
            return False
        if pn[i+10] not in '0123456789':
            return False
    return True

'알고리즘 문제 > CodeWar' 카테고리의 다른 글

The Vowel Code  (0) 2022.10.20
Fibonacci, Tribonacci and friends  (0) 2022.10.19
Count IP Addresses  (0) 2022.10.18
Fold an array  (0) 2022.10.17
Triple trouble  (0) 2022.10.17