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.
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?
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...
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)...
I am having trouble with a homework question The financial statements of P&G are presented in...
I am having trouble with a homework question The financial statements of P&G are presented in Appendix B. The company’s complete annual report, including the notes to the financial statements, is available online. Click here to view Appendix B Refer to P&G’s financial statements and accompanying notes to answer the following questions. Part 1 New attempt is in progress. Some of the new entries may impact the last attempt grading.Your answer is partially correct. Under P&G’s stock-based compensation plan, stock...
Hey. I am having trouble with a finance question: ----------------------------------------------------------------------------------------------------------------------------------------- Consider two bonds. The first is...
Hey. I am having trouble with a finance question: ----------------------------------------------------------------------------------------------------------------------------------------- Consider two bonds. The first is a 6% coupon bond with six years to maturity, and a yield to maturity of 4.5% annual rate, compounded semi-annually. The second bond is a 2% coupon bond with six years to maturity and a yield to maturity of 5.0%, annual rate, compounded semi-annually. Given the data for the first two bonds, now consider a third bond: a zero coupon bond with six years...
Having some trouble with this python question. An answer would be greatly appreciated! #Write a function...
Having some trouble with this python question. An answer would be greatly appreciated! #Write a function called are_anagrams. The function should #have two parameters, a pair of strings. The function should #return True if the strings are anagrams of one another, #False if they are not. # #Two strings are considered anagrams if they have only the #same letters, as well as the same count of each letter. For #this problem, you should ignore spaces and capitalization. # #So, for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT