Created
June 10, 2014 02:41
-
-
Save imran31415/8b3b000841f9ababb2e2 to your computer and use it in GitHub Desktop.
This file contains 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
import sys | |
import itertools | |
#itertools used for permutations to create sets | |
''' | |
Author: Imran Hassanali | |
Email: [email protected] | |
''' | |
def get_letters(word): | |
letters = [ch for ch in word if ch.lower() in "abcdefghijklmnopqrstuvwxyz"] | |
return letters | |
'''gets vowels or consonates from a string''' | |
def match_type(word, vowel = False, consonate = False): | |
if vowel: | |
vres = [ch for ch in word if ch.lower() in "aeiouy"] | |
elif consonate: | |
vres = [ch for ch in word if ch.lower() in "bcdfghjklmnpqrstvwxz"] | |
else: | |
raise "Incorrect usage, must specify vowel or consonate" | |
return vres | |
'''if gcd() returns > 1 then condition 3 is satisfied''' | |
def gcd(x, y): | |
while y != 0: | |
(x, y) = (y, x % y) | |
return x | |
'''get SS score of one customer order pair''' | |
def get_ss(customer,order): | |
if len(get_letters(order)) %2 ==0: | |
ss = len(match_type(customer,vowel=True)) * 1.5 | |
else: | |
ss = len(match_type(customer, consonate= True)) | |
if gcd(len(get_letters(order)), len(get_letters(customer))) >1: | |
ss *= 1.5 | |
return float(ss) | |
'''get the total SS of a set of pairs of customers and orders''' | |
def get_set_ss(dicta, SS = 0): | |
for k, v in dicta.iteritems(): | |
SS += get_ss(k, v) | |
return SS | |
def main_function(): | |
with open('suitability.txt','rU') as f: | |
for line in f: | |
#format into two parallel customer, order lists | |
customer, order = [x.replace('\n',"").replace(" ", "") for x in line.split(";")[0].split(',')],[x.replace('\n',"").replace(" ", "") for x in line.split(";")[1].split(',')] | |
# get possible combinations of customers and orders | |
'''loop through sets and find the set with greatest SS score for that line (maxSS) ''' | |
maxSS = 0 | |
for dset in itertools.permutations(customer, len(order)): | |
dset = zip(dset,order) | |
SS = get_set_ss(dict(dset)) | |
if SS > maxSS: | |
maxSS = SS | |
print maxSS | |
main_function() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment