Python编程7——Python比较相同字母组成的单词
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.
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."
当然程序还有一处可以更改的,就是使用多重赋值替代两个赋值语句
word1,word2=twoWords.split()
Recommend
-
52
-
7
前言 现代计算机通常由 CPU ,以及主板、内存、硬盘等主要硬件结构组成,而决定计算机性能的最核心部件是 CPU +内存, CPU 负责处理程序指令,内存负责存储指令执行结果。在这个工...
-
5
V2EX › Python 请教一个 Python 正则问题,相同代码运行出来结果不一样了 karlxu · 1 天前 ·...
-
4
Python 代码阅读合集介绍:为什么不推荐Python初学者直接看...
-
4
通过 for 循环,比较 Python 与 Ruby 编程思想的差别 2 作者:Doug Turnbull 译者:豌豆花下猫 @Python 猫 原文:
-
5
一、通过程序看现象 在开始为大家讲解Java 多线程缓存模型之前,我们先看下面的这一段代码。这段代码的逻辑很简单:主线程启动了两个子线程,一个线程1、一个线程2。线程1先执行,sleep睡眠2秒钟之后线程2执行。两个线程使用到了一个...
-
5
一、相似之处:Lock锁 vs Synchronized 代码块 Lock锁是一种类似于synchronized 同步代码块的线程同步机制。从Java 5开始java.util.concurrent.locks引入了若干个Lock锁的实现类,所以通常情况...
-
6
#yyds干货盘点# leetcode算法题:连接两字母单词得到的最长回文串 精选 原创 灰太狼_cxh...
-
6
寻找一款念英文字母能识别为单词,并翻译为中文的 APP 或小程序 V2EX › 问与...
-
2
Python中让单词第一个字母大写的函数 Python中函数capitalize_words():它对字符串中的所有单词...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK