forked from AdaGold/Hashmap
-
Notifications
You must be signed in to change notification settings - Fork 47
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
stpatrickschild
wants to merge
1
commit into
Ada-C13:master
Choose a base branch
from
stpatrickschild:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Time - HannahT #22
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,32 @@ | ||
def intersection(list1, list2) | ||
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,49 @@ | ||
|
||
def permutations?(string1, string2) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.