Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rock - mana #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 64 additions & 9 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,55 @@
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)
Comment on lines 2 to +6

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice work

"""
pass
anagram_table = {}
anagram_values = []

for word in strings:
key = ''.join(sorted(word))
if key in anagram_table.keys():
anagram_table[key].append(word)
else:
anagram_table[key] = []
anagram_table[key].append(word)

for value in anagram_table.values():
anagram_values.append(value)

return anagram_values

def top_k_frequent_elements(nums, k):
""" This method will return the k most common elements
In the case of a tie it will select the first occuring element.
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n)
Space Complexity: O(n)
"""
Comment on lines 24 to 29

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice work

pass
numbers_dict = {}
frequency_dict = {}
return_list = []

for num in nums:
if num in numbers_dict:
numbers_dict[num] += 1
else:
numbers_dict[num] = 1

for key, value in numbers_dict.items():
if value not in frequency_dict:
frequency_dict[value] = [key]
else:
frequency_dict[value].append(key)

for i in range(len(nums), 0, -1):
if i in frequency_dict:
return_list.extend(frequency_dict[i])

if len(return_list) >= k:
break

return return_list


def valid_sudoku(table):
Expand All @@ -22,8 +59,26 @@ def valid_sudoku(table):
Each element can either be a ".", or a digit 1-9
The same digit cannot appear twice or more in the same
row, column or 3x3 subgrid
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n)
Space Complexity: O(n)
"""
pass
for i in range(len(table)):
row = {}
column = {}
block = {}
row_cube = 3 * (i//3)
column_cube = 3 * (i%3)
for j in range(len(table)):
if table[i][j]!='.' and table[i][j] in row:
return False
row[table[i][j]] = 1
if table[j][i]!='.' and table[j][i] in column:
return False
column[table[j][i]] = 1
rc= row_cube+j//3
cc = column_cube + j%3
if table[rc][cc] in block and table[rc][cc]!='.':
return False
block[table[rc][cc]]=1
return True

3 changes: 2 additions & 1 deletion tests/test_top_k_frequent_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ def test_will_work_for_list_with_k_elements_all_unique():
def test_returns_2_3_for_1_2_2_2_3_3_3():
# Arrange
numbers = [1, 2, 2, 2, 3, 3, 3]
k = 2
k = 1

# Act
answer = top_k_frequent_elements(numbers, k)

# Assert
answer.sort()
print(answer)
assert answer == [2, 3]