diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..897e2c7 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,17 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + + hash = {} + result = [] + + list1.each do |number| + hash[number] = true + end + + list2.each do |number| + if !hash[number].nil? + result << number + end + end + + return result end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..6c1c39e 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,23 @@ - def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" -end \ No newline at end of file + + hash = {} + count = 0 + + word = string.split("") + + word.each do |letter| + hash[letter] = 0 + end + + word.each do |letter| + if hash[letter] + hash[letter] += 1 + end + + if hash[letter] % 2 == 0 + count += 1 + end + end + + return count == word.length/2 +end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..ed08619 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,7 @@ - def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + + first = string1.split("") + second = string2.split("") + + return first.sort == second.sort end \ No newline at end of file diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index 6e43777..246ccfd 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 eb1ce8c..792d9ef 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