python编程题目,会的帮帮忙

A string is an anagram of another string if they both have the same
characters, but they do not appear in exactly the same order. Define a function
anagrams(string1,string2) that takes two strings and returns True if they are anagrams,
and False otherwise. You can use the in and not in operators to do this problem
>>>anagrams("cat","cata")
False
>>>anagrams("cat","cat")
False
>>>anagrams("cat","aat")
False
>>>anagrams("cat","act")
True
>>>anagrams("staple","plates")
True
>>>anagrams("leap","pale")
True

#!/usr/bin/env python
def anagrams(s1,s2):
if s1==s2:
return False
else:
a=list(s1)
b=list(s2)
if sorted(a)==sorted(b):
return True
else:
return False

str1=raw_input('input str1')
str2=raw_input('input str2')
print('%s'%anagrams(str1,str2))
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-10-07
def anagrams(str1, str2):
if str1 == str2:
return False
if all([char1 in str2 for char1 in list(str1)]):
return True
else:
return False

if __name__ == "__main__":

print(anagrams("abc", "abc"))
print(anagrams("abc", "cba"))
print(anagrams("abc", "abd"))
相似回答
大家正在搜