Skip to content

Instantly share code, notes, and snippets.

@rajatdiptabiswas
Created February 5, 2018 17:23
Show Gist options
  • Save rajatdiptabiswas/f75bc4a54c6b9ac5021ab6e92b4ea7ba to your computer and use it in GitHub Desktop.
Save rajatdiptabiswas/f75bc4a54c6b9ac5021ab6e92b4ea7ba to your computer and use it in GitHub Desktop.
Lists all the palindromes that can be formed from a given string
#!/usr/bin/env python3
from itertools import permutations
def main():
t = int(input())
for cases in range(t):
string = str(input())
# creates all the possible permutations of the given string
perm = [''.join(p) for p in permutations(string)]
print(perm)
# creates an empty list to store palindromic permutations
perm_pal = []
for item in perm:
# checks if the string in the permutations is palindromic and adds to list
if item == item[::-1]:
perm_pal.append(item)
print(perm_pal)
# gets rid of all the repeated elements
unique = list(set(perm_pal))
print(unique)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment