-
Notifications
You must be signed in to change notification settings - Fork 47
Time - Emily #43
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
base: master
Are you sure you want to change the base?
Time - Emily #43
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,20 @@ | ||
| def intersection(list1, list2) | ||
| raise NotImplementedError, "Intersection not implemented" | ||
|
|
||
| intersections = {} | ||
|
|
||
| list1.each do |value| | ||
| if !intersections[value] | ||
| intersections[value] = 1 | ||
| elsif intersections[value] | ||
| intersections[value] += 1 | ||
| end | ||
| end | ||
|
|
||
| list2.each do |value| | ||
| if intersections[value] | ||
| intersections[value] += 1 | ||
| end | ||
| end | ||
|
|
||
| return (intersections.select { |key, value| value == 2}).keys | ||
| end | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,21 @@ | ||||||||||||||
|
|
||||||||||||||
| def palindrome_permutation?(string) | ||||||||||||||
| raise NotImplementedError, "palindrome_permutation? not implemented" | ||||||||||||||
| string_array = string.split("") | ||||||||||||||
| letter_instances = {} | ||||||||||||||
|
|
||||||||||||||
| string_array.each do |letter| | ||||||||||||||
| if !letter_instances[letter] | ||||||||||||||
| letter_instances[letter] = 1 | ||||||||||||||
| else | ||||||||||||||
| letter_instances[letter] += 1 | ||||||||||||||
| end | ||||||||||||||
| end | ||||||||||||||
|
|
||||||||||||||
| duplicate_letters = letter_instances.select { |key, value| (value % 2) == 0} | ||||||||||||||
|
|
||||||||||||||
| if duplicate_letters.values.sum == string_array.length || duplicate_letters.values.sum == string_array.length - 1 | ||||||||||||||
| return true | ||||||||||||||
| else | ||||||||||||||
| return false | ||||||||||||||
| end | ||||||||||||||
|
Comment on lines
+16
to
+20
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. You can simplify this and eliminate the if statement, since you're looking for a true/false answer.
Suggested change
|
||||||||||||||
| end | ||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,26 @@ | ||
|
|
||
| def permutations?(string1, string2) | ||
| raise NotImplementedError, "permutations? not implemented" | ||
| intersections = {} | ||
| string1_array = string1.split("") | ||
| string2_array = string2.split("") | ||
|
|
||
| string1_array.each do |value| | ||
| if !intersections[value] | ||
| intersections[value] = 1 | ||
| elsif intersections[value] | ||
| intersections[value] += 1 | ||
| end | ||
| end | ||
|
|
||
| string2_array.each do |value| | ||
| if intersections[value] | ||
| intersections[value] += 1 | ||
| end | ||
|
Comment on lines
+16
to
+18
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. What if the value isn't in I would also suggest subtracting instead of adding, and then you can check to see if all the values are 0. |
||
| end | ||
|
|
||
| if ((intersections.select { |key, value| (value % 2) == 0}).values.sum / 2) == string1_array.length | ||
| return true | ||
| else | ||
| return false | ||
| end | ||
| end | ||
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.
👍