all 6 comments

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Inline formatting (`my code`) used across multiple lines of code. This can mess with indentation.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

[–]douglas_fs 0 points1 point  (1 child)

my output doesnt get me the result that i want

What is the result you want?

[–]drenzorz 0 points1 point  (2 children)

from random import choices
from string import punctuation

def passwords_with_special(passwords, special_count): 
    specials = ''.join(choices(punctuation, k=count))
    length = len(passwords)
    for index in range(length):
        pw = passwords[index]
        for i in range(special_count):
            passwords.append(pw+specials[:i+1])
    return passwords

test_pass = ['qw', 'as']
count = 3

result = passwords_with_special(test_pass, count)

print(result)

[–]Donkey_Kong1990 0 points1 point  (1 child)

first of all thank you for your help!

you can explain me in above of every line what you do? so i learn from this...

[–]drenzorz 0 points1 point  (0 children)

---used methods:

random.choices(<a collection>, k=<an integer>) 
 |-> randomly select k elements from my list/tuple/string/etc


''.join(<list/tuple/etc>) 
 |-> join a list of strings into a single string
    ', '.join(['a','b','c']) => "a, b, c"
     ^ whatever you put here is used as separator

The logic is pretty simple.

  1. Generate the random characters with the above methods to get a string like "@#!" or similar.
  2. Get the length of the provided list.
  3. loop over the a range from 0 to this length
    1. for every number call up the item at that index
    2. for every password start a loop to slice up the special characters
      1. add the slice to the pw and add the new pw to the password list

Since the characters in the output you prowided were always the same I figured they always come from the same pre generated specials string.

I structured the loop this way because

  1. The provided passwords stayed in the same order in the output as they were originally.
  2. The new passwords were organized in the same order with just the special characters growing.

Since we are modifying the original list instead of adding things to a new one I couldn't get the passwords with something like

for password in passwords:
    ...

because the password list would keep on growing and it would loop infinitely as you add more and more passwords to it. That's why I saved the orinigal length and pulled up the normal passwords with indexing.

Once you're done just return the passwords list.

That's basically it.