Advertisement
Python253

example_compare_anagrams

May 13th, 2024
636
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 1 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Filename: example_compare_anagrams.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. This example script compares 2 strings and determines if they are anagrams of each other.
  9.  
  10. Requirements:
  11.    - Python 3.x
  12.    - The script requires the following Python modules:
  13.        - collections module with Counter class
  14.        - string module
  15.  
  16. Functions:
  17.    - clean_string(s): Removes punctuation, spaces, and newline characters from the input string.
  18.    - check_anagram(string1, string2): Checks if two strings are anagrams of each other.
  19.  
  20. Usage:
  21.    - Run the script and provide the two strings to compare when prompted.
  22.  
  23. Additional Notes:
  24.    - Anagrams are words or phrases formed by rearranging the letters of another word or phrase.
  25.    - This script ignores the case of the letters, spaces, punctuation, and newline characters.
  26.  
  27. Expected Example Output:
  28.  
  29.    String 1:
  30.       I pledge allegiance to the flag of the United States of America,
  31.       and to the republic for which it stands, one nation, under God,
  32.       indivisible, with liberty and justice for all!
  33.  
  34.    String 2:
  35.       I, George W. Bush, an evil Republican fascist,
  36.       used God to inflict pain on the world, end life,
  37.       facilitate death, create militant jihad rebels,
  38.       and to let youths die for nothing!
  39.  
  40.       String 1 & String 2 are anagrams!
  41. """
  42.  
  43. # Get Essential Imports
  44. from collections import Counter
  45. import string
  46.  
  47. # Function to clean the strings
  48. def clean_string(s):
  49.     """
  50.    Removes punctuation, spaces, and newline characters from the input string.
  51.  
  52.    Args:
  53.        s (str): The input string.
  54.  
  55.    Returns:
  56.        str: The cleaned string.
  57.    """
  58.     return s.translate(str.maketrans("", "", string.punctuation + " \n")).lower()
  59.  
  60. # Function to determine if two strings are anagrams of each other
  61. def check_anagram(string1, string2):
  62.     """
  63.    Checks if two strings are anagrams of each other.
  64.  
  65.    Args:
  66.        string1 (str): The first string.
  67.        string2 (str): The second string.
  68.  
  69.    Returns:
  70.        bool: True if the strings are anagrams, False otherwise.
  71.    """
  72.     return Counter(clean_string(string1)) == Counter(clean_string(string2))
  73.  
  74. if __name__ == "__main__":
  75.     """
  76.    Define String 1: The first string to compare
  77.    """
  78.     string1 = "  I pledge allegiance to the flag of the United States of America, \n   and to the republic for which it stands, one nation, under God,\n   indivisible, with liberty and justice for all!"
  79.     print("\nString 1:\n", string1)
  80.     """
  81.    Define String 2: The second string to compare
  82.    """
  83.     string2 = "  I, George W. Bush, an evil Republican fascist,\n   used God to inflict pain on the world, end life,\n   facilitate death, create militant jihad rebels,\n   and to let youths die for nothing!"
  84.     print("\nString 2:\n", string2)
  85.     """
  86.    Check if the strings are anagrams
  87.    """
  88.     if check_anagram(string1, string2):
  89.         """
  90.        Print if strings are anagrams
  91.        """
  92.         print("\n   String 1 & String 2 are anagrams!\n")
  93.     else:
  94.         """
  95.        Print if strings are not anagrams
  96.        """
  97.         print("\n   String 1 & String 2 are not anagrams!\n")
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement