From 80c24c8352bc835a4b84c7ee5c3618f0be72e051 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Sat, 28 Mar 2020 18:51:20 -0700 Subject: [PATCH 1/3] submission --- lib/array_intersection.rb | 2 +- lib/palindrome_permutation.rb | 13 +++++++++++-- lib/permutations.rb | 12 +++++++++++- test/palindrome_permutation_test.rb | 2 +- test/permutations_test.rb | 4 ++-- 5 files changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..ad17186 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,3 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + intersect = list1 & list2 end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..f436c04 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,13 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" -end \ No newline at end of file + singles = [] + string.chars.each do |char| + if string.count(char).odd? + singles << char + string.slice!(char) + end + end + + return false if singles.length > 1 + return true +end diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..4d696ee 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,14 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + return false if string1.length != string2.length + + string1.chars.each do |char| + if string2.include?(char) + string1.slice!(char) + string2.slice!(char) + end + end + + return true if string1.empty? && string2.empty? + return false end \ No newline at end of file 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..28c50ca 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,7 +13,7 @@ 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 From 1abdab867c15cd330d00a3096038a823889db69b Mon Sep 17 00:00:00 2001 From: Stephanie Date: Sat, 28 Mar 2020 23:36:30 -0700 Subject: [PATCH 2/3] refactored palindrome_permutation to use hash --- lib/palindrome_permutation.rb | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f436c04..2de904e 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,13 +1,19 @@ def palindrome_permutation?(string) - singles = [] - string.chars.each do |char| - if string.count(char).odd? - singles << char - string.slice!(char) - end + + letter_hash = {} + string.chars.map do |char| + letter_hash["#{char}"] = string.count(char) end + + odd_count = 0 + letter_hash.each do |key, value| + if value.odd? + odd_count += 1 + end + end + + odd_count > 1 ? (return false) : (return true) - return false if singles.length > 1 - return true end + From f8e8f7c4056befbd700ee67fdcd46d7fe1522985 Mon Sep 17 00:00:00 2001 From: Stephanie Date: Sat, 28 Mar 2020 23:39:59 -0700 Subject: [PATCH 3/3] refactored to use ternary --- lib/palindrome_permutation.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index 2de904e..102c245 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -8,9 +8,7 @@ def palindrome_permutation?(string) odd_count = 0 letter_hash.each do |key, value| - if value.odd? - odd_count += 1 - end + value.odd? ? (odd_count += 1) : next end odd_count > 1 ? (return false) : (return true)