From 61c663e13f7f4d7b9ba6d15d2747a3f19dd513f1 Mon Sep 17 00:00:00 2001 From: charlotte adams Date: Tue, 24 Mar 2020 15:26:07 -0700 Subject: [PATCH 1/8] list1 is now in an array of hashes available for next step comparison --- lib/array_intersection.rb | 65 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..6b31bf2 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,64 @@ + + def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" -end \ No newline at end of file + intersection_array = [] + numbers = Hash.new + i = 0 + while list1.length > i + numbers = { + list1[i] => 1 + } + + i += 1 + intersection_array << numbers + end + + # [{2=>1}, {3=>1}, {4=>1}] + return intersection_array + +# [{2=>1}, {3=>1}, {4=>1}], [4,5,6] + def compare(intersection_array, list2) + i = 0 + j = 0 + while list2.length > j + if list2[j] == intersection_array[i].keys + numbers = { + intersection_array[i] => 2 + } + + elsif + numbers = { + list2[j] => 1 + } + + end + i += 1 + j += 1 + + end + return intersection_array + + + end + + +end + + + +p intersection([2,3,4], [4,5,6]) + +# find the intersection of 4 +# + + + + + + + + + +# 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]) => [] \ No newline at end of file From bec43d85873890b087e750dca86c2b09a8324622 Mon Sep 17 00:00:00 2001 From: charlotte adams Date: Wed, 25 Mar 2020 12:20:42 -0700 Subject: [PATCH 2/8] create method and pass corrresponding tests --- lib/array_intersection.rb | 64 ++++++--------------------------------- 1 file changed, 10 insertions(+), 54 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index 6b31bf2..8b4482d 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,64 +1,20 @@ def intersection(list1, list2) - intersection_array = [] - numbers = Hash.new + array = Array.new + nums= Hash.new i = 0 - while list1.length > i - numbers = { - list1[i] => 1 - } + while list1.length > i + nums[list1[i]] = true i += 1 - intersection_array << numbers end - # [{2=>1}, {3=>1}, {4=>1}] - return intersection_array - -# [{2=>1}, {3=>1}, {4=>1}], [4,5,6] - def compare(intersection_array, list2) - i = 0 - j = 0 - while list2.length > j - if list2[j] == intersection_array[i].keys - numbers = { - intersection_array[i] => 2 - } - - elsif - numbers = { - list2[j] => 1 - } - - end - i += 1 - j += 1 - - end - return intersection_array - - + list2.each do |element| + # the value at nums[element] is going to be nil or true + if nums[element] + array << element + end end - - + return array end - - - -p intersection([2,3,4], [4,5,6]) - -# find the intersection of 4 -# - - - - - - - - - -# 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]) => [] \ No newline at end of file From 4de765caee5e67be01238273581c4ff9ee45a376 Mon Sep 17 00:00:00 2001 From: charlotte adams Date: Wed, 25 Mar 2020 16:29:12 -0700 Subject: [PATCH 3/8] Create permutation? method (Brute Force). All tests passing. --- lib/permutations.rb | 58 +++++++++++++++++++++++++++++++++++++-- test/permutations_test.rb | 2 +- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..0d282f5 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,58 @@ + def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" -end \ No newline at end of file + letters1 = Hash.new + letters2 = Hash.new + i = 0 + j = 0 + + while string1.length > i + + if letters1.key?(string1[i]) + # can use this instead: letters[string1[i]] += 1 + letters1[string1[i]] = letters1[string1[i]] + 1 + else + letters1[string1[i]] = 1 + + end + i += 1 + end + + p letters1 + + while string2.length > j + + if letters2.key?(string2[j]) + # can use this instead: letters[string1[i]] += 1 + letters2[string2[j]] = letters2[string2[j]] + 1 + else + letters2[string2[j]] = 1 + + end + j+= 1 + end + p letters2 + + if letters1 != letters2 + false + else + true + end + + +end + + +# look at first letter in string1 +# can i find this letter in string2 +# if yes...cross of letter in string2 +# if no...then not a permutation +# go to to second letter in string1 +# repeat +# reach end...if all letters are crossed off in string2, then a match. + + + + + + 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 1867f571cb33efff79be498d653d7dc529d0178f Mon Sep 17 00:00:00 2001 From: charlotte adams Date: Thu, 26 Mar 2020 11:25:16 -0700 Subject: [PATCH 4/8] refactor --- lib/permutations.rb | 67 ++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 49 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 0d282f5..35ad223 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,58 +1,27 @@ - - def permutations?(string1, string2) - letters1 = Hash.new - letters2 = Hash.new - i = 0 - j = 0 - - while string1.length > i - - if letters1.key?(string1[i]) - # can use this instead: letters[string1[i]] += 1 - letters1[string1[i]] = letters1[string1[i]] + 1 - else - letters1[string1[i]] = 1 - - end - i += 1 - end - - p letters1 - while string2.length > j - - if letters2.key?(string2[j]) - # can use this instead: letters[string1[i]] += 1 - letters2[string2[j]] = letters2[string2[j]] + 1 - else - letters2[string2[j]] = 1 - - end - j+= 1 + def str_to_hash(string) + letters = Hash.new + i = 0 + + while string.length > i + if letters.key?(string[i]) + letters[string[i]] += 1 + else + letters[string[i]] = 1 + end + i += 1 + end + letters end - p letters2 - if letters1 != letters2 + string1_hash = str_to_hash(string1) + string2_hash = str_to_hash(string2) + + if string1_hash != string2_hash false else true end - -end - - -# look at first letter in string1 -# can i find this letter in string2 -# if yes...cross of letter in string2 -# if no...then not a permutation -# go to to second letter in string1 -# repeat -# reach end...if all letters are crossed off in string2, then a match. - - - - - - +end \ No newline at end of file From 7c3e08c5571a7dcd6cd6215fbf03991b45de9274 Mon Sep 17 00:00:00 2001 From: charlotte adams Date: Thu, 26 Mar 2020 11:43:42 -0700 Subject: [PATCH 5/8] file clean up after installing rubocop and some refactoring --- lib/array_intersection.rb | 14 +++++--------- lib/permutations.rb | 30 ++++++++++++++---------------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index 8b4482d..97f85a2 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,20 +1,16 @@ - - def intersection(list1, list2) - array = Array.new - nums= Hash.new + array = [] + nums = {} i = 0 - while list1.length > i + while list1.length > i nums[list1[i]] = true i += 1 end list2.each do |element| # the value at nums[element] is going to be nil or true - if nums[element] - array << element - end + array << element if nums[element] end - return array + array end diff --git a/lib/permutations.rb b/lib/permutations.rb index 35ad223..a3901d7 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,27 +1,25 @@ def permutations?(string1, string2) def str_to_hash(string) - letters = Hash.new - i = 0 - - while string.length > i - if letters.key?(string[i]) - letters[string[i]] += 1 - else - letters[string[i]] = 1 - end - i += 1 + letters = {} + i = 0 + while string.length > i + if letters.key?(string[i]) + letters[string[i]] += 1 + else + letters[string[i]] = 1 end - letters + i += 1 + end + letters end - string1_hash = str_to_hash(string1) - string2_hash = str_to_hash(string2) - + string1_hash = str_to_hash(string1) + string2_hash = str_to_hash(string2) + if string1_hash != string2_hash false else true end - -end \ No newline at end of file +end From f34d9503a4899752c4006931febd51c5d43f58dc Mon Sep 17 00:00:00 2001 From: charlotte adams Date: Thu, 26 Mar 2020 12:06:32 -0700 Subject: [PATCH 6/8] palindrome_permutataion? tests 1,2,3,4 passing --- lib/palindrome_permutation.rb | 19 ++++++++++++++++--- test/palindrome_permutation_test.rb | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..981a95b 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,17 @@ - def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" -end \ No newline at end of file + return true if string.empty? + + # create a new hash which will hold counts of chars + letters = Hash.new(0) # iterate through the string, populate the hash + string.each_char do |char| + letters[char] += 1 + end # check the hash for chars that appear an odd number of times + odd_count = 0 + letters.each do |_index, value| + # add to your odd char counter + if value % 2 == 0 + odd_count += 1 + end + end + odd_count > 1 +end 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 8d37d50a07bb4e87df510fb00ea28752cffaf928 Mon Sep 17 00:00:00 2001 From: charlotte adams Date: Thu, 26 Mar 2020 19:00:08 -0700 Subject: [PATCH 7/8] final test in palindrome-permutaion passing. clean files. leave proper comments. --- lib/array_intersection.rb | 3 +++ lib/palindrome_permutation.rb | 30 ++++++++++++++++------------- lib/permutations.rb | 8 ++++++-- test/palindrome_permutation_test.rb | 5 +++-- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index 97f85a2..cdcca64 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -4,12 +4,15 @@ def intersection(list1, list2) i = 0 while list1.length > i + # creating nums hash with number as key and value as true nums[list1[i]] = true i += 1 end list2.each do |element| # the value at nums[element] is going to be nil or true + # check if list2[element] is equal to a key in the nums hash + # if it is equal, push element into array b/c that is an intersection. array << element if nums[element] end array diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index 981a95b..39ed803 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,17 +1,21 @@ + def palindrome_permutation?(string) return true if string.empty? - # create a new hash which will hold counts of chars - letters = Hash.new(0) # iterate through the string, populate the hash - string.each_char do |char| - letters[char] += 1 - end # check the hash for chars that appear an odd number of times - odd_count = 0 - letters.each do |_index, value| - # add to your odd char counter - if value % 2 == 0 - odd_count += 1 - end - end - odd_count > 1 + # create a new hash which will hold counts of characters + # set the default value of all keys to 0 + letters = Hash.new(0) + string.each_char do |char| + letters[char] += 1 + end + + odd_count = 0 + # check the hash for characters that appear an odd number of times + # the varialbe - key - is not being used thus _key + letters.each do |_key, value| + # add to odd character count + odd_count += 1 if value.odd? + end + odd_count <= 1 + end diff --git a/lib/permutations.rb b/lib/permutations.rb index a3901d7..7f382f9 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -4,19 +4,23 @@ def str_to_hash(string) letters = {} i = 0 while string.length > i + # check if string[index] is already a key in letters hash if letters.key?(string[i]) + # if already a key, increment value at said key by 1 letters[string[i]] += 1 else + # if NOT already a key, set the string[index] as a key in letters hash with a value of 1 letters[string[i]] = 1 end + # increment loop i += 1 end letters end - +# call helper method for input strings string1_hash = str_to_hash(string1) string2_hash = str_to_hash(string2) - +# compare hashes for equality if string1_hash != string2_hash false else diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index 984b333..9f1ef22 100644 --- a/test/palindrome_permutation_test.rb +++ b/test/palindrome_permutation_test.rb @@ -19,5 +19,6 @@ it "will return false for raceca" do expect(palindrome_permutation?("raceca")).must_equal false - end -end \ No newline at end of file + end + +end From 6f9051bf2a6358279d1ca15d8b77f7f0baaf4935 Mon Sep 17 00:00:00 2001 From: charlotte adams Date: Sun, 29 Mar 2020 20:54:38 -0700 Subject: [PATCH 8/8] clean up --- lib/permutations.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 7f382f9..4259e29 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -17,10 +17,10 @@ def str_to_hash(string) end letters end -# call helper method for input strings +# call helper method on input strings string1_hash = str_to_hash(string1) string2_hash = str_to_hash(string2) -# compare hashes for equality +# compare hashes if string1_hash != string2_hash false else