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

Abigail C - Rock #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
59 changes: 54 additions & 5 deletions hash_practice/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,46 @@
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 * m log(m)) (m is the max length of the word)
Space Complexity: O(n)
"""
Comment on lines 2 to 7

Choose a reason for hiding this comment

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

👍 Nice work.

pass
string_dict = {}
# see if the sorted value is a key in the dict and if not add the element in the dict we are on as an array.
for unsorted_word in strings:
sorted_word = str(sorted(unsorted_word))
if sorted_word in string_dict:
string_dict[sorted_word].append(unsorted_word)
else:
string_dict[sorted_word] = [unsorted_word]
return list(string_dict.values())
# If the sorted value is in the dict add the element we are on to the key's value with the existing array.


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: ?
"""
Comment on lines 20 to 25

Choose a reason for hiding this comment

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

👍 Time/space complexity? You are sorting, is there a way without sorting?

pass
# Create an empty dict
num_dict = {}
# Iterate through nums and add count as the value of the dictionary.
for num in nums:
if num in num_dict:
num_dict[num] += 1
else:
num_dict[num] = 1
# Once we have the dict full we will find the values with the largest keys k times.
sorted_list = set(sorted(num_dict.values(), reverse=True)[:k])

result = []
for key, value in num_dict.items():
if len(result) == k:
return result
if value in sorted_list:
result.append(key)
return result
# return the keys in a list


def valid_sudoku(table):

Choose a reason for hiding this comment

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

This solves the rows and columns, but not the 3x3 subgrids.

Expand All @@ -25,5 +53,26 @@ def valid_sudoku(table):
Time Complexity: ?
Space Complexity: ?
"""
pass
rows_dict = {}
col_dict = {}

for i in range(9):
col_dict[i] = {}
for row_index in range(len(table)):
rows_dict[row_index] = {}
row = table[row_index]
for index in range(len(row)):
if row[index] == ".":
continue
else:
if row[index] in rows_dict[row_index]:
return False
else:
rows_dict[row_index][row[index]] = index

if row[index] in col_dict[row_index]:
return False
else:
col_dict[row_index][row[index]] = index
return True