In: Computer Science
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'.
#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:-