Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: ipp6_0_gallic_gambit.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script demonstrates "Chapter 3: Practice Project #6 - Finding Voldemort: The Gallic Gambit" from the book "Impractical Python Projects" by Lee Vaughan.
- - The script uses the concept of finding anagrams to decode the name "Voldemort" by removing the letters corresponding to "I am Lord" and leveraging French knowledge.
- Requirements:
- - Python 3.x
- - itertools module (specifically the permutations function)
- Functions:
- - main():
- - Executes the main functionality of the script.
- The steps are as follows:
- 1. Define the initial string of remaining letters after removing "I am Lord".
- 2. Identify the substring 'mort' within the remaining letters.
- 3. Remove 'mort' from the string if it exists.
- 4. Generate all permutations of the remaining letters.
- 5. Remove duplicates by converting the list of permutations to a set.
- 6. Sort and print each unique permutation.
- Usage:
- - Run the script directly in a Python 3.x environment:
- $ python ipp6_0_gallic_gambit.py
- Additional Notes:
- - This script assumes the reader has basic knowledge of Python, including working with strings and using modules.
- - The itertools module is part of the Python Standard Library, so no additional installation is required.
- """
- from itertools import permutations
- def main():
- """
- Generates permutations from the letters remaining after removing 'I am Lord'
- and 'mort' from 'Tom Marvolo Riddle'.
- This function demonstrates string manipulation and the use of the
- itertools.permutations function to find meaningful anagrams, similar to how
- Tom Riddle might have created the name 'Voldemort'.
- The approach illustrates the application of combinatorial methods in Python
- to solve anagram puzzles with a creative twist.
- """
- # Initial letters after removing "I am Lord"
- remaining_letters = 'tmvoordle'
- # Finding 'mort' in the remaining letters
- mort_index = remaining_letters.find('mort')
- if mort_index != -1:
- remaining_letters = remaining_letters.replace('mort', '', 1)
- # Remaining letters after removing 'mort'
- remaining_letters = 'vdle'
- # Generate permutations of the remaining letters
- perms = [''.join(p) for p in permutations(remaining_letters)]
- unique_perms = set(perms)
- # Print description of the script
- print("_" * 100)
- print("\n\t :: Chapter 3: Practice Project #6 - Finding Voldemort: The Gallic Gambit ::")
- print("_" * 100)
- print("\n- This script generates permutations from the letters 'vdle'.\n"
- "- These letters are derived from the name 'Tom Marvolo Riddle' after removing 'I am Lord' & 'mort'.\n\n"
- "\t\t\t Below is the list of 24 unique permutations:\n")
- # Print permutations
- for i, perm in enumerate(sorted(unique_perms), start=1):
- print(f"\t\t\t\t\t {i:02}. {perm}")
- # Closing statement
- print("_" * 100)
- print("\n This concludes the demonstration of generating permutations for the "
- "given letters using Python.\n\n\t\t\t Thank you for your attention... Goodbye!")
- print("_" * 100)
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement