每日一程-19.检查输入的两个词是否构成变位词
作者:互联网
Author: Notus(hehe_xiao@qq.com)
Create: 2019-02-26
Update: 2019-02-26
检查输入的两个词是否构成变位词
环境
Python version: 3.7.1
代码如下(a.py)
'''
检查输入的两个词是否构成变位词, 即字母相同,顺序不同。
@Author: Notus(hehe_xiao@qq.com)
@Create: 2019-02-26
@Update: 2019-02-26
@Version: 0.1
'''
def areAnagram(word1, word2):
word1_sorted = sorted(word1)
word2_sorted = sorted(word2)
return word1_sorted == word2_sorted
twowords = input("输入两个由空格分隔的单词:")
word1, word2 = twowords.split(" ")
if areAnagram(word1, word2):
print("构成Anagram")
else:
print("不构成Anagram")
运行
C:\Users\Notus\Desktop>python a.py
输入两个由空格分隔的单词:word drow
构成Anagram
C:\Users\Notus\Desktop>python a.py
输入两个由空格分隔的单词:kkkk dddd
不构成Anagram
标签:02,26,变位,19,一程,2019,word1,word2,sorted 来源: https://www.cnblogs.com/leo1875/p/10440989.html