Question

In: Computer Science

question: Phone Number CheckingAlice has been having trouble with people filling her website’s form incorrectly, especially...


question: Phone Number CheckingAlice has been having trouble with people filling her website’s form incorrectly, especially with phone numbers. She’d like tomake sure a user’s phone number input only has digits (other than -) and is exactly 10 digits long.Finishvalidphonenumber(string)such that the function returns True if a phone number is valid, and False if not.

def valid_phone_number(number: str) -> bool:
    """
    Return True if the number is a valid phone number. A valid phone number:
    1) Has exactly 10 digits (we are excluding the country code)
    2) Only has digits (no alphabets or special characters)
    3) May have "-" to split numbers. Example: 0123456789 and 012-345-6789 are
    BOTH valid

    Do *not* use loops for this problem. Try to make sure you only use built-ins

    Hint: you will need 3 built-in string methods

    >>> valid_phone_number("0123456789")
    True

    >>> valid_phone_number("012-345-6789")
    True

    >>> valid_phone_number("01-23-45-67-89")
    True
    """
    # TODO: do some checking here

    # change this
    return True

#### Part 2: String Functions with loops ###

Solutions

Expert Solution

Code in Python with comments

def valid_phone_number(number: str) -> bool:
    # Firstly we remove all the occurance of - from our phone number
    number = number.replace("-", "")
    # After this our string should be of length 10 and a  number
    # Check for length
    if(len(number)!=10):
        return false
    # Check for number
    return number.isdigit()


print(valid_phone_number("0123456789"))
print(valid_phone_number("012-345-6789"))
print(valid_phone_number("01-23-45-67-89"))

python code screenshot for indentation

Console Output Screenshot

Let me know in the comments if you have any doubts.
Do leave a thumbs up if this was helpful.


Related Solutions

Hi, I'm having some trouble with this question, especially part B. Stocks offer an expected rate...
Hi, I'm having some trouble with this question, especially part B. Stocks offer an expected rate of return of 18%, with a standard deviation of 22%. Gold offers an expected return of 10% with a standard deviation of 30%. a) In light of the apparent inferiority of gold with respect to average return and volatility, would anyone hold gold in his portfolio? (4 points) b) Assume that the correlation between Stocks and Gold is -0.5. Find the weights wS and...
A person has a firstname, lastname, ID, and email. A phone number is of the form...
A person has a firstname, lastname, ID, and email. A phone number is of the form countrycode, number. A person may have several related telephone numbers, and a telephone number may be associated with multiple people. The possible relationships are: home, work, and mobile. A person may have at most one phone number for each type of relationship. Draw schema diagram and define and create the tables that implement the model and enforce the given constraints.
The College of Business has been having trouble with errors on student admittance records and you...
The College of Business has been having trouble with errors on student admittance records and you have been asked to investigate the cause of the problem. A form is considered defective if it has one or more errors. You think you have found one of the main reasons for the errors but in order to prove it, you decide to develop a process control chart to follow the current process for a few days. On the next three days, you...
Write an application, Phone Numbers, that creates and prints a random phone number of the form...
Write an application, Phone Numbers, that creates and prints a random phone number of the form XXX-XXX-XXXX. Include the dashes in the output. The phone number has some constraints. Do not let the first three digits contain an 3 or 7 (but do not be more restrictive than that) and ensure that the second set of three digits is not greater than 825. Note that any of the digits can be zero and zeroes should be shown.
Im in a java class and having trouble with assigning the seat to a specific number...
Im in a java class and having trouble with assigning the seat to a specific number from the array.. Below are the instructions I was given and then the code that I have already.   ITSE 2321 – OBJECT-ORIENTED PROGRAMMING JAVA Program 8 – Arrays and ArrayList A small airline has just purchased a computer for its new automated reservations system. You have been asked to develop the new system. You are to write an application to assign seats on flight...
this question has been answered incorrectly several times can someone answer it correctly and show me...
this question has been answered incorrectly several times can someone answer it correctly and show me some examples of a run Design and implement a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name,address, phone number, and email address. A student has a class status (year 1,year 2,year 3,or year 4). An employee has an office, salary, and date hired. Use the Date class from JavaAPI...
this question has been answered incorrectly several times can someone answer it correctly and show me...
this question has been answered incorrectly several times can someone answer it correctly and show me some examples of a run. AGAIN SOMEONE SENT AN ANSWER THAT IS INCORRECT CAN SOMEONE ELSE PLEASE GET ME THE PROPER ANSWER. Design and implement a class named Person and its two subclasses named Student and Employee. Make Faculty and Staff subclasses of Employee. A person has a name,address, phone number, and email address. A student has a class status (year 1,year 2,year 3,or...
Shondra tells her doctor that she has been feeling jittery and has had trouble sleeping. She...
Shondra tells her doctor that she has been feeling jittery and has had trouble sleeping. She also complains about her roommates changing the thermostat and making it too high. A blood test shows antibodies and suggests she might have an autoimmune disease. What organ do you think the antibodies are binding to and how might this cause her symptoms?
Having trouble with the Luhn algorithm for java. Everything works great except for when cthe number...
Having trouble with the Luhn algorithm for java. Everything works great except for when cthe number 5105105105105109 is entered. I'm sure there are other numbers that do this, but this returns check digit as 10 when it should come back as 0. Suggestions? here is the algorithm and then my current code without the prints because my error is in the math. I am not allowed to use break. Another key part of the credit card number standard is the...
Having trouble with this question. Do I make a graph from 0 to 1 with ten...
Having trouble with this question. Do I make a graph from 0 to 1 with ten minute intervals so that I would have 6 rectangles? I think this is right. A bug is crawling at a velocity of f(t)= 1/2-t meters per hour, where 0 ≤ t ≤ 1 and t is in hours. a) Find both the left-endpoint and right-endpoint approximations for the area under the graph f(t) during this hour. Use 10-minute increments (be careful about units). b)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT