If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Python Script for Generating 5-Char Passwords

Started by inpuckup, Oct 04, 2024, 12:53 AM

Previous topic - Next topic

inpuckupTopic starter

Craft a robust password generator in Python that yields a 5-character string comprising exactly two digits and three letters from the provided lists. The digits in play are 2 and 5, while the letters are d, f, and t. The password length is fixed at 5 characters, and the constraint is that it must contain only two digits and three letters, without any restrictions on order, location, or repetition.

The expected output of the script should include, but not be limited to, the following permutations:

ttf22
ttf25
ttf52
ttf55
ttt22
tf52d
tf52f
tf52t
td55d
td55f
td55t
t2t5t
t2td2
t2td5
ftf22
ftf25
55dtd
55dtf
55dtt
25ttd
52ttf
25ttt
... and so on.

In essence, we're looking to create a Python script that can generate all possible combinations of 5-character passwords using the given digits and letters, adhering to the specified constraints.
  •  


pimyb

I might approach this problem using a combinatorial approach to generate all possible combinations of digits and letters. Here's an example implementation:

import math

digits = [2, 5]
letters = ['d', 'f', 't']
length = 5
num_digits = 2

# Calculate the number of combinations
num_combinations = math.comb(length, num_digits)

# Generate all combinations
for i in range(num_combinations):
    # Calculate the positions of the digits
    digit_positions = []
    for j in range(num_digits):
        digit_positions.append((i // math.comb(length - j - 1, num_digits - j - 1)) % (length - j))

    # Generate the password
    password = [''] * length
    for j, pos in enumerate(digit_positions):
        password[pos] = str(digits[j % len(digits)])

    # Fill in the remaining positions with letters
    letter_index = 0
    for j in range(length):
        if password[j] == '':
            password[j] = letters[letter_index % len(letters)]
            letter_index += 1

    # Print the password
    print(''.join(password))

This script uses the math.comb function to calculate the number of combinations of digits and letters. It then generates all combinations using a loop and calculates the positions of the digits in each combination. Finally, it fills in the remaining positions with letters and prints the password.
  •  

Domkan

I'd recommend applying a deduplication strategy to smaller datasets before casting them into lists. This approach would significantly reduce the computational overhead and make the process more scalable.


from itertools import permutations, combinations_with_replacement
result_set = set()
for combo in combinations_with_replacement("25", 2):
    for combo2 in combinations_with_replacement("dft", 3):
        temp_permutations = [''.join(p) for p in permutations(combo + combo2)]
        result_set.update(temp_permutations)
print(*result_set, sep='\n')
print(f"Total unique permutations: {len(result_set)}")

  •  

azgraceu9

When dealing with permutation generation, it's essential to avoid premature printing, as this can lead to redundant outputs. Instead, we can leverage set data structures to filter out duplicates and then print the resulting sets directly, eliminating the need for intermediate list accumulation. This approach is particularly effective when each set can fit within the computer's memory, which is typically the case when dealing with varying set sizes.

However, it's crucial to acknowledge that this method can be computationally expensive for large datasets, resulting in prolonged execution times. In contrast, for smaller datasets, the performance is relatively snappy. To illustrate this, consider the following Python code snippet:

from itertools import product

def generate_permutations(digits, letters, digit_length, letter_length):
    with open('pas.txt', 'w') as result:
        for digit_combo in product(digits, repeat=digit_length):
            for letter_combo in product(letters, repeat=letter_length):
                combo = ''.join(digit_combo + letter_combo)
                permuted_combo = set(''.join(p) for p in permutations(combo))
                for perm in permuted_combo:
                    result.write(perm + '\n')

generate_permutations("25", "dft", 2, 3)

In my experiment, I attempted to generate permutations for 4 digits and 5 letters, resulting in a substantial file size of 5388968 bytes. Notably, the processing time was approximately 16 seconds, which is relatively fast considering my machine's underpowered processor.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...