0

I am thinking to create a loop that allows me to output different values. By that I mean something like this:

list = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 
117, 118, 119, 120, 121, 122]

for i in range(0,len(list)):
    n = list[i]
    con_1 = chr(n)

    for i in range(0,len(list)):
        n = list[i]
        con_2 = chr(n)

        for i in range(0,len(list)):
            n = list[i]
            con_3 = chr(n)

            for i in range(0,len(list)):
                n = list[i]
                con_4 = chr(n)

                for i in range(0,len(list)):
                    n = list[i]
                    con_5 = chr(n)

                    for i in range(0,len(list)):
                        n = list[i]
                        con_6 = chr(n)

                        for i in range(0,len(list)):
                            n = list[i]
                            con_7 = chr(n)

                            for i in range(0,len(list)):
                                n = list[i]
                                con_8 = chr(n)

                                con = con_1 + con_2 +con_3 + con_4 + con_5 + con_6 + con_7 + con_8
                                
                                f = open("wifi_8sf.txt","a+")
                                f.write(con+"\n")
                                f.close()
                                print(con)

This is a code I made for an 8 digit wifi decoder (I know it is the least efficient way to crack the code but just doing it for practice).

However, this is only 8 digit and it takes that long, imagine 50 digit or anything above.

So I am thinking a way to achieving it is by using globals(), for example, I need 50 digits of output, so I will use globals() to define 50 different variables, and each of them going to loop as individual and add up as a string in the end.

According to this idea, anyone got any way to solve it?

jpf
  • 1,447
  • 12
  • 22
  • 5
    Don't do that. Even if you could get it working, it would be a disaster. Look into `itertools.permutations` instead. – Carcigenicate Aug 02 '20 at 17:16
  • Your first statement can be simplified to: `lst = list(range(48, 123))`. In programming, if you are doing something repetitive, there is probably a better way of doing it. – Asocia Aug 02 '20 at 17:18
  • Take a look at [this answer](https://stackoverflow.com/a/4852666/6212294) about `big-o` concept and how to obtain it and then optimize it in an algorithm. – SEYED BABAK ASHRAFI Aug 02 '20 at 17:27

1 Answers1

0

As mentioned in the comments you could use the itertools module to achieve the result of the above eight hard-coded loops. That would enable you to have a variable number of alphanumerics in the constructed string and make your code way more readable.

See below:

import itertools

my_list = range(48,58) + range(65,123)

number_of_digits = 8

cartesian_product = itertools.product(my_list, repeat=number_of_digits)

for prod in cartesian_product:

    con = ""
    for digit in prod:
        con += chr(digit)

    print(con)
    # and do other things you want with con

Also apart from the above solution two more things:

  1. This fragment from your code:
for in range(0, len(list)):
    n = list[i]
    con_1 = chr(n)

could be replaced by the more readable and pythonic:

for i in list:
    con_1 = chr(i)
  1. You open and close a file in order to write to it in every iteration, which isn't efficient. To speed things up you could at least open it once before the iteration loop and close it after you finish.
Ioannis Lalopoulos
  • 1,491
  • 1
  • 12
  • 20
  • 1
    The OP's problem is not about permutations; there is no restriction that the different letters have to be distinct. – kaya3 Aug 02 '20 at 17:51
  • @kaya3 you are right - I have updated the code to use the cartesian product in order to solve the OP's problem. – Ioannis Lalopoulos Aug 02 '20 at 18:10