本文实例讲述了Python针对给定字符串求解所有子序列是否为回文序列的方法。分享给大家供大家参考,具体如下:
问题:
给定一个字符串,得到所有的子序列,判断是否为回文序列
思路:
对字符串遍历切片即可
下面是具体实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#!usr/bin/env python # -*- coding:utf-8 -*- ''''' __AUthor__:沂水寒城 功能:对指定字符串寻找所有回文子序列 ''' def is_huiwen(one_str_list): ''''' 输入一个字符串列表,判断是否为回文序列 ''' if len (one_str_list) = = 1 : return True else : half = len (one_str_list) / 2 if len (one_str_list) % 2 = = 0 : first_list = one_str_list[:half] second_list = one_str_list[half:] else : first_list = one_str_list[:half] second_list = one_str_list[half + 1 :] if first_list = = second_list[:: - 1 ]: return True else : return False def get_list_all_sub_list(num_list): ''' 输入一个列表,返回该列表所有的子列表,这里定义的空列表不属于子列表,故:子列表最小长度为1 ''' if len (num_list) = = 1 : return [num_list] sub_list = get_list_all_sub_list(num_list[: - 1 ]) extra = num_list[ - 1 :] temp_list = [] for one in sub_list: temp_list.append(one + extra) return sub_list + temp_list def slice_func(one_str): ''''' ''' result_list = [] for i in range ( 1 , len (one_str)): result_list.append(one_str[:i]) result_list.append(one_str[i:]) result_list + = list (one_str) result_list.append(one_str) return list ( set (result_list)) def main_func2(): ''''' 主调用函数 ''' str_list = [ 'abdc' , 'abba' ] for one_str in str_list: result_list = slice_func(one_str) print '-----------------------------------------------' for one in result_list: if is_huiwen( list (one)): print one + '是回文序列' def main_func1(): ''''' 主调用函数 ''' str_list = [ 'abdc' , 'abba' ] for one_str in str_list: one_str_list = list (one_str) one_all_sub_list = get_list_all_sub_list(one_str_list) print '------------------------------------------------' print one_all_sub_list for one in one_all_sub_list: if is_huiwen(one): print ' '.join(one)+' 是回文序列' if __name__ = = '__main__' : print "服务器之家测试结果:" main_func2() |
结果如下:
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://blog.csdn.net/together_cz/article/details/76615758