Advertisement
Python253

ipp6_0_gallic_gambit

Jun 1st, 2024
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: ipp6_0_gallic_gambit.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. Description:
  9.    - This script demonstrates "Chapter 3: Practice Project #6 - Finding Voldemort: The Gallic Gambit" from the book "Impractical Python Projects" by Lee Vaughan.
  10.    - 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.
  11.    
  12. Requirements:
  13.    - Python 3.x
  14.    - itertools module (specifically the permutations function)
  15.  
  16. Functions:
  17.    - main():
  18.        - Executes the main functionality of the script.
  19.          The steps are as follows:
  20.            1. Define the initial string of remaining letters after removing "I am Lord".
  21.            2. Identify the substring 'mort' within the remaining letters.
  22.            3. Remove 'mort' from the string if it exists.
  23.            4. Generate all permutations of the remaining letters.
  24.            5. Remove duplicates by converting the list of permutations to a set.
  25.            6. Sort and print each unique permutation.
  26.    
  27. Usage:
  28.    - Run the script directly in a Python 3.x environment:
  29.            
  30.            $ python ipp6_0_gallic_gambit.py
  31.    
  32. Additional Notes:
  33.    - This script assumes the reader has basic knowledge of Python, including working with strings and using modules.
  34.    - The itertools module is part of the Python Standard Library, so no additional installation is required.
  35. """
  36.  
  37. from itertools import permutations
  38.  
  39. def main():
  40.     """
  41.    Generates permutations from the letters remaining after removing 'I am Lord'
  42.    and 'mort' from 'Tom Marvolo Riddle'.
  43.  
  44.    This function demonstrates string manipulation and the use of the
  45.    itertools.permutations function to find meaningful anagrams, similar to how
  46.    Tom Riddle might have created the name 'Voldemort'.
  47.    
  48.    The approach illustrates the application of combinatorial methods in Python
  49.    to solve anagram puzzles with a creative twist.
  50.    """
  51.     # Initial letters after removing "I am Lord"
  52.     remaining_letters = 'tmvoordle'
  53.  
  54.     # Finding 'mort' in the remaining letters
  55.     mort_index = remaining_letters.find('mort')
  56.     if mort_index != -1:
  57.         remaining_letters = remaining_letters.replace('mort', '', 1)
  58.  
  59.     # Remaining letters after removing 'mort'
  60.     remaining_letters = 'vdle'
  61.  
  62.     # Generate permutations of the remaining letters
  63.     perms = [''.join(p) for p in permutations(remaining_letters)]
  64.     unique_perms = set(perms)
  65.  
  66.     # Print description of the script
  67.     print("_" * 100)
  68.     print("\n\t  :: Chapter 3: Practice Project #6 - Finding Voldemort: The Gallic Gambit ::")
  69.     print("_" * 100)
  70.     print("\n- This script generates permutations from the letters 'vdle'.\n"
  71.           "- These letters are derived from the name 'Tom Marvolo Riddle' after removing 'I am Lord' & 'mort'.\n\n"
  72.           "\t\t\t   Below is the list of 24 unique permutations:\n")
  73.  
  74.  
  75.     # Print permutations
  76.     for i, perm in enumerate(sorted(unique_perms), start=1):
  77.         print(f"\t\t\t\t\t   {i:02}. {perm}")
  78.    
  79.     # Closing statement
  80.     print("_" * 100)
  81.     print("\n   This concludes the demonstration of generating permutations for the "
  82.           "given letters using Python.\n\n\t\t\t   Thank you for your attention...   Goodbye!")
  83.     print("_" * 100)
  84.  
  85. if __name__ == "__main__":
  86.     main()
  87.  
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement