0

I would like to know how I could return every output of the list crack into a Text file

For example if I get:

abandon able ability

I would to have this (what I call output of crack) output in a text file. And so on

def generate(arr, i, s, len):

    # base case
    if (i == 0): # when len has
                # been reached
    
        # print it out
        print(s)
        return
    
    # iterate through the array
    for j in range(0, len):

        # Create new string with
        # next character Call
        # generate again until
        # string has reached its len
        appended = s + arr[j]
        generate(arr, i - 1, appended, len)

    return

# function to generate
# all possible passwords
def crack(arr, len):

    # call for all required lengths
    for i in range(3 , 5):
        generate(arr, i, "", len)
    
# Driver Code
arr = ["abandon ","ability ","able ","about ","above ","absent "]
len = len(arr)
crack(arr, len)
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Briac W.G
  • 3
  • 2
  • Nothing here looks like it writes to a text file. Perhaps you can learn how to open/write to files separately, then add that here? – OneCricketeer Mar 03 '22 at 22:03
  • 1
    Does this answer your question? [How to generate all permutations of a list?](https://stackoverflow.com/questions/104420/how-to-generate-all-permutations-of-a-list) – ti7 Mar 03 '22 at 22:06
  • Thats why I am asking how can I write the output to a txt file? – Briac W.G Mar 03 '22 at 22:32

1 Answers1

0

Demonstration of permutation vs combination of 3 items taken from a group of 5 items

In math the words combination and permutation have specific and different meanings. for 2048 items taken 12 at a time:

  1. permutation means the order of the 12 items is significant and the results will be 5.271537971E+39 items.
  2. combination means the order of the 12 items is not significant and the results will be '1.100526171E+31' items.
  3. Both combination and permutation are easy to program in python using the `itertools' library. But, either program will take a long time to run.
import itertools

lower_case_letters = ['a', 'b', 'c', 'd', 'e']

print(' Permutations '.center(80, '*'))
permutations = []
sample_size = 3
for unique_sample in itertools.permutations(lower_case_letters, sample_size):
    permutations.append(unique_sample)
    print(f'{len(permutations)=} {unique_sample=}')

f = open('permutations_results.txt', 'w')
for permutation in permutations:
    f.write(", ".join(permutation) + '\n')
f.close()

print(' Combinations '.center(80, '*'))
combinations = []
for unique_sample in itertools.combinations(lower_case_letters, sample_size):
    combinations.append(unique_sample)
    print(f'{len(combinations)=} {unique_sample=}')

f = open('combinations_results.txt', 'w')
for combination in combinations:
    f.write(", ".join(combination) + '\n')
f.close()
Carl_M
  • 861
  • 1
  • 7
  • 14
  • Hello thanks for submitting this answer I really appreciate it. But how come it prints None? Basically what i what to do is basically to print what happens in the terminal in side the text file. Can you please help me out? Thank you – Briac W.G Mar 04 '22 at 16:00
  • @BriacW.G, None was appearing because of len in the line output = crack(arr, len). The answer has been modified. – Carl_M Mar 04 '22 at 18:05
  • Thank you but this is answering to my question at 75%. How can it print all possible combinations in the text file? For example when I execute the following command I would like to have the following in my text file: ability abandon about above, ability abandon about absent, ability abandon above abandon, ability abandon above ability, ability abandon above able, ability abandon above about, Can you please help me? Thank you – Briac W.G Mar 04 '22 at 23:40
  • @BriacW.G, Is this closer to what you are after? I did not read input from a file yet, but that can be added. – Carl_M Mar 05 '22 at 01:04
  • Wait so how can I add a parameterof a max length of 12 words? if i have 13 words in the list? – Briac W.G Mar 05 '22 at 02:02
  • One way would be to use a random selection of 12 of the 13 words. – Carl_M Mar 05 '22 at 02:24
  • Which means? in python? – Briac W.G Mar 05 '22 at 02:35
  • There are various ways to do it with the capability of the random library. – Carl_M Mar 05 '22 at 02:42
  • Modified the answer to use a random 5 of the 6 words in the list. – Carl_M Mar 05 '22 at 03:03
  • But I dont think I am explaining the right thing. I have a list of 2048 predefined words each word is between "" and I want all possible combinations of these 2048 words in a 12 length of words max. So it would print in crack.txt all possible combinations with repetition of a 12 word max length. Do you see what I mean? – Briac W.G Mar 05 '22 at 03:23
  • With 12 values at a time selected from just 144 values there can be 49,633,807,532,904,947,942,162,432 unique sets. 12 values at a time selected from 2,048 values will produce greater than 5,444,517,870,735,015,415,413,993,718,908,291,383,296 sets. – Carl_M Mar 05 '22 at 04:33
  • Yes thats I am trying to print in the text file, all possible combinations from a 2048 word list of 12 word length max. So do you have any idea of how to generate that? – Briac W.G Mar 05 '22 at 15:28
  • In math the words combination and permutation have specific and different meanings. for 2048 items taken 12 at a time: `permutation` means the order of the 12 items is significant and the results will be `5.271537971E+39` items; `combination` means the order of the 12 items is not significant and the results will be '1.100526171E+31' items. Both combination and permutation are easy to program in python using the `itertools' library. But, either program will take a long time to run. – Carl_M Mar 05 '22 at 18:05
  • The answer has been revised to demonstrate combinations and permutations. – Carl_M Mar 05 '22 at 18:24
  • Thank you so much. I would like to know what hardware configuration is required approximately to run this code to get these results! Do you have any idea? – Briac W.G Mar 06 '22 at 03:32
  • Time to process 2048 items taken 12 at a time is determined software and hardware. The examples in the answer were done with `python` and the `iterable` library. `pandas` is a 3rd party library. It might be faster see: [Create a dataframe of permutations in pandas from list](https://stackoverflow.com/questions/45672342/create-a-dataframe-of-permutations-in-pandas-from-list). Hardware: how many cpus are used. See: [Speed-up your Pandas Workflow by changing a single line of code](https://towardsdatascience.com/speed-up-your-pandas-workflow-by-changing-a-single-line-of-code-11dfd85efcfb). – Carl_M Mar 06 '22 at 17:36