From fdddac4ce1a839dc40b9fa6dc42933583afd679f Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 11:31:03 -0700 Subject: [PATCH 01/22] adds pseudocode --- lib/array_intersection.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index ac8771f..b444f39 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,3 +1,5 @@ def intersection(list1, list2) - raise NotImplementedError, "Intersection not implemented" + # Add each element from the smaller array to the hash table. + # Lookup each element in the larger array in the hash table. + # If it’s found in the hash table, add it to the results array. end \ No newline at end of file From ad47667674d54c6792113fa3eb148c64182a3531 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 12:16:33 -0700 Subject: [PATCH 02/22] defines intersection method, all tests passing --- lib/array_intersection.rb | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index b444f39..2e0ed5a 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,5 +1,22 @@ def intersection(list1, list2) - # Add each element from the smaller array to the hash table. - # Lookup each element in the larger array in the hash table. - # If it’s found in the hash table, add it to the results array. + # define which array is the smallest and which is the largest + list1.length < list2.length ? (smaller, larger = list1, list2) : (smaller, larger = list2, list1) + + # Add each element from the smaller array to the hash table + lookup_hash = {} + smaller.each do |num| + if lookup_hash[num] + lookup_hash[num] = false + else + lookup_hash[num] = true + end + end + + # Lookup each element in the larger array in the hash table; if it’s found in the hash table, add it to the results array + results = [] + larger.each do |num| + results << num if lookup_hash[num] + end + + return results end \ No newline at end of file From 3fc73a8d85f3340c8e0acb8894f253e56999f208 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 12:23:34 -0700 Subject: [PATCH 03/22] fixes erroneous act statement in test 'returns true for 'pizza', 'pizza' --- test/permutations_test.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/permutations_test.rb b/test/permutations_test.rb index 79da2f6..39caeb4 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 @@ -14,6 +14,10 @@ end it "returns true for 'pizza', 'pizza'" do + expect(permutations?("pizza", "pizza")).must_equal true + end + + it "returns false for 'pizza', 'pasta'" do expect(permutations?("pizza", "pasta")).must_equal false end From a76ff642623efec7fd48f133e5b3cf0bc2ece7c4 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 12:23:53 -0700 Subject: [PATCH 04/22] adds pseudocode --- lib/permutations.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b08381..0ec51ae 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,4 +1,7 @@ def permutations?(string1, string2) - raise NotImplementedError, "permutations? not implemented" + # return false if the strings are not the same length + # define which string is smaller/larger + # add each char from the smaller string to the hash table + # lookup each char in the larger string in the hash table; if it's found, end \ No newline at end of file From c771fabab8a4e16e1bfea1e86f2c901083c1c24d Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 12:27:08 -0700 Subject: [PATCH 05/22] amends pseudocode, checks if strings are equal or not of same length --- lib/permutations.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 0ec51ae..6bb461a 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,7 +1,9 @@ def permutations?(string1, string2) - # return false if the strings are not the same length - # define which string is smaller/larger - # add each char from the smaller string to the hash table - # lookup each char in the larger string in the hash table; if it's found, + # return true if strings are equal; return false if the strings are not the same length + return true if string1 == string2 + return false if string1.length != string2.length + + # add each char from string1 to the hash table + # lookup each char in string2 in the hash table; if it's found, return true; else return false end \ No newline at end of file From 779eac3a09ec04487cb51950af0286a04b77fe6c Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 12:29:49 -0700 Subject: [PATCH 06/22] adds each char from string1 to the hash table --- lib/permutations.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/permutations.rb b/lib/permutations.rb index 6bb461a..5f9e369 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -5,5 +5,13 @@ def permutations?(string1, string2) return false if string1.length != string2.length # add each char from string1 to the hash table + lookup_hash = {} + string1.each_char do |char| + if lookup_hash[char] + lookup_hash[char] = false + else + lookup_hash[char] = true + end + end # lookup each char in string2 in the hash table; if it's found, return true; else return false end \ No newline at end of file From 02451ba13a766d5c18b9d1000ecbf0641545d828 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 12:41:29 -0700 Subject: [PATCH 07/22] looks up each char in string2 in the hash table, all tests passing --- lib/permutations.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 5f9e369..bf99fbe 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -13,5 +13,11 @@ def permutations?(string1, string2) lookup_hash[char] = true end end - # lookup each char in string2 in the hash table; if it's found, return true; else return false + + # lookup each char in string2 in the hash table; if the value of lookup_hash[num] is nil, return false; else return true + string2.each_char do |char| + return false if lookup_hash[char].nil? + end + + return true end \ No newline at end of file From ef84fadff4f57e3a0685a8463ea299801f0ac148 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 12:48:13 -0700 Subject: [PATCH 08/22] removes x from describe --- test/palindrome_permutation_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 509e6aec1f4fbc0dd64d8ea909d7d1938697c974 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 12:48:27 -0700 Subject: [PATCH 09/22] adds pseudocode and comments on logic --- lib/palindrome_permutation.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index f113692..24b11b4 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,4 +1,6 @@ def palindrome_permutation?(string) - raise NotImplementedError, "palindrome_permutation? not implemented" + # return true if the string is empty + # if the string has an even number of characters, the palindrome should have two of each letter + # if the string has an odd number of characters, the palindrome should have only one odd letter out end \ No newline at end of file From ee122063ae818d0a4bcdb4e94fc857f82ae6162c Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 13:02:42 -0700 Subject: [PATCH 10/22] fixes name of test for 'hello', adds test for an even-length palindrome --- test/palindrome_permutation_test.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index 984b333..99452d3 100644 --- a/test/palindrome_permutation_test.rb +++ b/test/palindrome_permutation_test.rb @@ -1,7 +1,7 @@ require_relative "test_helper" describe "palindrome_permutation?" do - it "will work for hello" do + it "will return false for hello" do expect(palindrome_permutation?("hello")).must_equal false end @@ -9,6 +9,10 @@ expect(palindrome_permutation?("carrace")).must_equal true end + it "will work for 'noon'" do + expect(palindrome_permutation?("noon")).must_equal true + end + it "will work for emptystring" do expect(palindrome_permutation?("")).must_equal true end From cb8e1693aa233987ac252a08a00fc35dbe9a5f2c Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 13:03:25 -0700 Subject: [PATCH 11/22] adds each char in string to the hash table, works for even-length palindromes --- lib/palindrome_permutation.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index 24b11b4..72f959e 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,6 +1,20 @@ def palindrome_permutation?(string) - # return true if the string is empty + return true if string.empty? + + # adds each char in string to the hash table + lookup_hash = {} + string.each_char do |char| + if lookup_hash[char] + lookup_hash[char] = false + else + lookup_hash[char] = true + end + end + # if the string has an even number of characters, the palindrome should have two of each letter + if (string.length).even? + return true if lookup_hash.length == string.length / 2 + end # if the string has an odd number of characters, the palindrome should have only one odd letter out end \ No newline at end of file From bb9764b3b79e4a8a851bd93d6179dee6cda9a440 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 13:16:17 -0700 Subject: [PATCH 12/22] leverages collisions to check if the hash table is the expected length depending on whether the string length is odd or even, passes all tests --- lib/palindrome_permutation.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index 72f959e..c6efeb0 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -11,10 +11,12 @@ def palindrome_permutation?(string) lookup_hash[char] = true end end - - # if the string has an even number of characters, the palindrome should have two of each letter - if (string.length).even? + + if string.length.even? # if the string has an even number of characters, the palindrome should have two of each letter return true if lookup_hash.length == string.length / 2 + elsif string.length.odd? # if the string has an odd number of characters, the palindrome should have only one odd letter out + return true if lookup_hash.length == (string.length / 2) + 1 end - # if the string has an odd number of characters, the palindrome should have only one odd letter out + + return false end \ No newline at end of file From 0fce2d8705176f6232e43c683b92c55c13fde23c Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 13:20:59 -0700 Subject: [PATCH 13/22] modifies test for 'returns false if the number of a specific letter are different', see comments --- test/permutations_test.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/permutations_test.rb b/test/permutations_test.rb index 39caeb4..e0ca0ea 100644 --- a/test/permutations_test.rb +++ b/test/permutations_test.rb @@ -21,7 +21,13 @@ expect(permutations?("pizza", "pasta")).must_equal false end + # this test was passing superficially because my method returned false if the strings were different lengths; I modified the second test string so it would still meet the conditions but not trigger line 4 in the method it "returns false if the number of a specific letter are different" do + expect(permutations?("pizza", "pizaa")).must_equal false + end + + # see above comment + it "returns false if the strings are of different lengths" do expect(permutations?("pizza", "piza")).must_equal false end end \ No newline at end of file From a1c47961df2ee77cb83758c4f406c08aff3edbfc Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 15:27:53 -0700 Subject: [PATCH 14/22] fixes mislabeled it block and adds follow-up test for when the number of specific letters are different --- test/permutations_test.rb | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/test/permutations_test.rb b/test/permutations_test.rb index e0ca0ea..4cf7285 100644 --- a/test/permutations_test.rb +++ b/test/permutations_test.rb @@ -13,21 +13,16 @@ expect(permutations?("pasta", "atsap")).must_equal true end - it "returns true for 'pizza', 'pizza'" do - expect(permutations?("pizza", "pizza")).must_equal true - end - it "returns false for 'pizza', 'pasta'" do expect(permutations?("pizza", "pasta")).must_equal false end - # this test was passing superficially because my method returned false if the strings were different lengths; I modified the second test string so it would still meet the conditions but not trigger line 4 in the method + # this test was passing superficially because of line 4 in my method; I added a follow-up test below it "returns false if the number of a specific letter are different" do - expect(permutations?("pizza", "pizaa")).must_equal false - end - - # see above comment - it "returns false if the strings are of different lengths" do expect(permutations?("pizza", "piza")).must_equal false end + + it "returns false if the number of a specific letter are different when the strings lengths are the same" do + expect(permutations?("pizza", "pizaa")).must_equal false + end end \ No newline at end of file From f9ea6ec8db9c3226d50806b44e9028155e818828 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 15:33:38 -0700 Subject: [PATCH 15/22] fixes typo --- test/permutations_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/permutations_test.rb b/test/permutations_test.rb index 4cf7285..2aecc90 100644 --- a/test/permutations_test.rb +++ b/test/permutations_test.rb @@ -22,7 +22,7 @@ expect(permutations?("pizza", "piza")).must_equal false end - it "returns false if the number of a specific letter are different when the strings lengths are the same" do + it "returns false if the number of a specific letter are different but the string lengths are the same" do expect(permutations?("pizza", "pizaa")).must_equal false end end \ No newline at end of file From 1e9354176f1fb20ea59de330f341809916024f95 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 15:38:59 -0700 Subject: [PATCH 16/22] attempts to pass test 'returns false if the number of a specific letter are different, but the string lengths are the same', 2 failures in other tests --- lib/permutations.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index bf99fbe..3b7eb03 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,9 +1,8 @@ - def permutations?(string1, string2) # return true if strings are equal; return false if the strings are not the same length return true if string1 == string2 return false if string1.length != string2.length - + # add each char from string1 to the hash table lookup_hash = {} string1.each_char do |char| @@ -14,6 +13,8 @@ def permutations?(string1, string2) end end + return false if string1 != string2 && string1.squeeze.length == string2.squeeze.length # returns false if the number of a specific letter are different, but the string lengths are the same + # lookup each char in string2 in the hash table; if the value of lookup_hash[num] is nil, return false; else return true string2.each_char do |char| return false if lookup_hash[char].nil? From 95e846498f5bed294cdcf0c8431446245bf6766c Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 16:16:13 -0700 Subject: [PATCH 17/22] counts how many of each letter are in each string, compares hash tables, passes all tests! --- lib/permutations.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 3b7eb03..1831131 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -5,16 +5,33 @@ def permutations?(string1, string2) # add each char from string1 to the hash table lookup_hash = {} + letter_count1 = {} string1.each_char do |char| if lookup_hash[char] lookup_hash[char] = false else lookup_hash[char] = true end + + if letter_count1[char] + letter_count1[char] += 1 + else + letter_count1[char] = 0 + end end - return false if string1 != string2 && string1.squeeze.length == string2.squeeze.length # returns false if the number of a specific letter are different, but the string lengths are the same - + letter_count2 = {} + string2.each_char do |char| + if letter_count2[char] + letter_count2[char] += 1 + else + letter_count2[char] = 0 + end + end + + # returns false if the number of a specific letter are different, but the string lengths are the same + return false if letter_count1 != letter_count2 + # lookup each char in string2 in the hash table; if the value of lookup_hash[num] is nil, return false; else return true string2.each_char do |char| return false if lookup_hash[char].nil? From 08d656479bbb3c97c74142b5171eea3ee4f47c83 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Mon, 30 Mar 2020 16:38:06 -0700 Subject: [PATCH 18/22] refactors code, all tests passing --- lib/array_intersection.rb | 13 ++++--------- lib/palindrome_permutation.rb | 19 ++++++------------- lib/permutations.rb | 25 ++++++------------------- 3 files changed, 16 insertions(+), 41 deletions(-) diff --git a/lib/array_intersection.rb b/lib/array_intersection.rb index 2e0ed5a..5db7b44 100644 --- a/lib/array_intersection.rb +++ b/lib/array_intersection.rb @@ -1,18 +1,13 @@ def intersection(list1, list2) - # define which array is the smallest and which is the largest - list1.length < list2.length ? (smaller, larger = list1, list2) : (smaller, larger = list2, list1) + list1.length < list2.length ? (smaller, larger = list1, list2) : (smaller, larger = list2, list1) # define which array is the smallest and which is the largest - # Add each element from the smaller array to the hash table + # add each element from the smaller array to the hash table lookup_hash = {} smaller.each do |num| - if lookup_hash[num] - lookup_hash[num] = false - else - lookup_hash[num] = true - end + lookup_hash[num] ? lookup_hash[num] = false : lookup_hash[num] = true end - # Lookup each element in the larger array in the hash table; if it’s found in the hash table, add it to the results array + # lookup each element in the larger array in the hash table; add to results array if found results = [] larger.each do |num| results << num if lookup_hash[num] diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index c6efeb0..8ab3aa0 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -1,22 +1,15 @@ - def palindrome_permutation?(string) return true if string.empty? # adds each char in string to the hash table lookup_hash = {} string.each_char do |char| - if lookup_hash[char] - lookup_hash[char] = false - else - lookup_hash[char] = true - end - end - - if string.length.even? # if the string has an even number of characters, the palindrome should have two of each letter - return true if lookup_hash.length == string.length / 2 - elsif string.length.odd? # if the string has an odd number of characters, the palindrome should have only one odd letter out - return true if lookup_hash.length == (string.length / 2) + 1 + lookup_hash[char] ? lookup_hash[char] = false : lookup_hash[char] = true end - return false + if string.length.even? # the palindrome should have two of each letter + return lookup_hash.length == string.length / 2 + elsif string.length.odd? # the palindrome should have only one odd letter out + return lookup_hash.length == (string.length / 2) + 1 + end end \ No newline at end of file diff --git a/lib/permutations.rb b/lib/permutations.rb index 1831131..9fc833f 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -1,5 +1,4 @@ def permutations?(string1, string2) - # return true if strings are equal; return false if the strings are not the same length return true if string1 == string2 return false if string1.length != string2.length @@ -7,32 +6,20 @@ def permutations?(string1, string2) lookup_hash = {} letter_count1 = {} string1.each_char do |char| - if lookup_hash[char] - lookup_hash[char] = false - else - lookup_hash[char] = true - end + lookup_hash[char] ? lookup_hash[char] = false : lookup_hash[char] = true - if letter_count1[char] - letter_count1[char] += 1 - else - letter_count1[char] = 0 - end + letter_count1[char] ? letter_count1[char] += 1 : letter_count1[char] = 0 # counts how many of each letter are in string1 end + # counts how many of each letter are in string2 letter_count2 = {} string2.each_char do |char| - if letter_count2[char] - letter_count2[char] += 1 - else - letter_count2[char] = 0 - end + letter_count2[char] ? letter_count2[char] += 1 : letter_count2[char] = 0 end - # returns false if the number of a specific letter are different, but the string lengths are the same - return false if letter_count1 != letter_count2 + return false if letter_count1 != letter_count2 # returns false if the number of a specific letter are different but the string lengths are the same (see comment in test file) - # lookup each char in string2 in the hash table; if the value of lookup_hash[num] is nil, return false; else return true + # lookup each char in string2 in the hash table string2.each_char do |char| return false if lookup_hash[char].nil? end From 725f959164fe4148b17ec2476f877a5c957367d1 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Wed, 1 Apr 2020 21:29:54 -0700 Subject: [PATCH 19/22] adds test and method for handling palindromes of any length with the same letter --- lib/palindrome_permutation.rb | 4 +++- test/palindrome_permutation_test.rb | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/palindrome_permutation.rb b/lib/palindrome_permutation.rb index 8ab3aa0..be540a0 100644 --- a/lib/palindrome_permutation.rb +++ b/lib/palindrome_permutation.rb @@ -7,7 +7,9 @@ def palindrome_permutation?(string) lookup_hash[char] ? lookup_hash[char] = false : lookup_hash[char] = true end - if string.length.even? # the palindrome should have two of each letter + if lookup_hash.length == 1 # palindromes of any length that are made up of the same letter + return true + elsif string.length.even? # the palindrome should have two of each letter return lookup_hash.length == string.length / 2 elsif string.length.odd? # the palindrome should have only one odd letter out return lookup_hash.length == (string.length / 2) + 1 diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index 99452d3..1c8d33e 100644 --- a/test/palindrome_permutation_test.rb +++ b/test/palindrome_permutation_test.rb @@ -21,6 +21,11 @@ expect(palindrome_permutation?("racecar")).must_equal true end + it "will work for oooo" do + expect(palindrome_permutation?("oooo")).must_equal true + end + + it "will return false for raceca" do expect(palindrome_permutation?("raceca")).must_equal false end From 4d20f68bbe9f3a3a2ec0e8f893f3f2157f5dd9b6 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Wed, 1 Apr 2020 21:31:56 -0700 Subject: [PATCH 20/22] removes extra space --- test/palindrome_permutation_test.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/test/palindrome_permutation_test.rb b/test/palindrome_permutation_test.rb index 1c8d33e..49007b0 100644 --- a/test/palindrome_permutation_test.rb +++ b/test/palindrome_permutation_test.rb @@ -25,7 +25,6 @@ expect(palindrome_permutation?("oooo")).must_equal true end - it "will return false for raceca" do expect(palindrome_permutation?("raceca")).must_equal false end From 0afdae41bdbd02f915729cc3949e01158f5e41de Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Thu, 2 Apr 2020 22:47:53 -0700 Subject: [PATCH 21/22] simplifies code --- lib/permutations.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 9fc833f..1dad277 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -3,11 +3,8 @@ def permutations?(string1, string2) return false if string1.length != string2.length # add each char from string1 to the hash table - lookup_hash = {} letter_count1 = {} string1.each_char do |char| - lookup_hash[char] ? lookup_hash[char] = false : lookup_hash[char] = true - letter_count1[char] ? letter_count1[char] += 1 : letter_count1[char] = 0 # counts how many of each letter are in string1 end @@ -21,7 +18,7 @@ def permutations?(string1, string2) # lookup each char in string2 in the hash table string2.each_char do |char| - return false if lookup_hash[char].nil? + return false if letter_count1[char].nil? end return true From db56aa4a45df61f121d958d284c2f1ba0ae82c69 Mon Sep 17 00:00:00 2001 From: peachmakkoli Date: Sat, 4 Apr 2020 15:16:53 -0700 Subject: [PATCH 22/22] fixes char count if letter only appears once in the string --- lib/permutations.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/permutations.rb b/lib/permutations.rb index 1dad277..c2ff3b0 100644 --- a/lib/permutations.rb +++ b/lib/permutations.rb @@ -5,13 +5,13 @@ def permutations?(string1, string2) # add each char from string1 to the hash table letter_count1 = {} string1.each_char do |char| - letter_count1[char] ? letter_count1[char] += 1 : letter_count1[char] = 0 # counts how many of each letter are in string1 + letter_count1[char] ? letter_count1[char] += 1 : letter_count1[char] = 1 # counts how many of each letter are in string1 end # counts how many of each letter are in string2 letter_count2 = {} string2.each_char do |char| - letter_count2[char] ? letter_count2[char] += 1 : letter_count2[char] = 0 + letter_count2[char] ? letter_count2[char] += 1 : letter_count2[char] = 1 end return false if letter_count1 != letter_count2 # returns false if the number of a specific letter are different but the string lengths are the same (see comment in test file)