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..400fef5 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 + return false if string1.length != string2.length + first_hash = Hash.new(0) + (string1.chars).each do |s| + first_hash[s] += 1 + end + + puts first_hash + #-------------------------------------------------# + second_hash = Hash.new(0) + (string2.chars).each do |s| + second_hash[s] += 1 + end + + first_hash == second_hash ? true : false +end + +#how can I check if two strings have the same number of letters + 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..4e76117 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 @@ -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