forked from AdaGold/hash-practice
-
Notifications
You must be signed in to change notification settings - Fork 43
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
Raisah #28
Open
raisahv
wants to merge
2
commits into
Ada-C12:master
Choose a base branch
from
raisahv: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
Raisah #28
Changes from all commits
Commits
Show all changes
2 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,20 +1,82 @@ | ||
|
||
require 'awesome_print' | ||
|
||
# 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(nm) because the method will always iterate through each string (n) in strings and then each string will be compared to all hashes (m) that have been created, which in the worst case could be the same as the number of strings | ||
# Space Complexity: O(n) because the size of anagrams_grouped and hash_groups will grow linearly with the size of strings in the worse case | ||
def grouped_anagrams(strings) | ||
raise NotImplementedError, "Method hasn't been implemented yet!" | ||
return [] if strings.empty? | ||
|
||
hash_groups = [] | ||
anagrams_grouped = [] | ||
|
||
strings.each do |string| | ||
string_hash = {} | ||
|
||
# count freq of characters in string through hash | ||
string.each_char do |char| | ||
if string_hash[char] | ||
string_hash[char] += 1 | ||
else | ||
string_hash[char] = 1 | ||
end | ||
end | ||
|
||
# if no hash_groups exist, add new string hash to hash group and add string to anagrams list | ||
if hash_groups.empty? | ||
hash_groups << string_hash | ||
anagrams_grouped << [string] | ||
|
||
# otherwise, test string hash against all previous hashes, if match, add to existing anagram group, if unique, add hash group, create new anagram group | ||
else | ||
unique = true | ||
hash_groups.each_with_index do |hash, index| | ||
if hash == string_hash | ||
unique = false | ||
anagrams_grouped[index] << string | ||
end | ||
end | ||
|
||
if unique | ||
hash_groups << string_hash | ||
anagrams_grouped << [string] | ||
end | ||
end | ||
end | ||
|
||
return anagrams_grouped | ||
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) because the sort_by method will have the time complexity of quick sort | ||
# Space Complexity: O(n) because the size of the variables will change linearly with the size of list | ||
def top_k_frequent_elements(list, k) | ||
Comment on lines
+52
to
54
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. 👍 Correct, nice work |
||
raise NotImplementedError, "Method hasn't been implemented yet!" | ||
return [] if list.empty? || k == 0 | ||
|
||
item_with_freq = [] | ||
item_freq = {} | ||
|
||
# make hash with freq of each element | ||
list.each do |item| | ||
if item_freq[item] | ||
item_freq[item] += 1 | ||
else | ||
item_freq[item] = 1 | ||
end | ||
end | ||
|
||
# turn hash into array and sort by freq | ||
item_with_freq = item_freq.to_a | ||
item_with_freq.sort_by { |item| item[1] } | ||
|
||
# iterate through sorted array k times and pull out top items | ||
top_items = [] | ||
k.times do |i| | ||
top_items << item_with_freq[i][0] | ||
end | ||
|
||
return top_items | ||
end | ||
|
||
|
||
|
@@ -23,8 +85,71 @@ 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: ? | ||
# Time Complexity: O(n) because at method must go through the table 3 times to check for each sceniario to be valid (row, column, 3x3 grid) | ||
# Space Complexity: O(1) because the size of the variables created is constant. (if the grid is greater than 9x9 this might change for the corners array) | ||
def valid_sudoku(table) | ||
Comment on lines
+88
to
90
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 works, but the method is pretty long and could use some refactoring. Maybe breaking some things into helper methods. |
||
raise NotImplementedError, "Method hasn't been implemented yet!" | ||
numbers = {} | ||
|
||
# iterate through each row and confirm valid | ||
row = 0 | ||
while row < table.length | ||
table[row].each do |value| | ||
return false if !check_value(numbers, value) | ||
end | ||
|
||
numbers = {} | ||
row += 1 | ||
end | ||
|
||
# iterate through each column and confirm valid | ||
numbers = {} | ||
col = 0 | ||
while col < table[0].length | ||
(0..8).each do |x| | ||
value = table[x][col] | ||
|
||
return false if !check_value(numbers, value) | ||
end | ||
|
||
numbers = {} | ||
col += 1 | ||
end | ||
|
||
# create list of each sub-box's upper left corner | ||
corners = [] | ||
(0..2).each do |row| | ||
(0..2).each do |col| | ||
corners << [row * 3, col * 3] | ||
end | ||
end | ||
|
||
# iterate through the list of corners to see if each value is unique within the sub-box | ||
numbers = {} | ||
index = 0 | ||
while index < corners.length | ||
(0..2).each do |x| | ||
(0..2).each do |y| | ||
x_point = corners[index][0] + x | ||
y_point = corners[index][1] + y | ||
value = table[x_point][y_point] | ||
|
||
return false if !check_value(numbers, value) | ||
end | ||
end | ||
|
||
numbers = {} | ||
index += 1 | ||
end | ||
|
||
return true | ||
end | ||
|
||
def check_value(numbers, value) | ||
if value != "." && numbers[value] | ||
return false | ||
elsif value != "." | ||
numbers[value] = true | ||
end | ||
|
||
return true | ||
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.
This works with good time/space complexity, but could use with some refactoring.