Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use python to do this \"\"\" CMPUT 175 LAB 02 You are provided with four empty f

ID: 3888033 • Letter: U

Question

Use python to do this

"""

CMPUT 175

LAB 02

You are provided with four empty functions. You should write code inside

these functions so that they perform as required. Each function also contains

instructions in the form of comments on the how to complete them. Please read

the comments carefully.

* Note that you are NOT allowed to import any other modules.

"""

LEN = 12

POS_HYPHEN_1 = 3

POS_HYPHEN_2 = 7

def check_length(phone_number):

    """

    This function should determine if the length of the phone_number is correct

    """

    return True

def check_hyphens(phone_number):

    """

    This function should determine if the hyphens are at the correct positions

    """

    return True

def check_digits(phone_number):

    """

    This function should determine if all the characters other than the

    hyphens are digits

    """

    return True

def modify(phone_number):

    """

    Given a valid phone_number, this function should replace the

    largest digits with 'X' and the second largest digits with 'Y'

    """

    modified_phone_number = ''

    return modified_phone_number

def validate(phone_number):

    """

    This function should check if phone_number input by the user is valid

    Return True if its valid otherwise return False

    """

    # Test the length of the phone number

    if check_length(phone_number) != True:

        return False

    # Test if the hyphens are placed correctly

    if check_hyphens(phone_number) != True:

        return False

    # Test if the digits are placed correctly

    if check_digits(phone_number) != True:

        return False

    return True

def main():

    while True:

        phone_number = input('Enter a phone number: ')

        # If the User enters an empty string: Exit

        if phone_number == '':

            return

        # Test if the number is valid

        if not validate(phone_number):

            print ('The phone number is not valid')

        else:

            # If the number is valid, replace the largest digit with X

            # and the second largest with Y

            print (modify(phone_number))

if __name__ == '__main__':

    main()

Explanation / Answer

Here is the code for you:

#!/usr/bin/python

LEN = 12
POS_HYPHEN_1 = 3
POS_HYPHEN_2 = 7
def check_length(phone_number):
"""
This function should determine if the length of the phone_number is correct
"""
return len(phone_number) == LEN
def check_hyphens(phone_number):
"""
This function should determine if the hyphens are at the correct positions
"""
if phone_number[POS_HYPHEN_1-1] <> '_':
   return False
if phone_number[POS_HYPHEN_2-1] <> '_':
   return False
for i in range(len(phone_number)):
   if phone_number[i] == '_':
       if i <> POS_HYPHEN_1-1 and i <> POS_HYPHEN_2-1:
           return False
return True
def check_digits(phone_number):
"""
This function should determine if all the characters other than the
hyphens are digits
"""
for i in range(len(phone_number)):
   if i <> POS_HYPHEN_1-1 and i <> POS_HYPHEN_2-1:
       if phone_number[i].isdigit() == False:
           return False
return True
def modify(phone_number):
"""
Given a valid phone_number, this function should replace the
largest digits with 'X' and the second largest digits with 'Y'
"""
modified_phone_number = ''
largeIndex = 0
for i in range(LEN):
   if i <> POS_HYPHEN_1-1 and i <> POS_HYPHEN_2-1 and int(phone_number[i]) > int(phone_number[largeIndex]):
       largeIndex = i
secondIndex = 0      
for i in range(LEN):
   if i <> POS_HYPHEN_1-1 and i <> POS_HYPHEN_2-1 and i <> largeIndex and int(phone_number[i]) > int(phone_number[largeIndex]):
       secondIndex = i
modified_phone_number = phone_number
modified_phone_number[largeIndex] = 'X'
modified_phone_number[secondIndex] = 'Y'      
return modified_phone_number
def validate(phone_number):
"""
This function should check if phone_number input by the user is valid
Return True if its valid otherwise return False
"""
# Test the length of the phone number
if check_length(phone_number) != True:
return False
# Test if the hyphens are placed correctly
if check_hyphens(phone_number) != True:
return False
# Test if the digits are placed correctly
if check_digits(phone_number) != True:
return False
return True
def main():
while True:
phone_number = raw_input('Enter a phone number: ')
# If the User enters an empty string: Exit
if phone_number == '':
return
# Test if the number is valid
if not validate(phone_number):
print ('The phone number is not valid')
else:
# If the number is valid, replace the largest digit with X
# and the second largest with Y
print (modify(phone_number))
if __name__ == '__main__':
main()