-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscount.py
More file actions
59 lines (49 loc) · 1.53 KB
/
Copy pathdiscount.py
File metadata and controls
59 lines (49 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Match products to customers to maximize suitability.
https://www.codeeval.com/open_challenges/48/
"""
from itertools import permutations, product, izip
from fractions import gcd
from fileinput import input
ALPHABET = 'abcdefghijklmnopqrstuvwxyz'
VOWELS = 'aeiouy' # yes, 'y' is a vowel. see defn.
CONSONANTS = set(ALPHABET).difference(VOWELS)
class memoize(object):
def __init__(self, f):
self.f = f
self.cache = {}
def __call__(self, *args):
if args not in self.cache:
self.cache[args] = self.f(*args)
return self.cache[args]
@memoize
def suit(customer, product):
"""how suited is this customer product pair?
>>> suit('Ira', 'iPod')
2
>>> suit('Ira', 'iPhone')
3.0
>>> suit('Ira', 'Apple')
1
"""
num_p = len(product)
num_c = len(customer)
refer = CONSONANTS if num_p % 2 else VOWELS
output = sum(1 for c in customer.lower() if c in refer)
if gcd(num_c, num_p) > 1:
output *= 1.5
return output
#
if __name__ == '__main__':
import doctest
doctest.testmod()
for line in input():
line = line.strip()
if not line:
continue
customers, products = line.split(';')
customers = customers.split(',')
products = products.split(',')
# what is the suitability for a given p?
keyfn = lambda e: sum(suit(c, p) for c, p in izip(*e))
p = max(product(permutations(customers), permutations(products)), key=keyfn)
print keyfn(p)