From 615a3c07663f2ddc0fb6bc691b43db57effdfb5a Mon Sep 17 00:00:00 2001 From: Yaz Date: Sat, 2 May 2020 20:07:38 -0700 Subject: [PATCH] complete --- lib/array_intersection.rb | 22 +++++++++++++++++++++- lib/palindrome_permutation.rb | 23 +++++++++++++++++++++-- lib/permutations.rb | 16 +++++++++++++++- test/array_intersection_test.rb | 1 + test/palindrome_permutation_test.rb | 2 +- test/permutations_test.rb | 4 ++-- 6 files changed, 61 insertions(+), 7 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..53afd6d 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,23 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + intersection = {} + short_list = [] + long_list = [] + results = [] + if list1.length < list2.length + short_list = list1 + long_list = list2 + else + short_list = list2 + long_list = list1 + end + short_list.each do |num| + intersection[num] = true + end + + long_list.each do |num| + if intersection[num] + results.push(num) + end + end + return results end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..f71b1fe 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,23 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" -end \ No newline at end of file + array_string = string.chars + palindrome_chars = Hash.new(0) + array_string.each do |ch| + palindrome_chars[ch] += 1 + end + char_counts = palindrome_chars.values + puts char_counts + if string.length % 2 != 0 + middle_char = char_counts.find_index {|i| + i % 2 == 1 + } + if middle_char == nil + return false + end + char_counts.delete_at(middle_char) + end + return char_counts.all? { |ch_num| + ch_num % 2 == 0 || ch_num == 0 + } +end +palindrome_permutation?('carrace') diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..721bbb5 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,18 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + if string1.length != string2.length + return false + end + common_chars = Hash.new(0) + string1.chars.each do |ch| + common_chars[ch] += 1 + end + string2.chars.each do |ch| + if common_chars[ch] == 0 + return false + else + common_chars[ch] -= 1 + end + end + return true end \ No newline at end of file diff --git a/test/array_intersection_test.rb b/test/array_intersection_test.rb index 4830afe..6664772 100644 --- a/test/array_intersection_test.rb +++ b/test/array_intersection_test.rb @@ -1,5 +1,6 @@ require_relative "test_helper" + describe "Array Intersection" do it "returns [4] for [2, 3, 4], and [4, 5, 6]" do expect(intersection([2, 3, 4], [4, 5, 6])).must_equal [4] diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index 6e43777..246ccfd 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 eb1ce8c..4dcc9ee 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 @@ -9,7 +9,7 @@ expect(permutations?("hello", "ehllo")).must_equal true end - it "returns true for 'heelo', 'ehllo'" do + it "returns false for 'heelo', 'ehllo'" do expect(permutations?("heelo", "ehllo")).must_equal false end it "returns true for 'pasta', 'atsap'" do