Skip to content

Instantly share code, notes, and snippets.

@eayoungs
Created February 14, 2018 06:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eayoungs/7e2532775101d74d466cb781589bdff8 to your computer and use it in GitHub Desktop.
Save eayoungs/7e2532775101d74d466cb781589bdff8 to your computer and use it in GitHub Desktop.
from itertools import permutations
a = ["aba", "bbb", "bab"]
b = ["ab", "bb", "aa"]
c = ["ab", "bb", "aa", "ab", "bb", "aa", "ab", "bb", "aa", "ab", "bb", "aa"]
def string_diff(x, y):
stuff = sum(1 for x, y in zip(x, y) if x != y)
return stuff
def compare_elements(arrangement):
i = 0
global counter
while i < len(arrangement)-1:
x = arrangement[i]
y = arrangement[i+1]
num_diff = string_diff(x,y)
counter += 1
if num_diff > 1:
return False
#print(x, y, num_diff)
i += 1
return True
def compare_permutations(input_array):
for arrangement in permutations(input_array):
if compare_elements(arrangement) is True:
return True
return False
counter = 0
compare_permutations(a), counter
counter = 0
compare_permutations(b), counter
counter = 0
compare_permutations(c), counter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment