Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: example_compare_anagrams.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- This example script compares 2 strings and determines if they are anagrams of each other.
- Requirements:
- - Python 3.x
- - The script requires the following Python modules:
- - collections module with Counter class
- - string module
- Functions:
- - clean_string(s): Removes punctuation, spaces, and newline characters from the input string.
- - check_anagram(string1, string2): Checks if two strings are anagrams of each other.
- Usage:
- - Run the script and provide the two strings to compare when prompted.
- Additional Notes:
- - Anagrams are words or phrases formed by rearranging the letters of another word or phrase.
- - This script ignores the case of the letters, spaces, punctuation, and newline characters.
- Expected Example Output:
- String 1:
- I pledge allegiance to the flag of the United States of America,
- and to the republic for which it stands, one nation, under God,
- indivisible, with liberty and justice for all!
- String 2:
- I, George W. Bush, an evil Republican fascist,
- used God to inflict pain on the world, end life,
- facilitate death, create militant jihad rebels,
- and to let youths die for nothing!
- String 1 & String 2 are anagrams!
- """
- # Get Essential Imports
- from collections import Counter
- import string
- # Function to clean the strings
- def clean_string(s):
- """
- Removes punctuation, spaces, and newline characters from the input string.
- Args:
- s (str): The input string.
- Returns:
- str: The cleaned string.
- """
- return s.translate(str.maketrans("", "", string.punctuation + " \n")).lower()
- # Function to determine if two strings are anagrams of each other
- def check_anagram(string1, string2):
- """
- Checks if two strings are anagrams of each other.
- Args:
- string1 (str): The first string.
- string2 (str): The second string.
- Returns:
- bool: True if the strings are anagrams, False otherwise.
- """
- return Counter(clean_string(string1)) == Counter(clean_string(string2))
- if __name__ == "__main__":
- """
- Define String 1: The first string to compare
- """
- 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!"
- print("\nString 1:\n", string1)
- """
- Define String 2: The second string to compare
- """
- 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!"
- print("\nString 2:\n", string2)
- """
- Check if the strings are anagrams
- """
- if check_anagram(string1, string2):
- """
- Print if strings are anagrams
- """
- print("\n String 1 & String 2 are anagrams!\n")
- else:
- """
- Print if strings are not anagrams
- """
- print("\n String 1 & String 2 are not anagrams!\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement