Question

In: Computer Science

Write a program in Python to print all possible combinations of phone numbers. The length of...

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.

Solutions

Expert Solution

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:

.


Related Solutions

Write a program in Python that will print first 100 numbers of the following series: 0,...
Write a program in Python that will print first 100 numbers of the following series: 0, 1, 1, 2, 3, 5, 8……..
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
Using the Python program: a/ Write a program that adds all numbers from 1 to 100...
Using the Python program: a/ Write a program that adds all numbers from 1 to 100 to a list (in order). Then remove the multiples of 3 from the list. Print the remaining values. b/ Write a program that initializes a list with ten random integers from 1 to 100. Then remove the multiples of 3 and 5 from the list. Print the remaining values. Please show your work and explain. Thanks
Write a C++ program to print all the perfect numbers below a certain given number. A...
Write a C++ program to print all the perfect numbers below a certain given number. A perfect number is a number that that is equal too the sum of it's divisors other than itself . For example, 6 and 28 are all perfect numbers. 6 = ( 1+2 +3) 28 = (1+2+4+7+14) Make sure your program conforms to the following requirements: 1. Accept the upper limit from the user (as an integer). 2. Make sure the number is positive (at...
using python without external libaries Using integer arithmetic operators '+' and '-', print all combinations that...
using python without external libaries Using integer arithmetic operators '+' and '-', print all combinations that sum up to 'sum' by inserting the operators between digits in 'number'. example for 'number=123456789' and 'sum = 0' Print the output using the terminal: Output should be exactly like this from 1 - 22 1 : +1+2-34-56+78+9=0 2 : +1-2-34+5+6+7+8+9=0 3 : +1-23-4-56-7+89=0 ... 12 : -1+2+34-5-6-7-8-9=0 13 : -1+23+4+56+7-89=0 14 : -1-2+34+56-78-9=0 ... 22 : -12-34+56+7-8-9=0
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal...
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal to n, such that the digit to the right is greater than the left digit (ai+1 > ai). E.g. if n=3 (123,124,125,……129,234,…..789)
Python Write a program that will analyse the string input and print “accept” or “reject” based...
Python Write a program that will analyse the string input and print “accept” or “reject” based on the pattern given Accept if it fulfils the following conditions -String length 9 -3 small alphabet (3 lowercase letter) -3 digits -3 big alphabet (3 uppercase letters) -1st alphabet should be a capital -Last alphabet should be a number -Two consecutive alphabets can't be small Reject if any of the conditions is absent So i want it to accept or reject my input,...
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it...
(Do the algorithm and flowchart) Write a C++ program that reads integer numbers and print it in horizontal order of the screen
Write a design algorithm and a python program which asks the user for the length of...
Write a design algorithm and a python program which asks the user for the length of the three sides of two triangles. It should compute the perimeter of the two triangles and display it. It should also display which triangle has the greater perimeter. If both have the same perimeter it should display that the perimeters are the same. Formula: Perimeter of triangleA = (side1A + side2A +side3A) See the 2 sample outputs. Enter side1 of TriangleA: 2 Enter side2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT