Skip to content
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

space - Sara Nilsen #23

Open
wants to merge 2 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
23 changes: 22 additions & 1 deletion lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
def intersection(list1, list2)
raise NotImplementedError, "Intersection not implemented"
longest_list = list1 #default longest list, it changes if list2 is longest than list1
other_list = list2
#-------------------------------------------#
if list1.length < list2.length
longest_list = list2
other_list = list1
end
#-------------------------------------------#
hash_table = {} #for comparing values
longest_list.each do |n|
hash_table[n] = 1
end
#-------------------------------------------#
intersection = [] #the intersection numbers
other_list.each do |n|
if hash_table.key?(n)
intersection << n
hash_table[n] += 1
end
end
#-------------------------------------------#
return intersection
end
35 changes: 33 additions & 2 deletions lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@

def palindrome_permutation?(string)
raise NotImplementedError, "palindrome_permutation? not implemented"
end
return true if string == ""

hash_map = Hash.new(0)
(string.chars).each do |s|
hash_map[s] += 1
end

odd = 0
hash_map.each_value do |v|
if v % 2 != 0
odd += 1
end
end
return odd > 1 ? false : true
end








# if string.length % 2 == 0 #when string size is even every value should be == 2 to be permutation
# hash_map.each do |l, v|
# if v != 2
# return false
# end
# else
# hash_map.each do |l, v|

# end
# end
20 changes: 18 additions & 2 deletions lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@

def permutations?(string1, string2)
raise NotImplementedError, "permutations? not implemented"
end
return false if string1.length != string2.length
first_hash = Hash.new(0)
(string1.chars).each do |s|
first_hash[s] += 1
end

puts first_hash
#-------------------------------------------------#
second_hash = Hash.new(0)
(string2.chars).each do |s|
second_hash[s] += 1
end

first_hash == second_hash ? true : false
end

#how can I check if two strings have the same number of letters

3 changes: 2 additions & 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 All @@ -20,4 +20,5 @@
it "will return false for raceca" do
expect(palindrome_permutation?("raceca")).must_equal false
end

end
5 changes: 4 additions & 1 deletion 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 @@ -20,4 +20,7 @@
it "returns false if the number of a specific letter are different" do
expect(permutations?("pizza", "piza")).must_equal false
end
it "returns false for hellllllllll" do
expect(permutations?("hellllllll", "hel")).must_equal false
end
end