-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
37 lines (30 loc) · 1.13 KB
/
main.py
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
import os
import operator
from itertools import combinations
from collections import defaultdict
if __name__ == "__main__":
input_file = os.path.dirname(
os.path.realpath(__file__)
) + '/orders.txt'
# Build a map linking each customer to all drugs ever purchased.
customer_drug_map = defaultdict(lambda: set([]))
with open(input_file) as f:
for line in f:
parts = line.strip().split(": ")
customer_id = parts[0]
drugs = parts[1].split(", ")
for drug in drugs:
customer_drug_map[customer_id].add(drug)
# Count up the times drugs were paired together.
pairings = defaultdict(int)
for customer, drugs in customer_drug_map.iteritems():
if len(drugs) <= 1:
continue
for pairing_combination in combinations(drugs, 2):
pairings[pairing_combination] += 1
# Sort desc
sorted = sorted(pairings.items(), key=operator.itemgetter(1))
sorted_desc = sorted.reverse()
# Output to stdout
for drug_combination, num_times_ordered_together in sorted[:2]:
print ", ".join(drug_combination)