def check_palin(word): if word==word[::-1]: return True else: return False def all_palindromes(string): result=list() st1="" for i in range(0,len(string)): st1 += string[i] result.append(string[i]) for j in range(i+1,len(string)): st1+=string[j] if check_palin(st1)==True: result.append(st1) st1="" return list(set(result))
Month: April 2017
Counting consecutive characters in a string python 2
from itertools import groupby sm='aabccaaddbb' print [[k, len(list(g))] for k, g in groupby(sm)] output: [['a', 2], ['b', 1], ['c', 2], ['a', 2], ['d', 2], ['b', 2]]
All possible substrings of a string in Python 2
st="abcde" li=list() for i in range(0,len(st)): for j in range(i,len(st)): substring = "" for k in range(i,j+1): substring+=st[k] li.append(substring) print li output: ['a', 'ab', 'abc', 'abcd', 'abcde', 'b', 'bc', 'bcd', 'bcde', 'c', 'cd', 'cde', 'd', 'de', 'e']
Generating combination of a list in python 2
import itertools li,l= [1, 3, 3],2 for subset in itertools.combinations(li, l): print subset l is the length of subsequent of element from the input iterable
Check if all elements in a list are same(identical)
def checkAll_same(li): set1=set(li) if len(set1)==1: print "List Element Are Same" else: print "List Element Are Not Same" checkAll_same([1,1,1,1])