-
Notifications
You must be signed in to change notification settings - Fork 47
Space - Katie #36
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
base: master
Are you sure you want to change the base?
Space - Katie #36
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| def intersection(list1, list2) | ||
| raise NotImplementedError, "Intersection not implemented" | ||
| end | ||
| common_items = [] | ||
| list1.each do |item| | ||
| if common_items.include?(item) == false && list2.include?(item) == true | ||
|
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.
|
||
| common_items.push(item) | ||
| end | ||
| end | ||
| return common_items | ||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,14 @@ | ||||||
|
|
||||||
| def palindrome_permutation?(string) | ||||||
|
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 see my notes on time complexity. |
||||||
| raise NotImplementedError, "palindrome_permutation? not implemented" | ||||||
| string_array = string.split("") | ||||||
| hash_of_letters = {} | ||||||
| string_array.each do |letter| | ||||||
| if hash_of_letters.keys.include?(letter) | ||||||
|
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. Remember that
Suggested change
This way you take advantage of the O(1) lookup time of a hash. |
||||||
| hash_of_letters[letter] += 1 | ||||||
| else | ||||||
| hash_of_letters[letter] = 1 | ||||||
| end | ||||||
| end | ||||||
| return true if hash_of_letters.select { |letter,value| value%2 != 0}.keys.length <= 1 | ||||||
| return false | ||||||
| end | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,25 @@ | ||||||
|
|
||||||
| def permutations?(string1, string2) | ||||||
|
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. See my suggestions regarding improving your runtime. |
||||||
| raise NotImplementedError, "permutations? not implemented" | ||||||
| end | ||||||
| string1_array = string1.split("") | ||||||
| string2_array = string2.split("") | ||||||
| hash_of_letters = {} | ||||||
|
|
||||||
| string1_array.each do |letter| | ||||||
| if hash_of_letters.keys.include?(letter) | ||||||
|
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.
Suggested change
|
||||||
| hash_of_letters[letter] += 1 | ||||||
| else | ||||||
| hash_of_letters[letter] = 1 | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| string2_array.each do |letter| | ||||||
| if hash_of_letters.keys.include?(letter) | ||||||
|
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.
Suggested change
|
||||||
| hash_of_letters[letter] -= 1 | ||||||
| else | ||||||
| return false | ||||||
| end | ||||||
| end | ||||||
|
|
||||||
| return true if hash_of_letters.select { |letter,value| value != 0}.keys.length == 0 | ||||||
| return false | ||||||
| end | ||||||
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, but it's O(n * m) in time complexity. Using a hash you can get this to O(n + m) time complexity which is much better.
I encourage you to think about how to do this.