Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
fdddac4
adds pseudocode
Mar 30, 2020
ad47667
defines intersection method, all tests passing
Mar 30, 2020
3fc73a8
fixes erroneous act statement in test 'returns true for 'pizza', 'pizza'
Mar 30, 2020
a76ff64
adds pseudocode
Mar 30, 2020
c771fab
amends pseudocode, checks if strings are equal or not of same length
Mar 30, 2020
779eac3
adds each char from string1 to the hash table
Mar 30, 2020
02451ba
looks up each char in string2 in the hash table, all tests passing
Mar 30, 2020
ef84fad
removes x from describe
Mar 30, 2020
509e6ae
adds pseudocode and comments on logic
Mar 30, 2020
ee12206
fixes name of test for 'hello', adds test for an even-length palindrome
Mar 30, 2020
cb8e169
adds each char in string to the hash table, works for even-length pal…
Mar 30, 2020
bb9764b
leverages collisions to check if the hash table is the expected lengt…
Mar 30, 2020
0fce2d8
modifies test for 'returns false if the number of a specific letter a…
Mar 30, 2020
a1c4796
fixes mislabeled it block and adds follow-up test for when the number…
Mar 30, 2020
f9ea6ec
fixes typo
Mar 30, 2020
1e93541
attempts to pass test 'returns false if the number of a specific lett…
Mar 30, 2020
95e8464
counts how many of each letter are in each string, compares hash tabl…
Mar 30, 2020
08d6564
refactors code, all tests passing
Mar 30, 2020
725f959
adds test and method for handling palindromes of any length with the …
Apr 2, 2020
4d20f68
removes extra space
Apr 2, 2020
0afdae4
simplifies code
Apr 3, 2020
db56aa4
fixes char count if letter only appears once in the string
Apr 4, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
def intersection(list1, list2)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Intersection not implemented"
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
lookup_hash = {}
smaller.each do |num|
lookup_hash[num] ? lookup_hash[num] = false : lookup_hash[num] = true
end

# 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]
end

return results
end
17 changes: 15 additions & 2 deletions lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@

def palindrome_permutation?(string)
raise NotImplementedError, "palindrome_permutation? not implemented"
return true if string.empty?

# adds each char in string to the hash table
lookup_hash = {}
string.each_char do |char|
lookup_hash[char] ? lookup_hash[char] = false : lookup_hash[char] = true
end

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
end
end
25 changes: 23 additions & 2 deletions lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@

def permutations?(string1, string2)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I think this could be simplified a bit, but otherwise well done.

raise NotImplementedError, "permutations? not implemented"
return true if string1 == string2
return false if string1.length != string2.length

# 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] = 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] = 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)

# lookup each char in string2 in the hash table
string2.each_char do |char|
return false if letter_count1[char].nil?
end

return true
end
12 changes: 10 additions & 2 deletions test/palindrome_permutation_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
require_relative "test_helper"

xdescribe "palindrome_permutation?" do
it "will work for hello" do
describe "palindrome_permutation?" do
it "will return false for hello" do
expect(palindrome_permutation?("hello")).must_equal false
end

it "will work for 'carrace'" do
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
Expand All @@ -17,6 +21,10 @@
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
Expand Down
9 changes: 7 additions & 2 deletions test/permutations_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,11 +13,16 @@
expect(permutations?("pasta", "atsap")).must_equal true
end

it "returns true for 'pizza', 'pizza'" do
it "returns false for 'pizza', 'pasta'" do
expect(permutations?("pizza", "pasta")).must_equal false
end

# 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", "piza")).must_equal false
end

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