diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..93fb13c 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,19 @@ def intersection(list1, list2) - 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 \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..ff5b3dd 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,25 @@ def palindrome_permutation?(string) - 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 \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..5af0a27 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,26 @@ - def permutations?(string1, string2) - 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 \ 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