Skip to content

Instantly share code, notes, and snippets.

@heshamnaim
Last active August 9, 2017 20:35
Show Gist options
  • Save heshamnaim/85b77d1708146343175cd5486de53893 to your computer and use it in GitHub Desktop.
Save heshamnaim/85b77d1708146343175cd5486de53893 to your computer and use it in GitHub Desktop.
Prancing Pony
from itertools import permutations,combinations
import time
start_time = time.time()
def palindrome_combinations(dwarf_names):
start_time = time.time()
for i in xrange(0,len(dwarf_names)):
for j in list(combinations(dwarf_names,i+1)):
for k in list(permutations(j)):
combination = "".join(k).lower()
if str(combination) == str(combination)[::-1]:
yield combination
print("Running time is %s seconds" % (time.time() - start_time))
print list(palindrome_combinations(["Gimli", "Fili","Ilif","Ilmig","Mark"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment