Last active
August 29, 2015 14:13
-
-
Save dorwardv/04c0dd717b4fbad90517 to your computer and use it in GitHub Desktop.
palindrome.py all subsets of the characters in a string, palindrome2.py substrings of a string only, input.txt sample input
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 | |
a | |
b | |
c | |
aba | |
baa | |
aabaa | |
hellolleh | |
habibah | |
zzzz | |
aaaga |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from sets import Set | |
def is_palindrome(str): | |
return str == str[::-1] | |
def generate_substrings(str1): | |
substrings=[] | |
for start in range(len(str1)+1): | |
for end in range (len(str1)+1): | |
if start > end: | |
continue | |
elif (str1[start:end] != ''): | |
substrings.append(str1[start:end]) | |
return list ( set (substrings)) | |
def list_palindrome(str): | |
palindromes = [] | |
for word in generate_substrings(str): | |
if is_palindrome(word): | |
palindromes.append(word) | |
return " ".join(palindromes) | |
filename = sys.argv[1] | |
f = open(filename,'r') | |
numlines = f.readline() | |
for x in range(int(numlines)): | |
line = f.readline().strip() | |
print(list_palindrome(line)) | |
f.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import itertools | |
from sets import Set | |
def is_palindrome(str): | |
return str == str[::-1] | |
def combinations(str): | |
lst = list(str) | |
words=Set() | |
for i in range(1,len(lst)+1): | |
for element in itertools.combinations(lst,i): | |
words = words | Set (itertools.permutations(element)) | |
return map(list,words) | |
def list_palindrome(str): | |
palindromes = [] | |
for lst in combinations(str): | |
word="".join(lst) | |
if is_palindrome(word): | |
palindromes.append(word) | |
return " ".join(palindromes) | |
filename = sys.argv[1] | |
f = open(filename,'r') | |
numlines = f.readline() | |
for x in range(int(numlines)): | |
line = f.readline().strip() | |
print(list_palindrome(line)) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment