forked from AdaGold/hash-practice
-
Notifications
You must be signed in to change notification settings - Fork 57
completes all but extra credit #28
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
Open
Kristinboyd
wants to merge
1
commit into
Ada-C15:master
Choose a base branch
from
Kristinboyd:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,77 @@ | ||
|
||
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 | ||
|
||
anagram_result = {} | ||
|
||
# iterate through each word in strings | ||
for word in strings: | ||
# sort anagrams into different lists | ||
# use sorted to rearrange each word alphabetically | ||
# eat, tea, ate --> aet, aet, aet | ||
# nat --> ant | ||
# bat --> abt | ||
|
||
# can I use this sorted method? | ||
sort_anagram = ''.join(sorted(word)) | ||
if sort_anagram in anagram_result: | ||
# if word is already an anagram of another word, append word to same list | ||
anagram_result[sort_anagram].append(word) | ||
|
||
else: | ||
# add word to new list | ||
# ! brackets need to be around word b/c we're adding it as a new list in hash table | ||
anagram_result[sort_anagram] = [word] | ||
|
||
return list(anagram_result.values()) | ||
|
||
|
||
def top_k_frequent_elements(nums, k): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
""" 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: ? | ||
""" | ||
pass | ||
|
||
# return empty list if nums is empty | ||
if not nums: | ||
return [] | ||
|
||
# create an empty hash table. key = number, value = occurence in string | ||
# iterate through nums | ||
# if number is not currently in the table, add it to the table and update value to 1 | ||
# if number is in table, update value | ||
nums_table = {} | ||
for number in nums: | ||
if number in nums_table: | ||
nums_table[number] += 1 | ||
else: | ||
nums_table[number] = 1 | ||
|
||
|
||
# create an empty array | ||
# iterate through hash table and append key to array based on value - in desc order | ||
# delete key from original hash table so we don't see it again | ||
nums_arr = [] | ||
|
||
# loop through K # of times | ||
for n in range(k): | ||
max_number = 0 | ||
frequency = 0 | ||
|
||
for num, count in nums_table.items(): | ||
if count > frequency: | ||
frequency = count | ||
max_number = num | ||
|
||
nums_arr.append(max_number) | ||
del nums_table[max_number] | ||
|
||
return nums_arr | ||
|
||
def valid_sudoku(table): | ||
""" This method will return the true if the table is still | ||
a valid sudoku table. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