diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..ffb76f5 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,19 @@ + def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + # raise NotImplementedError, "Intersection not implemented" + intersec_result = [] + + hash = {} + + list1.each do |number| + hash[number] = true + end + + list2.each do |number| + if !hash[number].nil? + intersec_result << number + end + end + + return intersec_result end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..eb68f60 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,30 @@ +def count_chars_in_string (string) + hash = {} + + string.each_char do |char| + if hash.key?(char) + hash[char] += 1 + else + hash[char] = 1 + end + end + return hash +end + +# the number of characters with odd number of occurences. +# If this countcount happens to exceed 1 at any step, +# we conclude that a palindromic permutation isn't possible for the string. + def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" -end \ No newline at end of file + # raise NotImplementedError, "palindrome_permutation? not implemented" + + odd_ocurrences_count = 0 + num_ocurrences_hash = count_chars_in_string(string) + + num_ocurrences_hash.each do |key, value| + odd_ocurrences_count += value % 2 + end + + return odd_ocurrences_count < 2 +end diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..b295719 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,27 @@ + +def count_chars_in_string (string) + hash = {} + + string.each_char do |char| + if hash.key?(char) + hash[char] += 1 + else + hash[char] = 1 + end + end + + return hash +end + def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" -end \ No newline at end of file + # raise NotImplementedError, "permutations? not implemented" + return false if string1.length != string2.length + # Return a boolean value. + + hash_string1_counting = count_chars_in_string(string1) + hash_string2_counting = count_chars_in_string(string2) + + return hash_string1_counting == hash_string2_counting + +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..dc3b865 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 @@ -20,4 +20,8 @@ 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 letter counts differ" do + expect(permutations?("pizza", "ppiza")).must_equal false + end end \ No newline at end of file