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
18 changes: 17 additions & 1 deletion lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
def intersection(list1, list2)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Intersection not implemented"
return [] if list1 == nil || list2 == nil

hash = {}

list1.each do |element|
hash[element] = true
end

result = []

list2.each do |number|
if hash.has_key?(number)
result << number
end
end
result

end
23 changes: 22 additions & 1 deletion lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@

def palindrome_permutation?(string)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "palindrome_permutation? not implemented"
return true if string.empty?

hash = {}

string.split("").each do |letter|
if hash[letter].nil?
hash[letter] = 1
else
hash[letter] += 1
end
end

odd = 0

hash.values.each do |v|
odd += 1 if v.odd?
end
if odd <= 1
return true
else
return false
end
end
26 changes: 24 additions & 2 deletions lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@

def permutations?(string1, string2)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "permutations? not implemented"
return true if string1 == string2
return false if string1.length != string2.length

hash = {}

string1.split('').each do |letter|
if hash[letter]
hash[letter] += 1
else
hash[letter] = 1
end
end

string2.split('').each do |letter|
return false if !hash.key?(letter)
if hash[letter] > 0
hash[letter] -= 1
else
return false
end
end

return true

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