Skip to content

Instantly share code, notes, and snippets.

@dorwardv
Created January 16, 2015 16:00
Show Gist options
  • Save dorwardv/ab665815c7b9f5af2348 to your computer and use it in GitHub Desktop.
Save dorwardv/ab665815c7b9f5af2348 to your computer and use it in GitHub Desktop.
To run: python LeastSwapPermutatedString.py "abcd" "dcba" untested with corner cases
import sys
import itertools
import array
import math
from sets import Set
from operator import add
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 list_tuples_to_list_string(words)
def list_tuples_to_list_string(the_list):
return map (tuples_to_string, the_list)
def tuples_to_string(the_tuple):
return reduce(add,the_tuple)
def is_item_in_list(the_item,the_list):
return the_item in the_list
def populate_arr_count(arr,str):
for i in range(len(str)):
index = ord(str[i])
arr[index] = arr[index] + 1
def is_array_equal(arr1,arr2):
return arr1 == arr2
def find_extra_char(arr1,arr2,the_string):
extra=set()
for n in range(len(the_string)):
index = ord(the_string[n])
if( arr2[index] > arr1[index] ):
extra = extra | set(the_string[n])
return list(extra)
def diff_indices(string1,string2):
string1_diff=""
string2_diff=""
for index in range(len(string2)):
if string2[index] != string1[index]:
string1_diff = string1_diff + string1[index]
string2_diff = string2_diff + string2[index]
return string1_diff, string2_diff
def _get_commons(lhs, rhs, dist):
commons = [char for index, char in enumerate(lhs) if char in\
rhs[int(max(0, index - dist)): int(min(index + dist, len(rhs)))]]
return commons, len(commons)
def transpose_only_jaro_winkler(lhs, rhs):
max_range = max(len(lhs),len(rhs)) #max(floor(float(max(len(lhs), len(rhs))) / float(2.0)) - 1, 0)
commons1, _len1 = _get_commons(lhs, rhs, max_range)
commons2, _len2 = _get_commons(rhs, lhs, max_range)
num_transpositions = sum( ch1 != ch2 for ch1, ch2 in zip(commons1, commons2))
if num_transpositions % 2 == 1 :
num_transpositions = num_transpositions + 1
num_transpositions = num_transpositions / 2
return num_transpositions
arr1 = array.array('i',(0 for i in range(256)))
arr2 = array.array('i',(0 for i in range(256)))
string1 = sys.argv[1]
string2 = sys.argv[2]
str1 = ""
str2 = ""
result = is_item_in_list(string1, combinations(string2))
if result:
##If case insensite
### populate_arr_count(arr1,string1.lower)
### populate_arr_count(arr2,string2.lower)
populate_arr_count(arr1,string1)
populate_arr_count(arr2,string2)
if (not is_array_equal(arr1,arr2)):
extra_chars = find_extra_char(arr1,arr2,string2)
str1 = string1
str2 = string2
for char in extra_chars:
str1 = str1.replace(char,'')
str2 = str2.replace(char,'')
if (str1 != str2):
string1,string2 = diff_indices(str1,str2)
print "" + str(result) + " " + str(transpose_only_jaro_winkler(string2, string1))
else:
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment