From 84fa574ac5740155dc814da8110624323bd5ebbd Mon Sep 17 00:00:00 2001 From: Katie Date: Mon, 30 Mar 2020 18:19:46 -0700 Subject: [PATCH] completed the three questions --- lib/array_intersection.rb | 11 +++++++++-- lib/palindrome_permutation.rb | 12 +++++++++++- lib/permutations.rb | 25 +++++++++++++++++++++++-- test/palindrome_permutation_test.rb | 2 +- test/permutations_test.rb | 2 +- 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..04b8cbc 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,10 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" -end \ No newline at end of file + common_items = [] + list1.each do |item| + if common_items.include?(item) == false && list2.include?(item) == true + common_items.push(item) + end + end + return common_items + +end diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..7651ea7 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,14 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + string_array = string.split("") + hash_of_letters = {} + string_array.each do |letter| + if hash_of_letters.keys.include?(letter) + hash_of_letters[letter] += 1 + else + hash_of_letters[letter] = 1 + end + end + return true if hash_of_letters.select { |letter,value| value%2 != 0}.keys.length <= 1 + return false end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..e7f9043 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,25 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" -end \ No newline at end of file + string1_array = string1.split("") + string2_array = string2.split("") + hash_of_letters = {} + + string1_array.each do |letter| + if hash_of_letters.keys.include?(letter) + hash_of_letters[letter] += 1 + else + hash_of_letters[letter] = 1 + end + end + + string2_array.each do |letter| + if hash_of_letters.keys.include?(letter) + hash_of_letters[letter] -= 1 + else + return false + end + end + + return true if hash_of_letters.select { |letter,value| value != 0}.keys.length == 0 + return false +end 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