Select to view content in your preferred language

In Python 3, create a function that receives two strings and returns True.

364
1
12-06-2022 05:01 AM
MubashshirHasan
New Contributor II

Create a Python method called check anagram() that receives two strings and returns True if one of them is an anagram of the other. Otherwise, the function returns False.

If the two strings include repeated characters but none of the characters repeat at the same location, they are termed anagrams. The strings should all have the same length.

Wherever possible, use case-insensitive comparison.

Here's my code:

 

def check_anagram(data1,data2):
    first = data1.lower()
    second = data2.lower()

    d1 = []
    d2 = []

    for i in range(0, len(first)):
        d1.append(first[i])
    for i in range(0, len(second)):
        d2.append(second[i])

    for_check1 = sorted(d1)
    for_check2 = sorted(d2)
    if (for_check1 != for_check2):
        return False

    count = 0
    if (len(d1) == len(d2)):
        for i in d1:
            for j in d2:
                if(i == j):
                    a = d1.index(i)
                    b = d2.index(j)
                    if(a == b):
                        return False
                    else:
                        count += 1
    if(count == len(first)):
        return True
    else:
        return False

    print(check_anagram("Schoolmaster", "Theclassroom"))

 

Although this application returns relevant results for string values such as silence, listen, Moonstarrer, Astronomerapple, and mango, it does not return results for the aforementioned two strings (in code)

What instances am I overlooking in this code? How can this be fixed?

0 Kudos
1 Reply
JohannesLindner
MVP Frequent Contributor

I'm not sure what you're trying to do after line 18. Can you explain that?

 

To check if two strings are anagrams, all you need is in your lines 1 to 16.

  • convert each string into a list of characters
  • sort those lists
  • if the lists are equal, the strings are anagrams.

Instead of doing the conversion to list manually, you can just call list(your_string). To make it even easier, you can skip that step and call sorted(your_string).

 

So, this is all you need to check if two strings are anagrams:

def check_anagram(string_1, string_2):
    return sorted(string_1.lower()) == sorted(string_2.lower())

 

check_anagram("a", "b")
False
check_anagram("abc", "bca")
True
check_anagram("schoolmaster", "theclassroom")
True
check_anagram("Schoolmaster", "TheClassroom")
True
check_anagram("Schoolmaster", "The Classroom")
False

 

To get that last example to work, you need to ignore spaces:

def check_anagram(string_1, string_2, ignore_spaces=False):
    if ignore_spaces:
        string_1 = string_1.replace(" ", "")
        string_2 = string_2.replace(" ", "")
    return sorted(string_1.lower()) == sorted(string_2.lower())
check_anagram("Schoolmaster", "The Classroom")
False
check_anagram("Schoolmaster", "The Classroom", True)
True

Have a great day!
Johannes