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

Time - HannahT #22

Open
wants to merge 1 commit 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
4 changes: 2 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'rake/testtask'
require "rake/testtask"

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = true
t.test_files = FileList['test/*_test.rb']
t.test_files = FileList["test/*_test.rb"]
end

task default: :test
33 changes: 31 additions & 2 deletions lib/array_intersection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
def intersection(list1, list2)

Choose a reason for hiding this comment

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

This works, but it's O(n * m) in time complexity. Using a hash you can get this to O(n + m) time complexity which is much better.

I encourage you to think about how to do this.

raise NotImplementedError, "Intersection not implemented"
end
matching_dudes = []

list1.each do |item1| # outer loop - 1st time - item1 = 2, 2nd time - 4
list2.each do |item2| # inner loop - 1st time - item2 - 2, 2ndtime - item2 =3, 3rd time - item2 = 5, 4thtime, item2 - 7
# 2nd time we run outer loop, comparing 4 to all these guys over again - # inner loop - 1st time - item2 - 2, 2ndtime - item2 =3, 3rd time - item2 = 5, 4thtime, item2 - 7
if item1 == item2 # these are both integers - they are elements inside the array
matching_dudes << item1 # this can be item1 or item2, doesn't matter since they're the same!
end
end
end
return matching_dudes
end

list1 = [2, 4, 6, 8]
list2 = [2, 3, 5, 7]
puts intersection(list2, list1)

# this is for 1 number, 1 array
# initialize empty array
# iterate over array
# if number == item in array
# put item in new array
# return new array

# this is for 2 arrays
# initialize empty array (to put the dudes that match into)
# iterate over first array
# inside that loop, iterate over 2nd array
# if item in 1st array == item in 2nd array
# put item in new array (the one w/ matching dudes)
# return new array!
25 changes: 22 additions & 3 deletions lib/palindrome_permutation.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@

require_relative "permutations.rb"
# Added a new test:)
def palindrome_permutation?(string)

Choose a reason for hiding this comment

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

👍 nice and good helper method.

raise NotImplementedError, "palindrome_permutation? not implemented"
end
hash = making_string_into_hash(string)
number_of_time_count_is_odd = 0
hash.each do |letter, count|
number_of_time_count_is_odd += 1 if count.odd?
# number_of_time_count_is_odd += 1 if count % 2 == 1 (another method to use instead)
end
return number_of_time_count_is_odd <= 1
end

# create a var of hash and set it == the helper method from permutations.rb making_string_into_hash(string)
# (since you want to change the string into hash with key: value, ie, letter: count)
# create (number_of_time_count_is_odd) var and set it == to 0
# use .each method on the hash to get the number of count number
# use if statment to see if the number count is odd or even number
# looking at the test, a word to be a palindrom needs:
# either be an even number || odd with just one nubmer example "racecar"
# if the stored count amount == even number or has one odd number
# return true to palindrome
# return true to empty string
# return false for anything else
51 changes: 48 additions & 3 deletions lib/permutations.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@

def permutations?(string1, string2)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "permutations? not implemented"
end
if string1.length != string2.length
return false
end
hash1 = making_string_into_hash(string1)
hash2 = making_string_into_hash(string2)
if hash1 == hash2
return true
end
return false
end

# created a helper method
def making_string_into_hash(string)
empty_hash = {}
string.each_char do |char|
# .each_char work for string...(to iterater over a string, .each will work on hash and array)
if empty_hash[char] # {'l' => 1}
empty_hash[char] += 1 # {'l' => 2}
else
empty_hash[char] = 1
end
end
return empty_hash
end

puts(making_string_into_hash("hello"))
puts(making_string_into_hash("elloh"))

# permutations?("hello", "ehllo") => true ["h", "e", ]
# hello - ehllo - helo heelo oohel
# h - 1, e -1, l -2, o -1
# heelo h -1 e -2 l -1, o -1
# permutations?("pasta", "atsap") => true
# permutations?("Pizza", "Pasta") => false
# permutations?("", "") => true

# check if the two strings have the same length
# if the length is not ==, return false
# if length is equal then
# parse the two sting into key value pair
# to do the parsing, separate the sring into char
# count how many times the char exist and assign number
# initialize an empty hash, {} no keys.
# to count the char, create a loop .each_char h, e, l, l, o
# on our first loop through, h is our character. 'h' => 1
# if 'h' is not in the hash, we need to add it
# if 'h' is in the hash, then we need to +1 the value
# then compare the two hashes that are created
109 changes: 109 additions & 0 deletions notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
QUESTION 1:

