From 8c7a5cdde51f98c13079931472a93b970b0a321f Mon Sep 17 00:00:00 2001 From: Vera Date: Thu, 26 Mar 2020 22:08:03 -0700 Subject: [PATCH 1/5] Array_intersection hash table --- lib/array_intersection.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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 From 2441b41682d1ec35d09408b906279d4e60ff6a30 Mon Sep 17 00:00:00 2001 From: Vera Date: Thu, 26 Mar 2020 22:40:49 -0700 Subject: [PATCH 2/5] Permutations solved --- lib/permutations.rb | 20 ++++++++++++++++++-- test/permutations_test.rb | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..eda21da 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,20 @@ 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.each_char do |char| + hash[char] = true + end + + string2.each_char do |letter| + if hash[letter] != true + return false + end + end + + return true +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 From bad3eacf70f648cdfe1bee5a657c032496adb27e Mon Sep 17 00:00:00 2001 From: Vera Date: Sun, 29 Mar 2020 13:42:45 -0700 Subject: [PATCH 3/5] Refectoring code --- lib/permutations.rb | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index eda21da..b295719 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,20 +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" return false if string1.length != string2.length # Return a boolean value. - hash = {} - - string1.each_char do |char| - hash[char] = true - end + hash_string1_counting = count_chars_in_string(string1) + hash_string2_counting = count_chars_in_string(string2) - string2.each_char do |letter| - if hash[letter] != true - return false - end - end + return hash_string1_counting == hash_string2_counting - return true end From e9f8282173377083dbdc8a9e3461dbc7db4adb34 Mon Sep 17 00:00:00 2001 From: Vera Date: Sun, 29 Mar 2020 13:43:39 -0700 Subject: [PATCH 4/5] Test added if the count letters are different --- test/permutations_test.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/permutations_test.rb b/test/permutations_test.rb index 5f6ae77..dc3b865 100644 --- a/test/permutations_test.rb +++ b/test/permutations_test.rb @@ -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 From 8c7913a498444202eb4b84baf66e5787beeae582 Mon Sep 17 00:00:00 2001 From: Vera Date: Sun, 29 Mar 2020 15:20:59 -0700 Subject: [PATCH 5/5] Palindrome_permutation implementation --- lib/palindrome_permutation.rb | 30 +++++++++++++++++++++++++++-- test/palindrome_permutation_test.rb | 2 +- 2 files changed, 29 insertions(+), 3 deletions(-) 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/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