6

Python编程7——Python比较相同字母组成的单词

 3 years ago
source link: https://iphyer.github.io/blog/2013/01/13/pythonanagrams/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Python编程7——Python比较相同字母组成的单词

这个程序比较简单就是比较两个单词是不是可以用相同的字母不同的排序组合出来的。

#the programe compares that some word letters can 
#be reput as a new word

def areAnagrames(word1, word2):
	"Return the result of the comparison"
	#resorted the two words in the aplhabeta order
	word1_sorted=sorted(word1)
	word2_sorted=sorted(word2)
	return word1_sorted==word2_sorted
	
print "Anagram Test"
twowords=raw_input("Enter two words separing with whitespace: \n")
twowordsList=twowords.split()
word1=twowordsList[0]
word2=twowordsList[1]

if areAnagrames(word1,word2):
	print "The two words are anagrams."
else:
	print "The two words are not anagrams."

tu1

当然程序还有一处可以更改的,就是使用多重赋值替代两个赋值语句

word1,word2=twoWords.split()
Written on January 13, 2013

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK