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

Add solution to Sublist problem. #720

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 12 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,18 @@
"uuid": "89bd3d71-000f-4cd9-9a84-ad1b22ddbd33",
"slug": "point-mutations",
"deprecated": true
},
{
"uuid": "a20c86c0-e87b-4910-83b7-3715c86c2b0b",
"slug": "sublist",
"core": false,
"unlocked_by": null,
"difficulty": 2,
"topics": [
"Control-flow (conditionals)",
"Control-flow (loops)",
"Logic"
]
}
],
"foregone": [
Expand Down
1 change: 1 addition & 0 deletions exercises/sublist/.meta/.version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
9 changes: 9 additions & 0 deletions exercises/sublist/.meta/generator/sublist_case.rb
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
24 changes: 24 additions & 0 deletions exercises/sublist/.meta/solutions/sublist.rb
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
22 changes: 22 additions & 0 deletions exercises/sublist/README.md
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).

Copy link
Contributor

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.

Copy link
Author

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

## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
112 changes: 112 additions & 0 deletions exercises/sublist/sublist_test.rb
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