From 1b97af96eb8247b58894f4cf2087c991be459a79 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Mon, 30 Mar 2020 19:05:30 -0700 Subject: [PATCH 1/2] three problems completed, all tests pass --- lib/array_intersection.rb | 15 ++++++++++++++- lib/palindrome_permutation.rb | 19 ++++++++++++++++++- lib/permutations.rb | 20 +++++++++++++++++++- test/palindrome_permutation_test.rb | 2 +- test/permutations_test.rb | 2 +- 5 files changed, 53 insertions(+), 5 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..514b1db 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,16 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + nums_array = [] + nums_hash = {} + + list1.each do |num| + nums_hash[num] = 0 + end + + list2.each do |num| + if nums_hash[num] + nums_array << num + end + end + + return nums_array end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..dc873b4 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,21 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + palindrome_hash = {} + result_array = [] + + string.each_char do |char| + if palindrome_hash[char] + palindrome_hash[char] += 1 + else + palindrome_hash[char] = 1 + end + end + + string.each_char do |char| + if palindrome_hash[char].odd? + result_array << char + end + end + + return result_array.length <= 1 end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..f9671bc 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,22 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + permutations_hash = {} + + string1.each_char do |char| + if permutations_hash[char] + permutations_hash[char] += 1 + else + permutations_hash[char] = 1 + end + end + + string2.each_char do |char| + if permutations_hash[char] && permutations_hash[char] > 0 + permutations_hash[char] -= 1 + else + return false + end + end + + return permutations_hash.values.all?(0) 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..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 e5e293e6261a2370846f5cc94589ab733a67fab0 Mon Sep 17 00:00:00 2001 From: mulhoo Date: Sun, 28 Mar 2021 13:19:45 -0700 Subject: [PATCH 2/2] example change --- lib/array_intersection.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index 514b1db..c6e93a0 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -2,13 +2,13 @@ def intersection(list1, list2) nums_array = [] nums_hash = {} - list1.each do |num| - nums_hash[num] = 0 + list1.each do |nums| + nums_hash[nums] = 0 end - list2.each do |num| - if nums_hash[num] - nums_array << num + list2.each do |nums| + if nums_hash[nums] + nums_array << nums end end