From 26f78114a1be245364871940bba36005e6800391 Mon Sep 17 00:00:00 2001 From: Alex Robertson Date: Mon, 30 Mar 2020 18:27:28 -0700 Subject: [PATCH] Submitting Hashmap exercises. --- lib/array_intersection.rb | 10 +++++++++- lib/palindrome_permutation.rb | 26 ++++++++++++++++++++++++-- lib/permutations.rb | 21 ++++++++++++++++++++- test/palindrome_permutation_test.rb | 2 +- test/permutations_test.rb | 2 +- 5 files changed, 55 insertions(+), 6 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..dd60dc8 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,11 @@ +# Time Complexity: O(n) +# Space Complexity: O(n) + def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + crossroads = [] + + list1.each do |n| + crossroads << n if list2.include?(n) != false + end + return crossroads end \ No newline at end of file diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..6c18aee 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,26 @@ +# Time Complexity: O(n) +# Space Complexity: O(n) def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" -end \ No newline at end of file + while string.empty? + return true + end + + board = {} + letters = string.chars + chalk = 0 + + letters.each do |c| + board[c] = 0 + end + + letters.each do |c| + if board[c] + board[c] += 1 + end + if board[c] % 2 == 0 + chalk += 1 + end + end + return chalk == letters.length/2 +end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..ee60811 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,23 @@ +# Time Complexity: O(n) +# Space Complexity: O(n) def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + if string1.length != string2.length + return false + end + + perm = string1.chars + mutant = string2.chars + mindflayer = {} + + perm.each do |c| + mindflayer[c] = true + end + + mutant.each do |c| + if mindflayer[c] != true + 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..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