-
Notifications
You must be signed in to change notification settings - Fork 47
Space - Hala #37
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 - Hala #37
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,45 @@ | ||
| ## Array Intersection | ||
|
|
||
| # Design and implement a method that takes two integer arrays with unique values and returns their intersection in a new array. | ||
|
|
||
| # For example: | ||
|
|
||
| # ``` | ||
| # intersection([2, 3, 4], [4, 5, 6]) => [4] | ||
| # intersection([50, 43, 25, 72], [25, 36, 43, 50, 80]) => [50, 25, 43] | ||
| # intersection([9, 30, 42], [56, 34, 90, 32]) => [] | ||
| # ``` | ||
|
|
||
|
|
||
|
|
||
| def intersection(list1, list2) | ||
| raise NotImplementedError, "Intersection not implemented" | ||
| shortlist = [] | ||
| longest = [] | ||
| if list1.length < list2.length | ||
| shortlist = list1 | ||
| longest = list2 | ||
| else | ||
| shortlist = list2 | ||
| longest = list1 | ||
| end | ||
|
|
||
| hash = {} | ||
| shortlist.each do |item| | ||
| if | ||
| longest.include?(item) | ||
|
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.
Think about how you could use a hash to make this faster. |
||
| hash[item] = true | ||
| else | ||
| hash[item] = false | ||
| end | ||
| end | ||
|
|
||
| list3 = [] | ||
| hash.keys.each do |key| | ||
| if | ||
| hash[key] == true | ||
| list3<< key | ||
| end | ||
| end | ||
|
|
||
| return list3 | ||
| end | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,27 @@ | ||||||||||||||
| ## Could Be A Palindrome | ||||||||||||||
|
|
||||||||||||||
| # Write a method which takes a string as an argument and returns true if the letters could be re-arranged into a palindrome. | ||||||||||||||
|
|
||||||||||||||
| # ``` | ||||||||||||||
| # palindrome_permutation?("hello") => false | ||||||||||||||
| # palindrome_permutation?("carrace") => true # because racecar is a palindrome | ||||||||||||||
| # ``` | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def palindrome_permutation?(string) | ||||||||||||||
| raise NotImplementedError, "palindrome_permutation? not implemented" | ||||||||||||||
| count = {} | ||||||||||||||
| x = 0 | ||||||||||||||
| string.each_char do |char| | ||||||||||||||
| count[char]= string.count(char) | ||||||||||||||
| end | ||||||||||||||
|
Comment on lines
+14
to
+16
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
|
||||||||||||||
| count.keys.each do |key| | ||||||||||||||
| if count[key]%2 != 0 | ||||||||||||||
| x +=1 | ||||||||||||||
| end | ||||||||||||||
| end | ||||||||||||||
| if x == 0 || x == 1 | ||||||||||||||
| return true | ||||||||||||||
| else | ||||||||||||||
| return false | ||||||||||||||
| end | ||||||||||||||
| end | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,46 @@ | ||
|
|
||
| def permutations?(string1, string2) | ||
| raise NotImplementedError, "permutations? not implemented" | ||
| ## Check Permutations | ||
|
|
||
| # Write a method which will take two strings as arguments and returns true if one string is a permutation of the other. | ||
|
|
||
| # ``` | ||
| # permutations?("hello", "ehllo") => true | ||
| # permutations?("pasta", "atsap") => true | ||
| # permutations?("Pizza", "Pasta") => false | ||
| # permutations?("", "") => true | ||
| # ``` | ||
|
|
||
|
|
||
| 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. This is another instance where you've brute-forced the solution. It works, but a hash would make this much better. I encourage you to think a bit further on this. |
||
| hash = {} | ||
| if string1.length == string2.length | ||
| string2.each_char do |char| | ||
| if string1.include?(char) | ||
| if hash[char] == true | ||
| repeated = char+char | ||
| hash[repeated] = true | ||
| else | ||
| hash[char] = true | ||
| end | ||
| else | ||
| hash[char]= false | ||
| end | ||
| end | ||
| else | ||
| return false | ||
| end | ||
|
|
||
| occurences = 0 | ||
| hash.keys.each do |key| | ||
| if hash[key] == true | ||
| occurences +=1 | ||
| end | ||
| end | ||
|
|
||
| if occurences == string1.length | ||
| return true | ||
| else | ||
| return false | ||
| end | ||
|
|
||
| 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.