Python获取一个字符串所有连续子串
2014-03-18 14:35
357 查看
获取一个字符串所有连续子串组成集合(set)的长度,居然是Facebook的interview题目,我也做出来了,哈哈:
不过感觉写的有点simple,问问stackoverflow:
还是这样写比较简单:
还有个朋友用yeild,没想到啊:
最后是关于simple的讨论:
"not so simple" - stop. Before you go any further, please understand that simple is good. Simple is simple to understand, simple to maintain, and simple to debug. No one is going to reject your code because it's too simple. The simpler you can make your code, the better.
看来我还是too young, too simple了...
def get_all_substrings(string): length = len(string) alist = [] for i in xrange(length): for j in xrange(i,length): alist.append(string[i:j + 1]) return alist print get_all_substring('abcde')
不过感觉写的有点simple,问问stackoverflow:
还是这样写比较简单:
def get_all_substrings_1(input_string): length = len(input_string) return [input_string[i:j + 1] for i in xrange(length) for j in xrange(i,length)]
还有个朋友用yeild,没想到啊:
def get_all_substrings(string): length = len(string) for i in xrange(length): for j in xrange(i + 1, length + 1): yield(string[i:j]) for i in get_all_substrings("abcde"): print i
最后是关于simple的讨论:
"not so simple" - stop. Before you go any further, please understand that simple is good. Simple is simple to understand, simple to maintain, and simple to debug. No one is going to reject your code because it's too simple. The simpler you can make your code, the better.
看来我还是too young, too simple了...
相关文章推荐
- 任意输入一个字符串,输出它的所有子串。Python练习(未考虑去重问题)
- 一个一万字长的连续字符串,找出其所有子串,并统计子串的出现次数
- 获取一个字符串的所有子串
- python 获取一个字符串所有大小写的情况
- 求一个字符串中连续出现次数最多的子串
- 求一个字符串中连续出现次数最多的子串
- 一个字符串中包含另一个字符串所有字符的最短子串
- Python Requests爬虫——获取一个收藏夹下所有答案的图片
- 求一个字符串中连续出现次数最多的子串
- 求一个字符串中连续出现次数最多的子串
- 一个字符串中包含另一个字符串所有字符的最短子串长度?——《编程之美》最短摘要的生成的简化
- python 删除字符串中的连续空格只保留一个
- python如何获取一个文件夹内包括子文件夹所有文件的名字和路径
- 寻找一个字符串中连续出现次数最多的子串
- 所谓微软面试题的解答:求一个字符串中连续出现次数最多的子串
- 求一个字符串中连续出现并且出现次数最多的子串.
- 求一个字符串s的最大连续递增数字子串
- 求一个字符串中连续出现次数最多的子串
- 求一个字符串中所有连续的整数和
- 求一个字符串中连续出现次数最多的子串