Skip to content

Space - Stephanie #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def intersection(list1, list2)
raise NotImplementedError, "Intersection not implemented"
intersect = list1 & list2

Choose a reason for hiding this comment

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

That works as it uses a feature of Ruby. Do think about how you'd solve it with a hash.

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)

Choose a reason for hiding this comment

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

This works, but see my note below.

raise NotImplementedError, "palindrome_permutation? not implemented"
end

letter_hash = {}
string.chars.map do |char|
letter_hash["#{char}"] = string.count(char)

Choose a reason for hiding this comment

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

string.count(char) is an O(n) operation, so that makes this a total of O(n^2) operation.

It could be better doing this:

Suggested change
letter_hash["#{char}"] = string.count(char)
letter_hash["#{char}"] = letter_hash["#{char}"].nil? ? 1 : letter_hash["#{char}"] + 1

end

odd_count = 0
letter_hash.each do |key, value|
value.odd? ? (odd_count += 1) : next
end

odd_count > 1 ? (return false) : (return true)

end

12 changes: 11 additions & 1 deletion lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@

def permutations?(string1, string2)

Choose a reason for hiding this comment

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

This fails for heelo and hello, and it's more than a little inefficient.

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

string1.chars.each do |char|
if string2.include?(char)

Choose a reason for hiding this comment

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

.include? is an O(n) operation, making this an O(n^2) method. Can you think of a better way to do this with a hash?

string1.slice!(char)
string2.slice!(char)
end
end

return true if string1.empty? && string2.empty?
return false
end
2 changes: 1 addition & 1 deletion test/palindrome_permutation_test.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 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,7 +13,7 @@
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

Expand Down