On 08/01/2021 05.52, Bischoop wrote: > On 2021-01-07, Chris Angelico <ros...@gmail.com> wrote: >> >> True. Unfortunately, it doesn't work, so what you'd have is something >> that can be easily parameterized to not work on other numbers of >> characters too. :) >> > > My bad is I'm kinda maniac and have to know how to, I know best solution > itertool but... I just must know, was thinking that studing the itertool > code would give me an answer but found it's in C. > Now I try list comprehension for it, so it's another day on it for me.
BTW have you realised a second 'typo' - that there are only 24 letters in this 'alphabet'? It is not difficult to parametrise the building of a "password cracking dictionary"! (NB not same definition as a Python dictionary) Take it one step (one character) at a time... We start from our list of "letters". This is every permutation of the alphabet, taken one at a time. Call it the one_character_list. [Let's not distract from the problem by arguing about the definition of "permutation"!] Next, we'll take the one_character_list and permute that, with each of our letter[s]. Thus 'a, b, c' expands to 'aa, ab, ac, ... ba, bb, bc, ... ca, cb, cc, ...'. For every one of the 24 elements of the one_character_list we now have 24 elements in the two_character_list, ie len( two_character_list ) should be 24*24 (== 24**2) and each element should be unique (ie same rule as a set). NB above equation varies if the rule for what may be the first character of a password differs for the rule of which letters/characters may follow! Similarly, if we take the two_character_list, we can permute that (again/further) with each letter. Thus 'aaa, aab, aac, ...baa, bab, bac, ...'. Thus, len( three_character_list ) == 24 * 24 * 24 == 24 ** 3 == len( two_character_list ) * 24 == len( one_character_list ) * 24 * 24 Carrying-on we can build a "password cracking dictionary" of any length. NB the three-character_list contains exactly 'what it says on the tin' (English-English expression meaning that the label is entirely correct). Thus if the password rules say 'up to three characters in length', then the password-cracker would have to test each of the three lists in-turn too look for one-character passwords, then two-character passwords, and finally three...! Disclaimers: Permutations and combinations are an interesting [learning] exercise, and we're getting used to your preference for step-by-step experimentation and learning-mastery. That said, @David is correct - once understood, use the tool! Because these multiple loops are "expensive" (in terms of execution-time). Per @Chris and comments above, consider when and where to use character-strings, tuples, lists, and/or sets - for 'best results' and/or 'best practice'. Per @yourself, once you have coded, tested, and debugged a solution featuring explicit for-loops, it will be a valuable exercise to upgrade (the code and quite probably the execution speed) using comprehensions/expressions... Sample code: def permute_lists( existing_list:list, expansion_list:list )->list: '''Expand each of the existing list's elements with every element of a provided list. ''' permuted_list = list() for existing_element in existing_list: for expansion_element in expansion_list: permuted_list.append( existing_element + expansion_element ) return permuted_list # establish character-set to be used letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'w', 'q', 'y', 'z'] # taking each of the letters once is the character-set one_character_list = letters print( len( one_character_list ) ) print( one_character_list, end="\n" ) # taking the letters two at a time two_character_list = permute_lists( one_character_list, letters ) print( len( two_character_list ), len( one_character_list ) * len( letters ) ) print( two_character_list, end="\n" ) # taking the letters three at a time three_character_list = permute_lists( two_character_list, letters ) print( len( three_character_list ), len( two_character_list ) * len( letters ) ) print( three_character_list, end="\n" ) -- Regards =dn -- https://mail.python.org/mailman/listinfo/python-list