diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..a831a9a 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,20 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + + intersections = {} + + list1.each do |value| + if !intersections[value] + intersections[value] = 1 + elsif intersections[value] + intersections[value] += 1 + end + end + + list2.each do |value| + if intersections[value] + intersections[value] += 1 + end + end + + return (intersections.select { |key, value| value == 2}).keys end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..0a5462f 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" + string_array = string.split("") + letter_instances = {} + + string_array.each do |letter| + if !letter_instances[letter] + letter_instances[letter] = 1 + else + letter_instances[letter] += 1 + end + end + + duplicate_letters = letter_instances.select { |key, value| (value % 2) == 0} + + if duplicate_letters.values.sum == string_array.length || duplicate_letters.values.sum == string_array.length - 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..c41e6a0 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,26 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + intersections = {} + string1_array = string1.split("") + string2_array = string2.split("") + + string1_array.each do |value| + if !intersections[value] + intersections[value] = 1 + elsif intersections[value] + intersections[value] += 1 + end + end + + string2_array.each do |value| + if intersections[value] + intersections[value] += 1 + end + end + + if ((intersections.select { |key, value| (value % 2) == 0}).values.sum / 2) == string1_array.length + return true + else + return false + end 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..4dcc9ee 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 @@ -9,7 +9,7 @@ expect(permutations?("hello", "ehllo")).must_equal true end - it "returns true for 'heelo', 'ehllo'" do + it "returns false for 'heelo', 'ehllo'" do expect(permutations?("heelo", "ehllo")).must_equal false end it "returns true for 'pasta', 'atsap'" do