diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..5db7b44 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,17 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + list1.length < list2.length ? (smaller, larger = list1, list2) : (smaller, larger = list2, list1) # define which array is the smallest and which is the largest + + # add each element from the smaller array to the hash table + lookup_hash = {} + smaller.each do |num| + lookup_hash[num] ? lookup_hash[num] = false : lookup_hash[num] = true + end + + # lookup each element in the larger array in the hash table; add to results array if found + results = [] + larger.each do |num| + results << num if lookup_hash[num] + end + + return results end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..be540a0 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,17 @@ - def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + return true if string.empty? + + # adds each char in string to the hash table + lookup_hash = {} + string.each_char do |char| + lookup_hash[char] ? lookup_hash[char] = false : lookup_hash[char] = true + end + + if lookup_hash.length == 1 # palindromes of any length that are made up of the same letter + return true + elsif string.length.even? # the palindrome should have two of each letter + return lookup_hash.length == string.length / 2 + elsif string.length.odd? # the palindrome should have only one odd letter out + return lookup_hash.length == (string.length / 2) + 1 + end end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..c2ff3b0 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,25 @@ - def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + return true if string1 == string2 + return false if string1.length != string2.length + + # add each char from string1 to the hash table + letter_count1 = {} + string1.each_char do |char| + letter_count1[char] ? letter_count1[char] += 1 : letter_count1[char] = 1 # counts how many of each letter are in string1 + end + + # counts how many of each letter are in string2 + letter_count2 = {} + string2.each_char do |char| + letter_count2[char] ? letter_count2[char] += 1 : letter_count2[char] = 1 + end + + return false if letter_count1 != letter_count2 # returns false if the number of a specific letter are different but the string lengths are the same (see comment in test file) + + # lookup each char in string2 in the hash table + string2.each_char do |char| + return false if letter_count1[char].nil? + 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 e9119de..49007b0 100644 --- a/test/palindrome_permutation_test.rb +++ b/test/palindrome_permutation_test.rb @@ -1,7 +1,7 @@ require_relative "test_helper" -xdescribe "palindrome_permutation?" do - it "will work for hello" do +describe "palindrome_permutation?" do + it "will return false for hello" do expect(palindrome_permutation?("hello")).must_equal false end @@ -9,6 +9,10 @@ expect(palindrome_permutation?("carrace")).must_equal true end + it "will work for 'noon'" do + expect(palindrome_permutation?("noon")).must_equal true + end + it "will work for emptystring" do expect(palindrome_permutation?("")).must_equal true end @@ -17,6 +21,10 @@ expect(palindrome_permutation?("racecar")).must_equal true end + it "will work for oooo" do + expect(palindrome_permutation?("oooo")).must_equal true + end + it "will return false for raceca" do expect(palindrome_permutation?("raceca")).must_equal false end diff --git a/test/permutations_test.rb b/test/permutations_test.rb index 79da2f6..2aecc90 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 @@ -13,11 +13,16 @@ expect(permutations?("pasta", "atsap")).must_equal true end - it "returns true for 'pizza', 'pizza'" do + it "returns false for 'pizza', 'pasta'" do expect(permutations?("pizza", "pasta")).must_equal false end + # this test was passing superficially because of line 4 in my method; I added a follow-up test below it "returns false if the number of a specific letter are different" do expect(permutations?("pizza", "piza")).must_equal false end + + it "returns false if the number of a specific letter are different but the string lengths are the same" do + expect(permutations?("pizza", "pizaa")).must_equal false + end end \ No newline at end of file