-
-
Notifications
You must be signed in to change notification settings - Fork 520
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
Add solution to Sublist problem. #720
Closed
Closed
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
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 @@ | ||
1 |
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,9 @@ | ||
require 'generator/exercise_case' | ||
|
||
class SublistCase < Generator::ExerciseCase | ||
|
||
def workload | ||
assert_equal { "Sublist.compare(#{listOne}, #{listTwo})" } | ||
end | ||
|
||
end |
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,24 @@ | ||
module BookKeeping | ||
VERSION = 1 | ||
end | ||
|
||
class Sublist | ||
|
||
EQUAL = 'equal' | ||
UNEQUAL = 'unequal' | ||
SUBLIST = 'sublist' | ||
SUPERLIST = 'superlist' | ||
|
||
def self.compare(list1, list2) | ||
comp = lambda {|l1, l2| (l1.empty?) || (l2.each_cons(l1.size).include? (l1))} | ||
if list1 == list2 | ||
EQUAL | ||
elsif comp.call(list1, list2) | ||
SUBLIST | ||
elsif comp.call(list2, list1) | ||
SUPERLIST | ||
else | ||
UNEQUAL | ||
end | ||
end | ||
end |
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,22 @@ | ||
# Sublist | ||
|
||
Given two lists determine if the first list is contained within the second list, if the second list is contained within the first list, if both lists are contained within each other or if none of these are true. | ||
|
||
Specifically, a list A is a sublist of list B if by dropping 0 or more elements from the front of B and 0 or more elements from the back of B you get a list that's completely equal to A. | ||
|
||
Examples: | ||
|
||
A = [1, 2, 3], B = [1, 2, 3, 4, 5], A is a sublist of B | ||
A = [3, 4, 5], B = [1, 2, 3, 4, 5], A is a sublist of B | ||
A = [3, 4], B = [1, 2, 3, 4, 5], A is a sublist of B | ||
A = [1, 2, 3], B = [1, 2, 3], A is equal to B | ||
A = [1, 2, 3, 4, 5], B = [2, 3, 4], A is a superlist of B | ||
A = [1, 2, 4], B = [1, 2, 3, 4, 5], A is not a superlist of, sublist of or equal to B | ||
|
||
* * * * | ||
|
||
For installation and learning resources, refer to the | ||
[exercism help page](http://exercism.io/languages/ruby). | ||
|
||
## Submitting Incomplete Solutions | ||
It's possible to submit an incomplete solution so you can see how others have completed the exercise. |
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,112 @@ | ||
require 'minitest/autorun' | ||
require_relative 'sublist' | ||
|
||
# Common test data version: 1.0.0 71e24b5 | ||
class SublistTest < Minitest::Test | ||
def test_empty_lists | ||
# skip | ||
assert_equal "equal", Sublist.compare([], []) | ||
end | ||
|
||
def test_empty_list_within_non_empty_list | ||
skip | ||
assert_equal "sublist", Sublist.compare([], [1, 2, 3]) | ||
end | ||
|
||
def test_non_empty_list_contains_empty_list | ||
skip | ||
assert_equal "superlist", Sublist.compare([1, 2, 3], []) | ||
end | ||
|
||
def test_list_equals_itself | ||
skip | ||
assert_equal "equal", Sublist.compare([1, 2, 3], [1, 2, 3]) | ||
end | ||
|
||
def test_different_lists | ||
skip | ||
assert_equal "unequal", Sublist.compare([1, 2, 3], [2, 3, 4]) | ||
end | ||
|
||
def test_false_start | ||
skip | ||
assert_equal "sublist", Sublist.compare([1, 2, 5], [0, 1, 2, 3, 1, 2, 5, 6]) | ||
end | ||
|
||
def test_consecutive | ||
skip | ||
assert_equal "sublist", Sublist.compare([1, 1, 2], [0, 1, 1, 1, 2, 1, 2]) | ||
end | ||
|
||
def test_sublist_at_start | ||
skip | ||
assert_equal "sublist", Sublist.compare([0, 1, 2], [0, 1, 2, 3, 4, 5]) | ||
end | ||
|
||
def test_sublist_in_middle | ||
skip | ||
assert_equal "sublist", Sublist.compare([2, 3, 4], [0, 1, 2, 3, 4, 5]) | ||
end | ||
|
||
def test_sublist_at_end | ||
skip | ||
assert_equal "sublist", Sublist.compare([3, 4, 5], [0, 1, 2, 3, 4, 5]) | ||
end | ||
|
||
def test_at_start_of_superlist | ||
skip | ||
assert_equal "superlist", Sublist.compare([0, 1, 2, 3, 4, 5], [0, 1, 2]) | ||
end | ||
|
||
def test_in_middle_of_superlist | ||
skip | ||
assert_equal "superlist", Sublist.compare([0, 1, 2, 3, 4, 5], [2, 3]) | ||
end | ||
|
||
def test_at_end_of_superlist | ||
skip | ||
assert_equal "superlist", Sublist.compare([0, 1, 2, 3, 4, 5], [3, 4, 5]) | ||
end | ||
|
||
def test_first_list_missing_element_from_second_list | ||
skip | ||
assert_equal "unequal", Sublist.compare([1, 3], [1, 2, 3]) | ||
end | ||
|
||
def test_second_list_missing_element_from_first_list | ||
skip | ||
assert_equal "unequal", Sublist.compare([1, 2, 3], [1, 3]) | ||
end | ||
|
||
def test_order_matters_to_a_list | ||
skip | ||
assert_equal "unequal", Sublist.compare([1, 2, 3], [3, 2, 1]) | ||
end | ||
|
||
def test_same_digits_but_different_numbers | ||
skip | ||
assert_equal "unequal", Sublist.compare([1, 0, 1], [10, 1]) | ||
end | ||
|
||
# Problems in exercism evolve over time, as we find better ways to ask | ||
# questions. | ||
# The version number refers to the version of the problem you solved, | ||
# not your solution. | ||
# | ||
# Define a constant named VERSION inside of the top level BookKeeping | ||
# module, which may be placed near the end of your file. | ||
# | ||
# In your file, it will look like this: | ||
# | ||
# module BookKeeping | ||
# VERSION = 1 # Where the version number matches the one in the test. | ||
# end | ||
# | ||
# If you are curious, read more about constants on RubyDoc: | ||
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html | ||
|
||
def test_bookkeeping | ||
skip | ||
assert_equal 1, BookKeeping::VERSION | ||
end | ||
end |
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.
Should also include instructions about how to run the tests.
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.
I solved this problem once and for all by autogenerating READMEs: #721