Advertisement
Python253

ipp9_5_permutations

Jun 1st, 2024
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: ipp9_5_permutations.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script demonstrates "Chapter 4: Practice Project #5 - Permutations" from the book "Impractical Python Projects" by Lee Vaughan.
  10.    - It generates all possible unique arrangements of individual column numbers, including negative values for route direction.
  11.  
  12. Requirements:
  13.    - Python 3.x
  14.    - itertools module
  15.  
  16. Functions:
  17.    - main():
  18.        Executes the main functionality of the script.
  19.        
  20.        This function performs the following processes:
  21.        1. Generate all permutations of the input columns using the `itertools.permutations()` function.
  22.        2. Generate all sign combinations for each permutation, representing the route direction, using the `itertools.product()` function.
  23.        3. Combine permutations with sign combinations to create unique column combinations, considering both column order and direction.
  24.        4. Format the unique column combinations for alignment and readability.
  25.        5. Print the formatted unique column combinations to the console.
  26.        6. Print a summary of the total number of permutations and unique column combinations.
  27.        7. Prompt the user to decide whether to save the data to a file.
  28.        8. If the user chooses to save the data, write the formatted combinations and summary to a text file named 'permutations_list.txt'.
  29.  
  30. Usage:
  31.    - Run the script to generate and display unique column combinations, and optionally save the data to a file.
  32.  
  33. Additional Notes:
  34.    - The generated unique column combinations are displayed in a formatted manner.
  35.    - The data & summary is saved to the current working directory as: 'permutations_list.txt'.
  36. """
  37.  
  38. import itertools
  39.  
  40. def main():
  41.     # Initialize columns
  42.     cols = [1, 2, 3, 4]
  43.    
  44.     # Generate all permutations
  45.     permutations = list(itertools.permutations(cols))
  46.    
  47.     # Generate all sign combinations
  48.     sign_combinations = list(itertools.product([-1, 1], repeat=len(cols)))
  49.  
  50.     # Create a list to store unique column combinations
  51.     unique_combinations = []
  52.    
  53.     # Generate unique column combinations with directions
  54.     for perm in permutations:
  55.         for signs in sign_combinations:
  56.             signed_perm = [a * b for a, b in zip(signs, perm)]
  57.             unique_combinations.append(signed_perm)
  58.  
  59.     # Format the output for alignment
  60.     formatted_combinations = []
  61.     for combo in unique_combinations:
  62.         formatted_combinations.append(
  63.             "[{}]".format(", ".join(f"{n:2}" for n in combo))
  64.         )
  65.  
  66.     # Print the results
  67.     print(f"Columns: {cols}\n")
  68.     print("Unique Column Combinations (including route direction):\n")
  69.  
  70.     # Print formatted combinations with multiple permutations per line
  71.     per_line = 4
  72.     for i in range(0, len(formatted_combinations), per_line):
  73.         line_combinations = "  ".join(formatted_combinations[i:i + per_line])
  74.         print(line_combinations)
  75.  
  76.     # Print summary
  77.     print(f"\nSummary:")
  78.     print(f"Factorial of num_cols without negatives: {len(permutations)}")
  79.     print(f"Number of column combinations: {len(unique_combinations)}")
  80.  
  81.     # Ask the user if they want to save the data to a file
  82.     save_to_file = input("\nDo you want to save the data as 'permutations_list.txt'?\n\n1: Yes\n2: No\n\nMake your selection (1 or 2): ")
  83.     if save_to_file == '1':
  84.         with open('permutations_list.txt', 'w', encoding='UTF-8') as file:
  85.             file.write(f"Columns: {cols}\n\n")
  86.             file.write("Unique Column Combinations (including route direction):\n\n")
  87.             for i in range(0, len(formatted_combinations), per_line):
  88.                 line_combinations = "  ".join(formatted_combinations[i:i + per_line])
  89.                 file.write(line_combinations + '\n')
  90.             file.write(f"\nSummary:\n")
  91.             file.write(f"Factorial of num_cols without negatives: {len(permutations)}\n")
  92.             file.write(f"Number of column combinations: {len(unique_combinations)}\n")
  93.         print("Data has been saved to 'permutations_list.txt'.")
  94.  
  95. if __name__ == "__main__":
  96.     main()
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement