Home > Blockchain >  Given a set of permutations return one specific number
Given a set of permutations return one specific number

Time:08-22

I want your help to catch an specific number or any number that I asked for in bool_check in this case that number is "4321" so i've been struggling to get True and also I want the program to show me that specific number from this set of permutations. For example, I want the output to say: True 4321 So far i got the permutations and the functions but i dont know how to connect everything. Thank you for your time.

from itertools import product

def bool_check(x):
    
    if x == "4321":
        return True
    else:
        return False

      
def find_key(diferent_dig, quantity_dig):
    
    chars = list(diferent_dig)
    perms = []
    for c in product(chars, repeat = quantity_dig):
        perms.append(c)

        
    return perms

print(find_key("1234", 4))

CodePudding user response:

product returns a tuple of the items, but you are searching for a string.

Eg. (1, 2, 3, 4) instead of 1234

I am not too sure what you are after but maybe something like this will help:

from itertools import product

def find_key(diferent_dig, quantity_dig):
   chars = list(diferent_dig)
   perms = []
   for c in product(chars, repeat = quantity_dig):
       perms.append(''.join(c)) # Convert from tuple to String
   return perms

print("1234" in find_key("1234", 4)) # returns True
print("5555" in find_key("1234", 4)) # returns False
  • Related