In: Computer Science
Write a program in Python to print all possible combinations of phone numbers. The length of the number will be given. Also 3 digits will be given, which can not be used. No two consecutive digits can be same. A number containing 4 would always have 4 in the beginning.
Code:
def checkCons(n): #it will return True if two consecutive digits is not same
for i in range(0,len(n)-1):
if n[i] == n[i+1]: #check two consecutive digits if same
return False #then return false
return True
length = int(input("Enter length of number : ")) #get length of phone number
a = int("1"*length) #lower limit for given length
b = int("9"*length) #upper limit for given length
Numbers=[]
# get 3 digits that will not present in number
d1 = str(input("Enter digit 1 : "))
d2 = str(input("Enter digit 2 : "))
d3 = str(input("Enter digit 3 : "))
for i in range(a,b+1): #it will iterate from lower limit to upper limit
num = str(i) #convert number into string
if (d1 not in num) and (d2 not in num) and (d3 not in num):#check any of given digit should not be present in number
if (checkCons(num)): #check two consecutive digits can not be same
if "4" in num: #if 4 in number
if (num[0] == "4"): #then 4 is also in the begining
Numbers.append(int(num)) #append number in list
else:
Numbers.append(int(num)) #append number in list
print("\nAll combinations of number is: ")
print(Numbers)
Output:
.