Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: ipp9_5_permutations.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script demonstrates "Chapter 4: Practice Project #5 - Permutations" from the book "Impractical Python Projects" by Lee Vaughan.
- - It generates all possible unique arrangements of individual column numbers, including negative values for route direction.
- Requirements:
- - Python 3.x
- - itertools module
- Functions:
- - main():
- Executes the main functionality of the script.
- This function performs the following processes:
- 1. Generate all permutations of the input columns using the `itertools.permutations()` function.
- 2. Generate all sign combinations for each permutation, representing the route direction, using the `itertools.product()` function.
- 3. Combine permutations with sign combinations to create unique column combinations, considering both column order and direction.
- 4. Format the unique column combinations for alignment and readability.
- 5. Print the formatted unique column combinations to the console.
- 6. Print a summary of the total number of permutations and unique column combinations.
- 7. Prompt the user to decide whether to save the data to a file.
- 8. If the user chooses to save the data, write the formatted combinations and summary to a text file named 'permutations_list.txt'.
- Usage:
- - Run the script to generate and display unique column combinations, and optionally save the data to a file.
- Additional Notes:
- - The generated unique column combinations are displayed in a formatted manner.
- - The data & summary is saved to the current working directory as: 'permutations_list.txt'.
- """
- import itertools
- def main():
- # Initialize columns
- cols = [1, 2, 3, 4]
- # Generate all permutations
- permutations = list(itertools.permutations(cols))
- # Generate all sign combinations
- sign_combinations = list(itertools.product([-1, 1], repeat=len(cols)))
- # Create a list to store unique column combinations
- unique_combinations = []
- # Generate unique column combinations with directions
- for perm in permutations:
- for signs in sign_combinations:
- signed_perm = [a * b for a, b in zip(signs, perm)]
- unique_combinations.append(signed_perm)
- # Format the output for alignment
- formatted_combinations = []
- for combo in unique_combinations:
- formatted_combinations.append(
- "[{}]".format(", ".join(f"{n:2}" for n in combo))
- )
- # Print the results
- print(f"Columns: {cols}\n")
- print("Unique Column Combinations (including route direction):\n")
- # Print formatted combinations with multiple permutations per line
- per_line = 4
- for i in range(0, len(formatted_combinations), per_line):
- line_combinations = " ".join(formatted_combinations[i:i + per_line])
- print(line_combinations)
- # Print summary
- print(f"\nSummary:")
- print(f"Factorial of num_cols without negatives: {len(permutations)}")
- print(f"Number of column combinations: {len(unique_combinations)}")
- # Ask the user if they want to save the data to a file
- 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): ")
- if save_to_file == '1':
- with open('permutations_list.txt', 'w', encoding='UTF-8') as file:
- file.write(f"Columns: {cols}\n\n")
- file.write("Unique Column Combinations (including route direction):\n\n")
- for i in range(0, len(formatted_combinations), per_line):
- line_combinations = " ".join(formatted_combinations[i:i + per_line])
- file.write(line_combinations + '\n')
- file.write(f"\nSummary:\n")
- file.write(f"Factorial of num_cols without negatives: {len(permutations)}\n")
- file.write(f"Number of column combinations: {len(unique_combinations)}\n")
- print("Data has been saved to 'permutations_list.txt'.")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement