def solve(self, lists, previous_selection=set()): """ >>> x = {Context(value=1.0, cohorts={1,2,3}), Context(value=2.0, cohorts={2,3,4})} >>> m = GreedyMaxCover() >>> r = m.run(x) # Silent the result >>> r = m.results_in_list >>> r == [[1,2,3]] or r == [[2,3,4]] True """ result1 = [] GreedyMaxCover._solve(lists, result1) size1 = MaxCover.length_of_total_elements(result1) size2 = -1 result2 = [] if previous_selection: result2 = GreedyMaxCover.get_selection_from_previous_results(lists, previous_selection) size2 = MaxCover.length_of_total_elements(result2) result3 = [] GreedyMaxCover._solve_from_size(lists, result3) size3 = MaxCover.length_of_total_elements(result3) # Find the distinct set of possible selections if size1 > size2: if size1 > size3: return [result1] else: return [result3] else: if size2 > size3: return [result2] else: return [result3]
def get_list_with_maximum_friends(lists): """ >>> x = [[1,2,3],[3,4],[4,5,6],[6,7,8,9]] >>> GreedyMaxCover.get_list_with_maximum_friends(x) == [1,2,3] True """ result = {} for i, l in enumerate(lists): friend, enemy = MaxCover.find_friend_enemy(lists, l) f = MaxCover.length_of_total_elements(friend) e = MaxCover.length_of_total_elements(enemy) result[i] = f - e # print result r = sorted(result.iteritems(), key=operator.itemgetter(1), reverse=True) # print r return lists[r[0][0]]