Check iO:初学Python

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

上一篇:C语言中调用Lua


下一篇:Python编程学习,高效求解素数程序实例