2

How can I write a function that checks whether 2 inputs are anagrams?

word1 = input("Enter a string: ")
word2 = input("Enter a second string: ")

def isAnagram(s1, s2):
    s1 = word1.sort()
    s2 = word2.sort()
    if s1 == s2:
       print("This is an anagram")
    else:
       print("This is not an anagram)
isAnagram()
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Whoo Cares
  • 141
  • 1
  • 4
  • 11
  • 3
    1. I think you need to google the definition of anagram. 2. This is a zillionth question here about an anagram program in python and I'm sure you'll find more than enough hints around. 3. SO is for specific questions, not for "fix my code" ones. – sashkello Nov 13 '13 at 02:41
  • 1
    4. You never accepted any answers to your questions. This makes people yet more reluctant to answer. If you found what you were looking for, accept the answer and/or upvote other helpful answers. – sashkello Nov 13 '13 at 02:49
  • 2
    @sashkello: He seems to understand anagrams just fine. His algorithm is correct, and even reasonably efficient. He just doesn't understand some basic Python stuff. – abarnert Nov 13 '13 at 02:50
  • @abarnert Yep, I see it now, misread code at first. Basic search skills would come useful though, here is what I found within 20 seconds: http://stackoverflow.com/questions/14990725/checking-strings-against-each-other-anagrams/14990938#14990938 – sashkello Nov 13 '13 at 02:52
  • Thanks everybody. @sashkello you're right, I did a little more googling and found some more help. I think I was just frustrated that I couldn't figure this out. – Whoo Cares Nov 13 '13 at 03:13
  • 2
    The very basic idea to solve anagram is to sort both string than equate them. If they are equal than its an anagram otherwise not. A simple python implementation can be found http://jee-appy.blogspot.com/2016/06/python-anagram-program.html – Ajeet Khan Jun 26 '16 at 22:49
  • This question should be a duplicate of https://stackoverflow.com/questions/14990725. – Karl Knechtel Aug 01 '22 at 22:26

3 Answers3

4

You've defined the function almost correctly, but there are a few problems.

First you're asking for s1 and s2 as parameters. That's good. Now use those values, not the globals word1 and word2.

Second, if those values are strings, you can't call sort on them, because strings don't have a sort method. But you can call the sorted function on any sequence, even strings.

Third, there's a simple typo, a missing " on the second print.

It might be better to return a True or False value, and put the print outside the function, but let's leave that for now.

Putting that together, here's a working function:

def isAnagram(s1, s2):
    s1 = sorted(s1)
    s2 = sorted(s2)
    if s1 == s2:
       print("This is an anagram")
    else:
       print("This is not an anagram")

But now, you have to call the function properly, too. You've defined the function to take two parameters, s1 and s2. That means you need to call the function with two arguments.

So, where do you get those arguments? Well, you've already got those variables word1 and word2 sitting around, and they seem like exactly what you want.

So, change the last line to:

isAnagram(word1, word2)

And you're done.

abarnert
  • 354,177
  • 51
  • 601
  • 671
1

Your approach to finding anagrams is perfectly reasonable. Sorting the words and comparing them is the easiest way to find if two words are anagrams of one another.

However, I think you're confused by the idea of function parameters. When you define

foo(x1, x2)

when foo is called, it is expected to be called with 2 parameters. You define

anagram(s1, s2)

but never supply it with s1 and s2. The parameter list is not a list of variable names that you use in the function -- you can assign new variables at will. Instead, it is the list of inputs that the function takes.

so, anagram() is incorrect. You need to call anagram(input1, input2). (Assuming you don't have default values, which I won't get into.

def isAnagram(s1, s2):
    sortedWord1 = sorted(s1) # s1 is word1! (sorted instead of sort, strings are immutable)
    #what should you do here?
    if sortedWord1 == sortedWord2:
       print("This is an anagram")
    else:
       print("This is not an anagram") # you forgot a closing quote!

word1 = input("Enter a string: ")
word2 = input("Enter a second string: ")

isAnagram(word1, word2)

I changed your code very slightly such that it should do the right thing. I recommend that you read up on functions a bit more before you continue, though.

Think of them just like functions in math! f(x) is meaningful, f, while still meaningful, is probably not what you were looking for.

>>> isAnagram("anagram", "nagaram")
This is an an anagram
>>> isAnagram("anagram", "woohoo")
This is not an anagram
>>> isAnagram("a", "a")
This is an an anagram
vroomfondel
  • 3,056
  • 1
  • 21
  • 32
1

You've got the right idea, but since word1 and word2 are strings, they don't have the .sort() attribute. You can still sort the letters within that string:

>>> w='hello'
>>> sorted(w)
['e', 'h', 'l', 'l', 'o']

The result of sorted() is a list, we can turn it back to a string my joining them using an empty string:

>>> ''.join(sorted(w))
'ehllo'

Armed with this knowledge, your program might look something like:

word1 = input("Enter a string: ")
word2 = input("Enter a second string: ")

def isAnagram(s1, s2):
    s1 = ''.join(sorted(word1))
    s2 = ''.join(sorted(word2))
    if s1 == s2:
       print("This is an anagram")
    else:
       print("This is not an anagram")

isAnagram(word1, word2)
Hai Vu
  • 37,849
  • 11
  • 66
  • 93