(digit_set, non_zero_chars, ok_zero_chars)
This function creates permutations given the set of digits,
       letters not alllowed to be 0, and letters allowed to be 0
This function creates permutations given the set of digits,
       letters not alllowed to be 0, and letters allowed to be 0
This function creates permutations given the set of digits letters not alllowed to be 0 and letters allowed to be 0
def dig_perms(digit_set, non_zero_chars, ok_zero_chars):
    """This function creates permutations given the set of digits,
       letters not alllowed to be 0, and letters allowed to be 0
    """
    non_zero_count = len(non_zero_chars)  # How many letters are non-0
    ok_zero_count = len(ok_zero_chars)  # How many letters are allowed 0
    total_count = non_zero_count + ok_zero_count  # Total number of letters
    if total_count < 1:  # if total numbers of letters is 0
        return [()]  # return a singe empty permutation
    non_zero_digit_set = digit_set - set((0,))  # generate a non-zero digit set
    available_zero_digit_count = len(non_zero_digit_set)  # how many non-zero digits are available
    ok_zero_digit_count = len(digit_set)  # how many ok zero digits are available
    # if either fewer digits than letters at all or fewer non-0 digits
    # than letters that need to be non-zero
    if ok_zero_digit_count < total_count or available_zero_digit_count < non_zero_count:
        return []  # Return no permutations possible
    # Simple case when zeros are allowed everwhere
    # or no zero is containted within the given digits
    elif non_zero_count == 0 or ok_zero_digit_count == available_zero_digit_count:
        return permutations(digit_set, total_count)
    # Another simple case all letters are non-0
    elif ok_zero_count == 0:
        return permutations(non_zero_digit_set, total_count)
    else:
        # General case
        # Generate a list of possible 0 positions
        positions_list = list(range(non_zero_count, total_count))
        # Chain two iterators
        # first iterator with all non-0 permutations
        # second iterator with all permulations without 1 letter
        # insert 0 in all possible positions of that permutation
        return chain(permutations(non_zero_digit_set, total_count),
                     map(lambda iters: iters[0][:iters[1]] + (0,) + iters[0][iters[1]:],
                         product(permutations(non_zero_digit_set, total_count - 1),
                                 positions_list)))
def dig_perms ( digit_set , non_zero_chars , ok_zero_chars ) : non_zero_count = len ( non_zero_chars ) # How many letters are non-0 ok_zero_count = len ( ok_zero_chars ) # How many letters are allowed 0 total_count = non_zero_count + ok_zero_count # Total number of letters if total_count < 1 : # if total numbers of letters is 0 return [ ( ) ] # return a singe empty permutation non_zero_digit_set = digit_set - set ( ( 0 , ) ) # generate a non-zero digit set available_zero_digit_count = len ( non_zero_digit_set ) # how many non-zero digits are available ok_zero_digit_count = len ( digit_set ) # how many ok zero digits are available # if either fewer digits than letters at all or fewer non-0 digits # than letters that need to be non-zero if ok_zero_digit_count < total_count or available_zero_digit_count < non_zero_count : return [ ] # Return no permutations possible # Simple case when zeros are allowed everwhere # or no zero is containted within the given digits elif non_zero_count == 0 or ok_zero_digit_count == available_zero_digit_count : return permutations ( digit_set , total_count ) # Another simple case all letters are non-0 elif ok_zero_count == 0 : return permutations ( non_zero_digit_set , total_count ) else : # General case # Generate a list of possible 0 positions positions_list = list ( range ( non_zero_count , total_count ) ) # Chain two iterators # first iterator with all non-0 permutations # second iterator with all permulations without 1 letter # insert 0 in all possible positions of that permutation return chain ( permutations ( non_zero_digit_set , total_count ) , map ( lambda iters : iters [ 0 ] [ : iters [ 1 ] ] + ( 0 , ) + iters [ 0 ] [ iters [ 1 ] : ] , product ( permutations ( non_zero_digit_set , total_count - 1 ) , positions_list ) ) )