From 34bbf638e6d66911d90672c7dc46af0860bf0129 Mon Sep 17 00:00:00 2001 From: Rudresh Amin Date: Fri, 8 Sep 2017 19:14:37 -0700 Subject: [PATCH] Add solution to Sublist problem. --- config.json | 12 ++ exercises/sublist/.meta/.version | 1 + .../sublist/.meta/generator/sublist_case.rb | 9 ++ exercises/sublist/.meta/solutions/sublist.rb | 24 ++++ exercises/sublist/README.md | 22 ++++ exercises/sublist/sublist_test.rb | 112 ++++++++++++++++++ 6 files changed, 180 insertions(+) create mode 100644 exercises/sublist/.meta/.version create mode 100644 exercises/sublist/.meta/generator/sublist_case.rb create mode 100644 exercises/sublist/.meta/solutions/sublist.rb create mode 100644 exercises/sublist/README.md create mode 100644 exercises/sublist/sublist_test.rb diff --git a/config.json b/config.json index 1ca3cf464c..47fc548b92 100644 --- a/config.json +++ b/config.json @@ -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": [ diff --git a/exercises/sublist/.meta/.version b/exercises/sublist/.meta/.version new file mode 100644 index 0000000000..56a6051ca2 --- /dev/null +++ b/exercises/sublist/.meta/.version @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/exercises/sublist/.meta/generator/sublist_case.rb b/exercises/sublist/.meta/generator/sublist_case.rb new file mode 100644 index 0000000000..fd7c835381 --- /dev/null +++ b/exercises/sublist/.meta/generator/sublist_case.rb @@ -0,0 +1,9 @@ +require 'generator/exercise_case' + +class SublistCase < Generator::ExerciseCase + + def workload + assert_equal { "Sublist.compare(#{listOne}, #{listTwo})" } + end + +end diff --git a/exercises/sublist/.meta/solutions/sublist.rb b/exercises/sublist/.meta/solutions/sublist.rb new file mode 100644 index 0000000000..a0d65d705b --- /dev/null +++ b/exercises/sublist/.meta/solutions/sublist.rb @@ -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 diff --git a/exercises/sublist/README.md b/exercises/sublist/README.md new file mode 100644 index 0000000000..072f7dda36 --- /dev/null +++ b/exercises/sublist/README.md @@ -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. diff --git a/exercises/sublist/sublist_test.rb b/exercises/sublist/sublist_test.rb new file mode 100644 index 0000000000..f9e2949c65 --- /dev/null +++ b/exercises/sublist/sublist_test.rb @@ -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