Question

In: Computer Science

Use Python Write a function that takes a mobile phone number as a string and returns...

Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider:

* all numbers must be 9 or 10 digits in length;

* all numbers must contain at least 4 different digits;

* the sum of all the digits must be equal to the last two digits of the number.

For example '045502226' is a valid number because it is 9 digits in length, it contains 5 different digits (0, 2, 4, 5 & 6), and its digits sum to 26 and the last two digits are '26'.

Solutions

Expert Solution

#Code:-

#function to check if a mobile number is valid or not
#function take input parameter a string and return a bool value (True/False)
def checkValidMobileNumber(s):
#if mobile number is not digit , return False
if(s.isdigit==False):
return False
  
#codition 1 => all numbers must be 9 or 10 digits in length, otherwise return False
if len(s)<9 or len(s)>10 :
return False
  
#codition 2 => if there is less than 4 different digits, return False
if len(set(s))<4 :
return False
  
#calculate sum of digits
sum=0
for i in s:
sum=sum+int(i)
n=len(s);
  
#condition 3 => the sum of all the digits must be equal to the last two digits of the number
if sum!=(int(s[n-2])*10+int(s[n-1])):
return False
  
#if all above conditions are valid then return True
return True

print(checkValidMobileNumber("045502226")) #True
print(checkValidMobileNumber("123")) #False
print(checkValidMobileNumber("1231233211")) #False

//screenshot of code:-

#Screenshot of output:-


Related Solutions

Write Python class that takes a string and returns with a valid phone number. Number format...
Write Python class that takes a string and returns with a valid phone number. Number format is ten-digit numbers consisting of a three-digit area code and a seven-digit number. Clean up different telephone numbers by removing punctuation, and removing incorrect format and the country code (1). You should throw a ValueError with a string if there are too many or too few digits, or the wrong digits. For example, the strings: +1 (617) 111-0000, 617-111-0000, 1 617 111 0000, 617.111.0000...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters...
Write a PYTHON function CommonLetters(mystring) that takes mystring as input and returns the number of letters in mystring that also occur in the string ‘Python’. Using above function, write a program that repeatedly prompts the user for a string and then prints the number of letters in the string that are also in string ‘Python’. The program terminates when the user types an empty string.
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
python 3 please Define a function voweliest that takes as input a string and returns as...
python 3 please Define a function voweliest that takes as input a string and returns as output a tuple where the string that has the most vowels in it is the first element and the second is the number of vowels in that string. Note: don't worry about ties or capital letters Hint: consider defining and using a separate function that counts the number of vowels in a given string
Python Implement function noVowel() that takes a string s as input and returns True if no...
Python Implement function noVowel() that takes a string s as input and returns True if no char- acter in s is a vowel, and False otherwise (i.e., some character in s is a vowel). >>> noVowel('crypt') True >>> noVowel('cwm') True >>> noVowel('car') False
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT