首页 > 编程语言 > 详细

Check iO:初学Python

时间:2014-07-09 21:54:43      阅读:1999      评论:0      收藏:0      [点我收藏+]

The end of other

For language training our Robots want to learn about suffixes.

In this task, you are given a set of words in lower case. Check whether there is a pair of words, such that one word is the end of another (a suffix of another). For example: {"hi", "hello", "lo"} -- "lo" is the end of "hello", so the result is True.

Hints: For this task you should know how to iterate through set types and string data type functions. Read more about set type here and string functions here.

Input: Words as a set of strings.

Output: True or False, as a boolean.

 

题目大义:给出一个含有单词的集合(set),如果某个单词是另一个单词的后缀,如"lo"是"hello"的后缀,则返回True,如果不存在则返回False。

 

一开始认为set可以使用index,就像数组般使用,可惜了,set并不支持。想想也合理,c++中的set也是不支持[]索引的,于是乎想到了for xx in xx形式,加上提示:str.endswith(suffix[, start[, end]]),恍然大悟,给出拙劣的Python代码

 

1 def checkio(words_set):
2     for words in words_set:
3         for other_words in words_set:
4             if other_words != words and other_words.endswith(words):
5                 return True
6     
7     return False

 

 

 

 

 

 

Check iO:初学Python,布布扣,bubuko.com

Check iO:初学Python

原文:http://www.cnblogs.com/hzhesi/p/3829967.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!