forked from AdaGold/hash-practice
-
Notifications
You must be signed in to change notification settings - Fork 40
Jessica #1
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
jessicacaley
wants to merge
1
commit into
Ada-C11:master
Choose a base branch
from
jessicacaley: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
Jessica #1
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 |
|---|---|---|
|
|
@@ -2,19 +2,56 @@ | |
|
|
||
| # 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 logn) where n is the length of the strings in the strings element | ||
| # Space Complexity: O(n) where n is the length of the input | ||
| def grouped_anagrams(strings) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| return [] if strings == [] | ||
| anagrams = {} | ||
|
|
||
| strings.each_with_index do |string, i| | ||
| sorted_string = string.split("").sort.join("").downcase | ||
| if anagrams[sorted_string] | ||
| anagrams[sorted_string] << string | ||
| else | ||
| anagrams[sorted_string] = [string] | ||
| end | ||
| end | ||
|
|
||
| return anagrams.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(n) where n is the length of list | ||
| # Space Complexity: O(n) where n is the length of the list | ||
| def top_k_frequent_elements(list, 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. 👍 |
||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| return [] if list == [] | ||
|
|
||
| frequency = {} | ||
| list.each do |element| | ||
| if frequency[element] | ||
| frequency[element] += 1 | ||
| else | ||
| frequency[element] = 1 | ||
| end | ||
| end | ||
|
|
||
| k_frequent_elements = [] | ||
|
|
||
| k.times do |i| | ||
| max_freq = 0 | ||
| max_freq_element = nil | ||
| frequency.each do |element, freq| | ||
| if freq > max_freq | ||
| max_freq = freq | ||
| max_freq_element = element | ||
| end | ||
| end | ||
| k_frequent_elements << max_freq_element | ||
| frequency[max_freq_element] = 0 if max_freq_element | ||
| end | ||
|
|
||
| return k_frequent_elements | ||
| end | ||
|
|
||
|
|
||
|
|
@@ -26,5 +63,131 @@ def top_k_frequent_elements(list, k) | |
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def valid_sudoku(table) | ||
| raise NotImplementedError, "Method hasn't been implemented yet!" | ||
| subgrids = {} | ||
|
|
||
| # return false if any element is repeated in a row | ||
| table.each do |row| | ||
| row_count = {} | ||
| row.each do |element| | ||
| if row_count[element] | ||
| return false unless element == "." | ||
| else | ||
| row_count[element] = 1 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # return false if any element is repeated in a column | ||
| columns = { | ||
| 0 => {}, | ||
| 1 => {}, | ||
| 2 => {}, | ||
| 3 => {}, | ||
| 4 => {}, | ||
| 5 => {}, | ||
| 6 => {}, | ||
| 7 => {}, | ||
| 8 => {}, | ||
| } | ||
| table.each do |row| | ||
| row.each_with_index do |element, index| | ||
| if columns[index][element] | ||
| return false unless element == "." | ||
| else | ||
| columns[index][element] = 1 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| first_subgrid = {} | ||
| second_subgrid = {} | ||
| third_subgrid = {} | ||
|
|
||
| table[0..2].each do |row| | ||
|
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 looks pretty repetitive, could you instead create a method to do this on different subgrids? |
||
| row[0..2].each do |element| | ||
| if first_subgrid[element] | ||
| return false unless element == "." | ||
| else | ||
| first_subgrid[element] = 1 | ||
| end | ||
| end | ||
|
|
||
| row[3..5].each do |element| | ||
| if second_subgrid[element] | ||
| return false unless element == "." | ||
| else | ||
| second_subgrid[element] = 1 | ||
| end | ||
| end | ||
|
|
||
| row[6..8].each do |element| | ||
| if third_subgrid[element] | ||
| return false unless element == "." | ||
| else | ||
| third_subgrid[element] = 1 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| first_subgrid = {} | ||
| second_subgrid = {} | ||
| third_subgrid = {} | ||
|
|
||
| table[3..5].each do |row| | ||
| row[0..2].each do |element| | ||
| if first_subgrid[element] | ||
| return false unless element == "." | ||
| else | ||
| first_subgrid[element] = 1 | ||
| end | ||
| end | ||
|
|
||
| row[3..5].each do |element| | ||
| if second_subgrid[element] | ||
| return false unless element == "." | ||
| else | ||
| second_subgrid[element] = 1 | ||
| end | ||
| end | ||
|
|
||
| row[6..8].each do |element| | ||
| if third_subgrid[element] | ||
| return false unless element == "." | ||
| else | ||
| third_subgrid[element] = 1 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| first_subgrid = {} | ||
| second_subgrid = {} | ||
| third_subgrid = {} | ||
|
|
||
| table[6..8].each do |row| | ||
| row[0..2].each do |element| | ||
| if first_subgrid[element] | ||
| return false unless element == "." | ||
| else | ||
| first_subgrid[element] = 1 | ||
| end | ||
| end | ||
|
|
||
| row[3..5].each do |element| | ||
| if second_subgrid[element] | ||
| return false unless element == "." | ||
| else | ||
| second_subgrid[element] = 1 | ||
| end | ||
| end | ||
|
|
||
| row[6..8].each do |element| | ||
| if third_subgrid[element] | ||
| return false unless element == "." | ||
| else | ||
| third_subgrid[element] = 1 | ||
| end | ||
| end | ||
| end | ||
|
|
||
| return true | ||
| end | ||
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
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.
I would say that it's O(n) where n is the number of strings if the length of any particular string is pretty small.
It's O(n m log m) where n is the number of strings and m is the average length of any particular string, if the strings can be arbitrarily large.