From 48c87b488e1c0ddb86ad4e5c40238dbdedc9c210 Mon Sep 17 00:00:00 2001 From: Rebeca Date: Fri, 22 Jul 2022 21:05:58 -0700 Subject: [PATCH 1/5] first loop created --- hash_practice/exercises.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 48bf95e..ac92a7f 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -1,11 +1,28 @@ +from array import array +from distutils.log import error +import string + + def grouped_anagrams(strings): """ This method will return an array of arrays. Each subarray will have strings which are anagrams of each other Time Complexity: ? Space Complexity: ? """ - pass + + words = [strings] #this is an array with strings in it + for strings in words: + if sorted(words[0]) == sorted(words[1]): + sorted(words[0]) = [] + if(sorted(strings)== sorted(strings)): + print(string) + +#expected_answer = [ + ["ate","eat","tea"], + ["nat","tan"], + ["bat"] + # ] def top_k_frequent_elements(nums, k): """ This method will return the k most common elements From 40b83c17235fc6d0d2177a6e6db3c23d67d1dd60 Mon Sep 17 00:00:00 2001 From: Rebeca Date: Fri, 22 Jul 2022 21:43:23 -0700 Subject: [PATCH 2/5] final submission --- hash_practice/exercises.py | 42 ++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index ac92a7f..b2c4c37 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -10,19 +10,14 @@ def grouped_anagrams(strings): Time Complexity: ? Space Complexity: ? """ - - words = [strings] #this is an array with strings in it - for strings in words: - if sorted(words[0]) == sorted(words[1]): - sorted(words[0]) = [] - if(sorted(strings)== sorted(strings)): - print(string) - -#expected_answer = [ - ["ate","eat","tea"], - ["nat","tan"], - ["bat"] - # ] + anagram_dict = {} + for sorted_word in strings: + a = tuple(sorted(sorted_word)) + if a in anagram_dict: + anagram_dict[a].append(sorted_word) + else: + anagram_dict[a] = [sorted_word] + return list(anagram_dict.values()) def top_k_frequent_elements(nums, k): """ This method will return the k most common elements @@ -30,7 +25,24 @@ def top_k_frequent_elements(nums, k): Time Complexity: ? Space Complexity: ? """ - pass + if len(nums) == 0: + return [] + + count = {} + freq = [[] for i in range(len(nums) + 1)] + + for n in nums: + count[n] = 1 + count.get(n, 0) + + for n, c in count.items(): + freq[c].append(n) + + res = [] + for i in range(len(freq) - 1, 0, -1): + for n in freq[i]: + res.append(n) + if len(res) == k: + return res def valid_sudoku(table): @@ -42,5 +54,5 @@ def valid_sudoku(table): Time Complexity: ? Space Complexity: ? """ - pass + From f03853a76dcda47d763380f791111f0c67e922a9 Mon Sep 17 00:00:00 2001 From: Rebeca Date: Fri, 22 Jul 2022 21:50:09 -0700 Subject: [PATCH 3/5] passing all tests --- hash_practice/exercises.py | 50 +++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index b2c4c37..944a37d 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -54,5 +54,53 @@ def valid_sudoku(table): Time Complexity: ? Space Complexity: ? """ - + columns = { + 0: set(), + 1: set(), + 2: set(), + 3: set(), + 4: set(), + 5: set(), + 6: set(), + 7: set(), + 8: set(), +} + rows = { + 0: set(), + 1: set(), + 2: set(), + 3: set(), + 4: set(), + 5: set(), + 6: set(), + 7: set(), + 8: set(), + } + grids = { + (0,0): set(), + (0,3): set(), + (0,6): set(), + (3,0): set(), + (3,3): set(), + (3,6): set(), + (6,0): set(), + (6,3): set(), + (6,6): set() + } + + for row in range(len(table)): + for col in range(len(table[0])): + n = table[row][col] + if n != ".": + n = int(n) + if n < 1 or n > 9: + return False + if n in columns[col] or n in rows[row] or n in grids[(get_top_left(row), get_top_left(col))]: + return False + else: + columns[col].add(n) + rows[row].add(n) + grids[(get_top_left(row), get_top_left(col))].add(n) + + return True From 0459eabf620d99d14d824ec6fa8fd1dd5af01059 Mon Sep 17 00:00:00 2001 From: Rebeca Date: Fri, 22 Jul 2022 21:53:07 -0700 Subject: [PATCH 4/5] final final --- hash_practice/exercises.py | 51 +------------------------------------- 1 file changed, 1 insertion(+), 50 deletions(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 944a37d..8f1c79b 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -54,53 +54,4 @@ def valid_sudoku(table): Time Complexity: ? Space Complexity: ? """ - columns = { - 0: set(), - 1: set(), - 2: set(), - 3: set(), - 4: set(), - 5: set(), - 6: set(), - 7: set(), - 8: set(), -} - rows = { - 0: set(), - 1: set(), - 2: set(), - 3: set(), - 4: set(), - 5: set(), - 6: set(), - 7: set(), - 8: set(), - } - grids = { - (0,0): set(), - (0,3): set(), - (0,6): set(), - (3,0): set(), - (3,3): set(), - (3,6): set(), - (6,0): set(), - (6,3): set(), - (6,6): set() - } - - for row in range(len(table)): - for col in range(len(table[0])): - n = table[row][col] - if n != ".": - n = int(n) - if n < 1 or n > 9: - return False - if n in columns[col] or n in rows[row] or n in grids[(get_top_left(row), get_top_left(col))]: - return False - else: - columns[col].add(n) - rows[row].add(n) - grids[(get_top_left(row), get_top_left(col))].add(n) - - return True - + pass \ No newline at end of file From 872da18b962acbece7f68c8ff58372a7534b36ce Mon Sep 17 00:00:00 2001 From: Rebeca Date: Fri, 22 Jul 2022 21:57:59 -0700 Subject: [PATCH 5/5] final final --- hash_practice/exercises.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/hash_practice/exercises.py b/hash_practice/exercises.py index 8f1c79b..d10d187 100644 --- a/hash_practice/exercises.py +++ b/hash_practice/exercises.py @@ -7,8 +7,8 @@ def grouped_anagrams(strings): """ This method will return an array of arrays. Each subarray will have strings which are anagrams of each other - Time Complexity: ? - Space Complexity: ? + Time Complexity: O(n) + Space Complexity: O(n) """ anagram_dict = {} for sorted_word in strings: @@ -54,4 +54,5 @@ def valid_sudoku(table): Time Complexity: ? Space Complexity: ? """ - pass \ No newline at end of file + pass +print(top_k_frequent_elements([1, 2, 2, 2, 3, 3, 3], 2)) \ No newline at end of file