In: Computer Science
An 8-digit password is required to have exactly three 0’s. The other 5 digits can be any number 1-7, but numbers 1-7 may not be repeated. Write a program that lists all the possible outcomes. Provide your code and first 100 outcomes (Python).
CODE:
#A package to find permutations.
from itertools import permutations
#Getting all the permutations of given list of numbers and of
length 6 because
#[0,0,0] will be treated as 1 it's actual length is 3 so rest 5
digit are required
# hence a total of 6.
perm = permutations([[0,0,0],1,2,3,4,5,6,7],6)
#Converting the permutation to list.
perm_list = list(perm)
#A variable that holds our password.
password = ""
#Looping 100 times to print first 100 password.
for i in range(0,100):
#Converting the tuple present at each index in perm_list to
list.
ls = list(perm_list[i])
#Looping throught each element of the converted list.
for item in ls:
#Checking for [0,0,0] in the list.
if(isinstance(item,list)):
#Appending each 0 to password.
for element in item:
password += str(element)
#If not [0,0,0]
else:
#Appending directly the digit.
password += str(item)
#Printing the password.
print(password)
#Resetting the password variable to hold next password.
password = ""
CODE SCREENSHOT:
CODE OUTPUT: