diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..c6e93a0 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,16 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + nums_array = [] + nums_hash = {} + + list1.each do |nums| + nums_hash[nums] = 0 + end + + list2.each do |nums| + if nums_hash[nums] + nums_array << nums + end + end + + return nums_array end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..dc873b4 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,21 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + palindrome_hash = {} + result_array = [] + + string.each_char do |char| + if palindrome_hash[char] + palindrome_hash[char] += 1 + else + palindrome_hash[char] = 1 + end + end + + string.each_char do |char| + if palindrome_hash[char].odd? + result_array << char + end + end + + return result_array.length <= 1 end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..f9671bc 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,22 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + permutations_hash = {} + + string1.each_char do |char| + if permutations_hash[char] + permutations_hash[char] += 1 + else + permutations_hash[char] = 1 + end + end + + string2.each_char do |char| + if permutations_hash[char] && permutations_hash[char] > 0 + permutations_hash[char] -= 1 + else + return false + end + end + + return permutations_hash.values.all?(0) end \ No newline at end of file diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index e9119de..984b333 100644 --- a/test/palindrome_permutation_test.rb +++ b/test/palindrome_permutation_test.rb @@ -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 diff --git a/test/permutations_test.rb b/test/permutations_test.rb index 79da2f6..5f6ae77 100644 --- a/test/permutations_test.rb +++ b/test/permutations_test.rb @@ -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