Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion lib/array_intersection.rb
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)

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.

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.include? is an O(n) operation and since you have this in an O(m) loop... this becomes O(n * m)

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
25 changes: 24 additions & 1 deletion lib/palindrome_permutation.rb
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.count has an O(n) loop, so this is an O(n^2) algorithm... Another place a hash could make this better.

Suggested change
string.each_char do |char|
count[char]= string.count(char)
end
string.each_char do |char|
count[char]= count[char] ? count[char] + 1 : 1
end

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
46 changes: 44 additions & 2 deletions lib/permutations.rb
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)

Choose a reason for hiding this comment

The 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
2 changes: 1 addition & 1 deletion test/palindrome_permutation_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative "test_helper"

xdescribe "palindrome_permutation?" do
describe "palindrome_permutation?" do
it "will work for hello" do
expect(palindrome_permutation?("hello")).must_equal false
end
Expand Down
2 changes: 1 addition & 1 deletion test/permutations_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require_relative "test_helper"

xdescribe "permutations?" do
describe "permutations?" do
it "returns true for empty string" do
expect(permutations?("", "")).must_equal true
end
Expand Down