diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..6ed8ff4 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,45 @@ +## Array Intersection + +# Design and implement a method that takes two integer arrays with unique values and returns their intersection in a new array. + +# For example: + +# ``` +# intersection([2, 3, 4], [4, 5, 6]) => [4] +# intersection([50, 43, 25, 72], [25, 36, 43, 50, 80]) => [50, 25, 43] +# intersection([9, 30, 42], [56, 34, 90, 32]) => [] +# ``` + + + def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + shortlist = [] + longest = [] + if list1.length < list2.length + shortlist = list1 + longest = list2 + else + shortlist = list2 + longest = list1 + end + + hash = {} + shortlist.each do |item| + if + longest.include?(item) + hash[item] = true + else + hash[item] = false + end + end + + list3 = [] + hash.keys.each do |key| + if + hash[key] == true + list3<< key + end + end + + return list3 end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..61372c3 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,27 @@ +## Could Be A Palindrome + +# Write a method which takes a string as an argument and returns true if the letters could be re-arranged into a palindrome. + +# ``` +# palindrome_permutation?("hello") => false +# palindrome_permutation?("carrace") => true # because racecar is a palindrome +# ``` + def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + count = {} + x = 0 + string.each_char do |char| + count[char]= string.count(char) + end + count.keys.each do |key| + if count[key]%2 != 0 + x +=1 + end + end + if x == 0 || x == 1 + return true + else + return false + end end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..6bb610a 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,46 @@ -def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" +## Check Permutations + +# Write a method which will take two strings as arguments and returns true if one string is a permutation of the other. + +# ``` +# permutations?("hello", "ehllo") => true +# permutations?("pasta", "atsap") => true +# permutations?("Pizza", "Pasta") => false +# permutations?("", "") => true +# ``` + + +def permutations?(string1, string2) + hash = {} + if string1.length == string2.length + string2.each_char do |char| + if string1.include?(char) + if hash[char] == true + repeated = char+char + hash[repeated] = true + else + hash[char] = true + end + else + hash[char]= false + end + end + else + return false + end + + occurences = 0 + hash.keys.each do |key| + if hash[key] == true + occurences +=1 + end + end + + if occurences == string1.length + return true + else + return false + end + 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