Created
February 14, 2018 06:21
-
-
Save eayoungs/7e2532775101d74d466cb781589bdff8 to your computer and use it in GitHub Desktop.
Solution to code fights challenge https://codefights.com/arcade/intro/level-7/PTWhv2oWqd6p4AHB9
This file contains hidden or 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
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