Python method to find out whether all subsequences are palindrome sequences for a given string

不言
Release: 2018-04-21 15:19:30
Original
2074 people have browsed it

This article mainly introduces Python's method for determining whether all subsequences are palindrome sequences for a given string. It involves Python's operating skills related to string traversal, judgment, and operation. Friends who need it can refer to it

The example in this article describes Python's method of determining whether all subsequences are palindrome sequences for a given string. Share it with everyone for your reference. The details are as follows:

Question:

Given a string, get all the subsequences and determine whether For the palindrome sequence

Idea:

Just traverse the slices of the string

The following It is a specific implementation:

#!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()
Copy after login

The results are as follows:

Related recommendations:

How to convert a python string into a two-dimensional array

##

The above is the detailed content of Python method to find out whether all subsequences are palindrome sequences for a given string. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template