def intersection(list1, list2)
matching_dudes = []
# this is for 1 number, 1 array
# initialize empty array
# iterate over array
# if number == item in array
# put item in new array
# return new array

# this is for 2 arrays
# initialize empty array (to put the dudes that match into)
# iterate over first array
# inside that loop, iterate over 2nd array
# if item in 1st array == item in 2nd array
# put item in new array (the one w/ matching dudes)
# return new array!

list1.each do |item1| # outer loop - 1st time - item1 = 2, 2nd time - 4
list2.each do |item2| # inner loop - 1st time - item2 - 2, 2ndtime - item2 =3, 3rd time - item2 = 5, 4thtime, item2 - 7
# 2nd time we run outer loop, comparing 4 to all these guys over again - # inner loop - 1st time - item2 - 2, 2ndtime - item2 =3, 3rd time - item2 = 5, 4thtime, item2 - 7
if item1 == item2 # these are both integers - they are elements inside the array
matching_dudes << item1 # this can be item1 or item2, doesn't matter since they're the same!
end
end
end
return matching_dudes
end

list1 = [2, 4, 6, 8]
list2 = [2, 3, 5, 7]
puts intersection(list2, list1)
__________________________________________________________
QUESTION 2:
def permutations?(string1, string2)
if string1.length != string2.length
return false
end
hash1 = making_string_into_hash(string1)
hash2 = making_string_into_hash(string2)
if hash1 == hash2
return true
end
return false
end

# permutations?("hello", "ehllo") => true ["h", "e", ]
# hello - ehllo - helo heelo oohel
# h - 1, e -1, l -2, o -1
# heelo h -1 e -2 l -1, o -1
# permutations?("pasta", "atsap") => true
# permutations?("Pizza", "Pasta") => false
# permutations?("", "") => true

# check if the two strings have the same length
# if the length is not ==, return false
# if length is equal then
# parse the two sting into key value pair
# to do the parsing, separate the sring into char
# count how many times the char exist and assign number
# initialize an empty hash, {} no keys.
# to count the char, create a loop .each_char h, e, l, l, o
# on our first loop through, h is our character. 'h' => 1
# if 'h' is not in the hash, we need to add it
# if 'h' is in the hash, then we need to +1 the value
# then compare the two hashes that are created

def making_string_into_hash(string)
empty_hash = {}
string.each_char do |char|
# .each_char work for string...(to iterater over a string, .each will work on hash and array)
if empty_hash[char] # {'l' => 1}
empty_hash[char] += 1 # {'l' => 2}
else
empty_hash[char] = 1
end
end
return empty_hash
end

puts(making_string_into_hash("hello"))
puts(making_string_into_hash("elloh"))
__________________________________________________________
QUESTION 3:

require_relative "permutations.rb"

def palindrome_permutation?(string)
hash = making_string_into_hash(string)
number_of_time_count_is_odd = 0
hash.each do |letter, count|
number_of_time_count_is_odd += 1 if count.odd?
# number_of_time_count_is_odd += 1 if count % 2 == 1 (another method to use instead)
end
return number_of_time_count_is_odd <= 1
end

# create a var of hash and set it == the helper method from permutations.rb making_string_into_hash(string)
# (since you want to change the string into hash with key: value, ie, letter: count)
# create (number_of_time_count_is_odd) var and set it == to 0
# use .each method on the hash to get the number of count number
# use if statment to see if the number count is odd or even number
# looking at the test, a word to be a palindrom needs:
# either be an even number || odd with just one nubmer example "racecar"
# if the stored count amount == even number or has one odd number
# return true to palindrome
# return true to empty string
# return false for anything else
2 changes: 1 addition & 1 deletion test/array_intersection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
it "returns [50, 25, 43] for 50, 43, 25, 72], and [25, 36, 43, 50, 80]" do
expect(intersection([50, 43, 25, 72], [25, 36, 43, 50, 80]).sort).must_equal [25, 43, 50]
end
end
end
10 changes: 7 additions & 3 deletions 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 @@ -19,5 +19,9 @@

it "will return false for raceca" do
expect(palindrome_permutation?("raceca")).must_equal false
end
end
end

it "will return true for 'hannah'" do
expect(palindrome_permutation?("hannah")).must_equal true
end
end
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 @@ -20,4 +20,4 @@
it "returns false if the number of a specific letter are different" do
expect(permutations?("pizza", "piza")).must_equal false
end
end
end
4 changes: 2 additions & 2 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'minitest/autorun'
require 'minitest/reporters'
require "minitest/autorun"
require "minitest/reporters"
require "minitest/skip_dsl"

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
Expand Down