def update(data_set, data_setting): new_means = dictionary(list) centroids_P = list() for res, point in zip(data_setting, data_set): new_means[res].append(point) for means in new_means.values(): centroids_P.append(Meanmeans(means)) return centroids_P
def group(self, strings: List[str]) -> List[List[str]]: """ Approach: Categorized by sorted string. Time Complexity: O(NK log K) Space Complexity: O(NK) :param strings: :return: """ anagrams = dictionary(list) for string in strings: anagrams[tuple(sorted(string))].append(string) return anagrams.values()
def group_(self, strings: List[str]) -> List[List[str]]: """ Approach: Categorized by count. Time Complexity: O(NK) Space Complexity: O(NK) :param strings: :return: """ anagrams = dictionary(list) for string in strings: count = [0] * 26 for char in string: count[ord(char) - ord('a')] += 1 anagrams[tuple(count)].append(string) return anagrams.values()
def setCentroids(data_set, k): cpoint = list() v0 = 0 axis = len(data_set[v0]) XYminmaxValues = dictionary(int) for point in data_set: for i in range(axis): val = point[i] ForMinV = ForMinValues % i ForMaxV = ForMaxValues % i if val > XYminmaxValues[ForMaxV] or ForMaxV not in XYminmaxValues: XYminmaxValues[ForMaxV] = val elif val < XYminmaxValues[ForMinV] or ForMinV not in XYminmaxValues: XYminmaxValues[ForMinV] = val else: "Do nothing" for i in range(k): xymeans = list() for j in range(axis): min_val = XYminmaxValues[ForMinValues % j] max_val = XYminmaxValues[ForMaxValues % j] xymeans.append(same(min_val, max_val)) cpoint.append(xymeans) return cpoint
def findMinimumSubString(string): length = len(string) distinctCount = len(set([givenString for givenString in string])) startIndex = 0 minimumLength = length total = 0 begin = 0 currentCount = dictionary(lambda: 0) for index in range(length): currentCount[string[index]] += 1 if currentCount[string[index]] == 1: total += 1 if total == distinctCount: while currentCount[string[begin]] > 1: if currentCount[string[begin]] > 1: currentCount[string[begin]] -= 1 begin += 1 len_window = index - begin + 1 if minimumLength > len_window: minimumLength = len_window startIndex = begin return len(string[startIndex:startIndex + minimumLength])