Advertisement
Stingerwasp

Variations with repetition

May 29th, 2023 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.59 KB | Software | 0 0
  1. import itertools
  2.  
  3. def generate_variations(string, length):
  4.     variations = [''.join(comb) for comb in itertools.product(string, repeat=length)]
  5.     return variations
  6.  
  7. def write_variations_to_file(variations):
  8.     with open('output.txt', 'w') as file:
  9.         for variation in variations:
  10.             file.write(variation + '\n')
  11.  
  12. # Example usage
  13. string = input("Enter a string of characters: ")
  14. length = int(input("Enter the length: "))
  15.  
  16. variations = generate_variations(string, length)
  17. write_variations_to_file(variations)
  18.  
  19. print("Variations have been written to 'output.txt'.")
  20.  
Tags: variations
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement