Python method for solving a palindrome sequence of all sub-sequences for a given string

Source: Internet
Author: User
This article mainly describes python for a given string to solve all sub-sequences is a palindrome sequence of methods, involving Python for string traversal, judgment, operations related operations skills, the need for friends can refer to the next

This example describes how Python solves all sub-sequences as palindrome sequences for a given string. Share to everyone for your reference, as follows:

Problem:

Given a string, get all the sub-sequences and determine if they are palindrome sequences

Ideas:

Traversing a slice of a string can

Here are the specific implementations:

#!usr/bin/env python#-*-coding:utf-8-*-"" __author__: Yishui Cold City function: Find all palindrome subsequence ' def is_huiwen (one_str_list) for the specified string: "" Enter a list of strings to determine if the palindrome sequence ' If Len ' (one_str_list) ==1:return True Else:half=len (one_str_list)/2 If Len (one_str_lis T)%2==0:first_list=one_str_list[:half] Second_list=one_str_list[half:] Else:first_list=one_str_list[:h ALF] Second_list=one_str_list[half+1:] If first_list==second_list[::-1]: return True Else:return Fal Sedef get_list_all_sub_list (num_list): "Enter a list, return all the sub-lists of the list, the empty list defined here does not belong to the sub-list, so: the minimum length of the sub-list is 1" If Len (num_list) ==1:r    Eturn [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_listdef Slice_func (ONE_STR): "" "" result_list=["for I in Ran GE (1,len (one_str)): Result_list.append (One_str[:i]) result_list.append (one_str[i:]) result_list+=list (ONE_STR) Res  Ult_list.append (ONE_STR)Return list (set (result_list)) def MAIN_FUNC2 (): "'" ' Keynote with function ' ' str_list=[' ABDC ', ' ABBA '] for ONE_STR in Str_list:r      Esult_list=slice_func (one_str) print '-----------------------------------------------' for one in Result_list: If Is_huiwen (list one): Print one+ ' is a palindrome sequence ' Def main_func1 (): ' ' "' Keynote with function ' ' str_list=[' ABDC ', ' ABBA '] for one_s TR 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) + ' is a palindrome sequence ' if __name__ = = ' __main__ ': print "script home test Result:" Main_func2 ()

The results are as follows:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.