forked from AdaGold/hash-practice
-
Notifications
You must be signed in to change notification settings - Fork 41
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
Space - Cathy #14
Open
cojenco
wants to merge
4
commits into
Ada-C13:master
Choose a base branch
from
cojenco: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
Space - Cathy #14
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,19 +1,55 @@ | ||
|
||
# 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) since we're only looping through the words once | ||
# Space Complexity: O(n) where a new hash is storing the n # of strings | ||
|
||
def grouped_anagrams(strings) | ||
raise NotImplementedError, "Method hasn't been implemented yet!" | ||
return [] if strings.empty? | ||
groups = Hash.new() | ||
|
||
# sort each string so we can compare if they look the same == anagrams | ||
strings.each do |string| | ||
sorted_str = string.split("").sort.join | ||
|
||
# store data into groups | ||
if groups[sorted_str] | ||
groups[sorted_str] << string | ||
else | ||
groups[sorted_str] = [] | ||
groups[sorted_str] << string | ||
end | ||
end | ||
# print groups | ||
|
||
return groups.values | ||
end | ||
|
||
|
||
# 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(nlogn) since I'm calling a sort_by enumerable | ||
# Space Complexity: O(n) | ||
def top_k_frequent_elements(list, k) | ||
raise NotImplementedError, "Method hasn't been implemented yet!" | ||
# loop through list and create hash: num => count | ||
# then search for the k-most frequent values mapped to key | ||
return [] if list.empty? | ||
count = {} | ||
list.each do |num| | ||
count[num] ? count[num] += 1 : count[num] = 1 | ||
end | ||
|
||
sorted_hash = count.sort_by {|k, v| -v} # hash#sort_by returns a matrix (2-D array) | ||
ans = [] | ||
i = 0 | ||
while i < k | ||
ans << sorted_hash[i][0] | ||
i += 1 | ||
end | ||
|
||
return ans | ||
# ans = count.keys.max_by(k){|key| count[key] } | ||
# https://apidock.com/ruby/Enumerable/max_by # WHY?! what does the max_by actually do?! | ||
end | ||
|
||
|
||
|
@@ -22,8 +58,81 @@ def top_k_frequent_elements(list, k) | |
# 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: ? | ||
# Reference: https://www.tutorialspoint.com/valid-sudoku-in-python | ||
# Time Complexity: O(1) since a Sudoku board is always 9x9 (81 cells), scaling never changes | ||
# Space Complexity: O(1) where the extra hashes are constant tracking 1~9 | ||
def valid_sudoku(table) | ||
raise NotImplementedError, "Method hasn't been implemented yet!" | ||
# 1. check rows 2. check columns 3. check sub-boxes | ||
# utilize the indices in the 2D-array and use hashed to keep track | ||
(0...9).each do |i| | ||
row = {} | ||
col = {} | ||
sub_box = {} | ||
# below marks the anchor points for each sub-box: [0,0], [0,3], [0,6], [3,0], [3,3], [3,6], [6,0], [6,3], [6,6] | ||
sub_row = 3 * (i / 3) | ||
sub_col = 3 * (i % 3) | ||
|
||
(0...9).each do |j| | ||
# check rows | ||
if table[i][j] =~ /\d/ && row[table[i][j]] | ||
return false | ||
else | ||
row[table[i][j]] = 1 | ||
end | ||
# check columns | ||
if table[j][i] =~ /\d/ && col[table[j][i]] | ||
return false | ||
else | ||
col[table[j][i]] = 1 | ||
end | ||
# check sub-boxes: scope the boundaries of sub-boxes according to the anchor points sub_row and sub_col | ||
sr = sub_row + (j / 3) | ||
sc = sub_col + (j % 3) | ||
if table[sr][sc] =~ /\d/ && sub_box[table[sr][sc]] | ||
return false | ||
else | ||
sub_box[table[sr][sc]] = 1 | ||
end | ||
end | ||
end | ||
|
||
return true | ||
end | ||
|
||
|
||
# Thought I could use a hash function to solve grouped_anagrams, but realized the sum/math would cause incorrect results | ||
# def grouped_anagrams(strings) | ||
Comment on lines
+103
to
+104
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. 🥇 |
||
# # hash function and add sum | ||
# # 2 factors: score by alphabet and string length # can I assume they are anagrams if both factors meet? | ||
# return [] if strings.empty? | ||
# alpha = { | ||
# "a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5, | ||
# "f" => 6, "g" => 7, "h" => 8, "i" => 9, "j" => 10, | ||
# "k" => 11, "l" => 12, "m" => 13, "n" => 14, "o" => 15, | ||
# "p" => 16, "q" => 17, "r" => 18, "s" => 19, "t" => 20, | ||
# "u" => 21, "v" => 22, "w" => 23, "x" => 24, "y" => 25, "z" => 26, | ||
# } | ||
# groups = Hash.new() | ||
|
||
# strings.each do |string| | ||
# # loop through each letter and count total score # get length | ||
# l = 0 | ||
# sum = 0 | ||
# while l < string.length | ||
# char = string[l] | ||
# sum += alpha[char] | ||
# l += 1 | ||
# end | ||
|
||
# # store data into groups | ||
# if groups[sum] | ||
# groups[sum] << string | ||
# else | ||
# groups[sum] = [] | ||
# groups[sum] << string | ||
# end | ||
# end | ||
# # print groups | ||
|
||
# return groups.values | ||
# end |
This file contains 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
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.
Nice work, good job looking up what max_by does. Well done.