-6

I have tried this, i want the program to compare every letter in S1 with every letter in S2, any ideas?

s1= "Army"
s2= "Mary"
def anagram(S1,S2):
    q=0
    w=0
    S1.lower()
    S2.lower()
    for i in S1:
        if S1[q]==S2[w]:
            print S1[q]
        else:
            q+=1
            w+=1

anagram(s1,s2)
Riko
  • 19
  • 3
  • There are a couple of questions on StackOverflow that talk about how to compare two strings and find out if they are anagrams. Have you looked at those first? Also do you understand why your code isn't working as expected? – aug May 05 '17 at 00:26
  • "any ideas?" doesn't make a good question. Is there a problem with your code? Then state it. If not, then don't post. [ask] – Julien May 05 '17 at 00:26
  • @Riko I'm not aggressive I'm just telling you what is acceptable on SO. If you prefer I can shut up and let you post bad questions that will keep being down voted... :) – Julien May 05 '17 at 00:30
  • Simple idea: sort the 2 string, and compare - If they are anagrams, they will be equal def anagram(s1,s2): return ''.join(sorted(s1.lower())) == ''.join(sorted(s2.lower())) – Theo May 05 '17 at 00:34
  • what you could do is check length then look through and take index all the letters then your good. – thesonyman101 May 05 '17 at 00:34

1 Answers1

1

Consider using collections.counter:

import collections

s1 = "Army"
s2 = "Mary"
s3 = "Zary"

def anagram(s1,s2):
  return collections.Counter(s1.lower()) == collections.Counter(s2.lower())

print anagram(s1,s2) # True
print anagram(s1, s3) # False

Try it here!

Sash Sinha
  • 18,743
  • 3
  • 23
  • 40