From e6b6e8aa18cce059b9c62328fcf82f7ce25a837b Mon Sep 17 00:00:00 2001 From: Tithvorlak Mok Date: Sat, 28 Mar 2020 21:03:10 -0700 Subject: [PATCH 1/6] compeleted array-intersection --- lib/array_intersection.rb | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..f007d85 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,36 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" -end \ No newline at end of file + + #create a hash with each element of the array as keys and value as true + hash = {} + list1.each do |element| + hash[element] = true + end + #find each element of list2 by the hash's keys + result = [] + list2.each do |element| + if hash[element] == true + result << element + end + end + return result +end + +# Without using the hash table +# def intersection(list1, list2) +# result = [] +# if list1.length >= list2.length +# list1.length.times do |i| +# if list1.include?(list2[i]) +# result << list2[i] +# end +# end +# return result +# else +# list2.length.times do |i| +# if list2.include?(list1[i]) +# result << list1[i] +# end +# end +# return result +# end +# end \ No newline at end of file From ec658ddf6183a7b87cb6d58a87a215f0821a70f9 Mon Sep 17 00:00:00 2001 From: Tithvorlak Mok Date: Sun, 29 Mar 2020 00:58:51 -0700 Subject: [PATCH 2/6] completed palindrome --- lib/palindrome_permutation.rb | 27 ++++++++++++++++++++++++++- test/palindrome_permutation_test.rb | 2 +- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..28f720b 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,29 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + return true if string == "" + + #Convert the string to an array of letters + string_array = string.chars + + #reverse_string_array + reverse_string_array = [] + string_array.each do |letter| + reverse_string_array.unshift(letter) + end + + #create a string_hash by having each element string_array as keys + string_hash = {} + + string_array.length.times do |i| + string_hash[string_array[i]] = reverse_string_array[i] + end + print string_hash + + #compare each key and value of the hash + string_hash.each do |key, value| + if value == key + return true + end + end + return false 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 From aadd1b83d7372cfbc22631720a429f71845da0ac Mon Sep 17 00:00:00 2001 From: Tithvorlak Mok Date: Sun, 29 Mar 2020 01:51:06 -0700 Subject: [PATCH 3/6] completed permutations --- lib/permutations.rb | 22 +++++++++++++++++++++- test/permutations_test.rb | 4 ++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..2550374 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,24 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + return true if string1 == "" && string2 == "" + return false if (string1.length != string2.length) + + + #convert both strings to arrays of letters + array1 = string1.chars + array2 = string2.chars + + #create a hash by assigning each element of array1 for the keys of hash + + hash = {} + array1.each do |letter| + hash[letter] = true + end + + array2.each do |letter| + if hash[letter] != true + return false + end + end + return true end \ No newline at end of file diff --git a/test/permutations_test.rb b/test/permutations_test.rb index 79da2f6..ada5ee1 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 @@ -13,7 +13,7 @@ expect(permutations?("pasta", "atsap")).must_equal true end - it "returns true for 'pizza', 'pizza'" do + it "returns true for 'pizza', 'pasta'" do expect(permutations?("pizza", "pasta")).must_equal false end From 321b1cd2d30717f1fe4017b4559d1f398a70d809 Mon Sep 17 00:00:00 2001 From: Tithvorlak Mok Date: Sun, 29 Mar 2020 02:01:32 -0700 Subject: [PATCH 4/6] added time and space complexity --- lib/array_intersection.rb | 2 ++ lib/palindrome_permutation.rb | 2 ++ lib/permutations.rb | 3 ++- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index f007d85..2bad0d6 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,5 @@ +#Time complexity: O(n+m) +#Space complexity: O(n) def intersection(list1, list2) #create a hash with each element of the array as keys and value as true diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index 28f720b..a28b22d 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,3 +1,5 @@ +#Time complexity: O(n+m) +#Space complexity: O(n) def palindrome_permutation?(string) return true if string == "" diff --git a/lib/permutations.rb b/lib/permutations.rb index 2550374..371700f 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,5 @@ - +#Time complexity: O(n+m) +#Space complexity: O(n) def permutations?(string1, string2) return true if string1 == "" && string2 == "" return false if (string1.length != string2.length) From 05f9c96cde3064247f428f052e53a4cd46017483 Mon Sep 17 00:00:00 2001 From: Tithvorlak Mok Date: Sun, 29 Mar 2020 16:47:18 -0700 Subject: [PATCH 5/6] changed time/space complextity --- lib/palindrome_permutation.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index a28b22d..fdb6f22 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,5 +1,5 @@ -#Time complexity: O(n+m) -#Space complexity: O(n) +#Time complexity: O(n+m+l) +#Space complexity: O(n^2) def palindrome_permutation?(string) return true if string == "" @@ -19,11 +19,10 @@ def palindrome_permutation?(string) string_array.length.times do |i| string_hash[string_array[i]] = reverse_string_array[i] end - print string_hash - + #compare each key and value of the hash string_hash.each do |key, value| - if value == key + if key == value return true end end From 90e54da7e68c97bda5a09a8cff06c8cf0b0dd795 Mon Sep 17 00:00:00 2001 From: Tithvorlak Mok Date: Sun, 29 Mar 2020 16:51:20 -0700 Subject: [PATCH 6/6] deleted an unneeded line of code! --- lib/permutations.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 371700f..54429d5 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,10 +1,8 @@ #Time complexity: O(n+m) #Space complexity: O(n) def permutations?(string1, string2) - return true if string1 == "" && string2 == "" return false if (string1.length != string2.length) - #convert both strings to arrays of letters array1 = string1.chars array2 = string2.chars