From 92b1144c519bb327b5f04bffc4a544ea7328f319 Mon Sep 17 00:00:00 2001 From: Sara Nilsen Date: Sat, 28 Mar 2020 17:13:52 -0700 Subject: [PATCH 1/2] all exercises --- lib/array_intersection.rb | 23 ++++++++++++++++++- lib/palindrome_permutation.rb | 35 +++++++++++++++++++++++++++-- lib/permutations.rb | 13 ++++++++++- test/palindrome_permutation_test.rb | 3 ++- test/permutations_test.rb | 2 +- 5 files changed, 70 insertions(+), 6 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..7443cf4 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,24 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + longest_list = list1 #default longest list, it changes if list2 is longest than list1 + other_list = list2 +#-------------------------------------------# + if list1.length < list2.length + longest_list = list2 + other_list = list1 + end +#-------------------------------------------# + hash_table = {} #for comparing values + longest_list.each do |n| + hash_table[n] = 1 + end +#-------------------------------------------# + intersection = [] #the intersection numbers + other_list.each do |n| + if hash_table.key?(n) + intersection << n + hash_table[n] += 1 + end + end +#-------------------------------------------# + return intersection end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..af9a7e5 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,35 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" -end \ No newline at end of file + return true if string == "" + + hash_map = Hash.new(0) + (string.chars).each do |s| + hash_map[s] += 1 + end + + odd = 0 + hash_map.each_value do |v| + if v % 2 != 0 + odd += 1 + end + end + return odd > 1 ? false : true +end + + + + + + + + +# if string.length % 2 == 0 #when string size is even every value should be == 2 to be permutation +# hash_map.each do |l, v| +# if v != 2 +# return false +# end +# else +# hash_map.each do |l, v| + +# end +# end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..c87e3a5 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,15 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + return false if string1.length != string2.length + hash_table = {} + (string1.chars).each do |s| + hash_table[s] = 1 + end + #-------------------------------------------------# + (string2.chars).each do |s| + if !hash_table.key?(s) + return false + end + end + return true end \ No newline at end of file diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index e9119de..f952b96 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 @@ -20,4 +20,5 @@ it "will return false for raceca" do expect(palindrome_permutation?("raceca")).must_equal false end + end \ No newline at end of file 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 90b2a990339a8f8cdebe26b3950ce0fceed2707e Mon Sep 17 00:00:00 2001 From: Sara Nilsen Date: Wed, 1 Apr 2020 23:24:34 -0700 Subject: [PATCH 2/2] made changes to permutations --- lib/permutations.rb | 19 ++++++++++++------- test/permutations_test.rb | 3 +++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index c87e3a5..400fef5 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,15 +1,20 @@ def permutations?(string1, string2) return false if string1.length != string2.length - hash_table = {} + first_hash = Hash.new(0) (string1.chars).each do |s| - hash_table[s] = 1 + first_hash[s] += 1 end + + puts first_hash #-------------------------------------------------# + second_hash = Hash.new(0) (string2.chars).each do |s| - if !hash_table.key?(s) - return false - end + second_hash[s] += 1 end - return true -end \ No newline at end of file + + first_hash == second_hash ? true : false +end + +#how can I check if two strings have the same number of letters + diff --git a/test/permutations_test.rb b/test/permutations_test.rb index 5f6ae77..4e76117 100644 --- a/test/permutations_test.rb +++ b/test/permutations_test.rb @@ -20,4 +20,7 @@ it "returns false if the number of a specific letter are different" do expect(permutations?("pizza", "piza")).must_equal false end + it "returns false for hellllllllll" do + expect(permutations?("hellllllll", "hel")).must_equal false + end end \ No newline at end of file