Skip to content
Merged
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
22 changes: 1 addition & 21 deletions bin/add-practice-exercise
Original file line number Diff line number Diff line change
Expand Up @@ -91,27 +91,7 @@ curl -s -o "canonical-data/${slug}.json" "$github"


camel_slug=$(perl -pe 's/(?:^|-)([a-z])/\u$1/g' <<< "$slug")
cat << SPEC_GENERATOR > exercises/practice/${slug}/.meta/spec_generator.moon
{
module_name: '${camel_slug}',
-- or, module_imports: {'func1', 'func2', ...},

-- optional:
test_helpers: [[
A block of code here, indented 2 spaces
]]

generate_test: (case, level) ->
local lines
-- you may want to "switch case.property" here
lines = {
"result = ${camel_slug}.#{case.property} #{case.input}",
"expected = #{quote case.expected}",
"assert.are.equal expected, result"
}
table.concat [indent line, level for line in *lines], '\n'
}
SPEC_GENERATOR
cp -v exercises/shared/templates/spec_generator.moon exercises/practice/${slug}/.meta/spec_generator.moon


cat << NEXT_STEPS
Expand Down
13 changes: 13 additions & 0 deletions bin/generate-spec
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ else

spec ..= "\n\n" .. process(canonical_data)

if spec_generator.bonus
spec ..= [[


-- The next tests are optional.
-- Set the environment variable ENABLE_OPTIONAL_TESTS to run them:
-- For example, in bash run: ENABLE_OPTIONAL_TESTS=true busted

if os.getenv('ENABLE_OPTIONAL_TESTS') == 'true'
describe 'Bonus tests', ->
]]
spec ..= spec_generator.bonus

spec_path = exercise_directory .. '/' .. snake_name .. '_spec.moon'
write_file spec_path, spec

Expand Down
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "custom-set",
"name": "Custom Set",
"uuid": "142418f9-5d3b-4dd2-8324-3d956faa32e6",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "flower-field",
"name": "Flower Field",
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/custom-set/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
7 changes: 7 additions & 0 deletions exercises/practice/custom-set/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Instructions

Create a custom set type.

Sometimes it is necessary to define a custom data structure of some type, like a set.
In this exercise you will define your own set.
How it works internally doesn't matter, as long as it behaves like a set of unique elements.
17 changes: 17 additions & 0 deletions exercises/practice/custom-set/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"glennj"
],
"files": {
"solution": [
"custom_set.moon"
],
"test": [
"custom_set_spec.moon"
],
"example": [
".meta/example.moon"
]
},
"blurb": "Create a custom set type."
}
62 changes: 62 additions & 0 deletions exercises/practice/custom-set/.meta/example.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class CustomSet
new: (items = {}) =>
@data = {}
@add item for item in *items

add: (item) =>
@data[item] = true

is_empty: =>
not next @data

contains: (item) =>
@data[item]

is_subset: (other) =>
for item, _ in pairs @data
return false if not other\contains item
true

is_disjoint: (other) =>
for item, _ in pairs @data
return false if other\contains item
true

-- metamethods can just go into the class

__eq: (other) =>
@is_subset(other) and other\is_subset(@)

__sub: (other) =>
diff = @@!
for item, _ in pairs @data
if not other\contains item
diff\add item
diff

__add: (other) =>
union = @@!
union\add item for item, _ in pairs @data
union\add item for item, _ in pairs other.data
union

__band: (other) =>
intersect = @@!
for item, _ in pairs @data
if other\contains item
intersect\add item
intersect

-- methods implemented using metamethod

is_equal: (other) =>
@ == other

intersection: (other) =>
@ & other

difference: (other) =>
@ - other

union: (other) =>
@ + other
54 changes: 54 additions & 0 deletions exercises/practice/custom-set/.meta/spec_generator.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
int_list = (list) -> "{#{table.concat list, ', '}}"

{
module_name: 'CustomSet',

generate_test: (case, level) ->
local lines
switch case.property
when 'empty'
lines = {
"set = CustomSet #{int_list case.input.set}",
"assert.is.#{case.expected}, set\\is_empty!"
}
when 'contains'
lines = {
"set = CustomSet #{int_list case.input.set}",
"assert.is.#{case.expected}, set\\#{case.property} #{case.input.element}"
}
when 'subset', 'disjoint', 'equal'
lines = {
"set1 = CustomSet #{int_list case.input.set1}",
"set2 = CustomSet #{int_list case.input.set2}",
"assert.is.#{case.expected}, set1\\is_#{case.property} set2"
}
when 'add'
lines = {
"set = CustomSet #{int_list case.input.set}",
"set\\#{case.property} #{case.input.element}"
"expected = CustomSet #{int_list case.expected}"
"assert.is.true set\\is_equal expected"
}
when 'intersection', 'difference', 'union'
lines = {
"set1 = CustomSet #{int_list case.input.set1}",
"set2 = CustomSet #{int_list case.input.set2}",
"result = set1\\#{case.property} set2"
"expected = CustomSet #{int_list case.expected}"
"assert.is.true result\\is_equal expected"
}
table.concat [indent line, level for line in *lines], '\n'

-- bonus tests are a multiline string indented with 6 spaces
bonus: [[
pending 'use the equal operator, are equal', ->
set1 = CustomSet {1, 2, 3, 4}
set2 = CustomSet {4, 3, 1, 2}
assert.is.true set1 == set2

pending 'use the equal operator, not equal', ->
set1 = CustomSet {1, 2, 3, 4}
set2 = CustomSet {4, 3, 1, 20}
assert.is.true set1 != set2
]]
}
130 changes: 130 additions & 0 deletions exercises/practice/custom-set/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[20c5f855-f83a-44a7-abdd-fe75c6cf022b]
description = "Returns true if the set contains no elements -> sets with no elements are empty"

[d506485d-5706-40db-b7d8-5ceb5acf88d2]
description = "Returns true if the set contains no elements -> sets with elements are not empty"

[759b9740-3417-44c3-8ca3-262b3c281043]
description = "Sets can report if they contain an element -> nothing is contained in an empty set"

[f83cd2d1-2a85-41bc-b6be-80adbff4be49]
description = "Sets can report if they contain an element -> when the element is in the set"

[93423fc0-44d0-4bc0-a2ac-376de8d7af34]
description = "Sets can report if they contain an element -> when the element is not in the set"

[c392923a-637b-4495-b28e-34742cd6157a]
description = "A set is a subset if all of its elements are contained in the other set -> empty set is a subset of another empty set"

[5635b113-be8c-4c6f-b9a9-23c485193917]
description = "A set is a subset if all of its elements are contained in the other set -> empty set is a subset of non-empty set"

[832eda58-6d6e-44e2-92c2-be8cf0173cee]
description = "A set is a subset if all of its elements are contained in the other set -> non-empty set is not a subset of empty set"

[c830c578-8f97-4036-b082-89feda876131]
description = "A set is a subset if all of its elements are contained in the other set -> set is a subset of set with exact same elements"

[476a4a1c-0fd1-430f-aa65-5b70cbc810c5]
description = "A set is a subset if all of its elements are contained in the other set -> set is a subset of larger set with same elements"

[d2498999-3e46-48e4-9660-1e20c3329d3d]
description = "A set is a subset if all of its elements are contained in the other set -> set is not a subset of set that does not contain its elements"

[7d38155e-f472-4a7e-9ad8-5c1f8f95e4cc]
description = "Sets are disjoint if they share no elements -> the empty set is disjoint with itself"

[7a2b3938-64b6-4b32-901a-fe16891998a6]
description = "Sets are disjoint if they share no elements -> empty set is disjoint with non-empty set"

[589574a0-8b48-48ea-88b0-b652c5fe476f]
description = "Sets are disjoint if they share no elements -> non-empty set is disjoint with empty set"

[febeaf4f-f180-4499-91fa-59165955a523]
description = "Sets are disjoint if they share no elements -> sets are not disjoint if they share an element"

[0de20d2f-c952-468a-88c8-5e056740f020]
description = "Sets are disjoint if they share no elements -> sets are disjoint if they share no elements"

[4bd24adb-45da-4320-9ff6-38c044e9dff8]
description = "Sets with the same elements are equal -> empty sets are equal"

[f65c0a0e-6632-4b2d-b82c-b7c6da2ec224]
description = "Sets with the same elements are equal -> empty set is not equal to non-empty set"

[81e53307-7683-4b1e-a30c-7e49155fe3ca]
description = "Sets with the same elements are equal -> non-empty set is not equal to empty set"

[d57c5d7c-a7f3-48cc-a162-6b488c0fbbd0]
description = "Sets with the same elements are equal -> sets with the same elements are equal"

[dd61bafc-6653-42cc-961a-ab071ee0ee85]
description = "Sets with the same elements are equal -> sets with different elements are not equal"

[06059caf-9bf4-425e-aaff-88966cb3ea14]
description = "Sets with the same elements are equal -> set is not equal to larger set with same elements"

[d4a1142f-09aa-4df9-8b83-4437dcf7ec24]
description = "Sets with the same elements are equal -> set is equal to a set constructed from an array with duplicates"

[8a677c3c-a658-4d39-bb88-5b5b1a9659f4]
description = "Unique elements can be added to a set -> add to empty set"

[0903dd45-904d-4cf2-bddd-0905e1a8d125]
description = "Unique elements can be added to a set -> add to non-empty set"

[b0eb7bb7-5e5d-4733-b582-af771476cb99]
description = "Unique elements can be added to a set -> adding an existing element does not change the set"

[893d5333-33b8-4151-a3d4-8f273358208a]
description = "Intersection returns a set of all shared elements -> intersection of two empty sets is an empty set"

[d739940e-def2-41ab-a7bb-aaf60f7d782c]
description = "Intersection returns a set of all shared elements -> intersection of an empty set and non-empty set is an empty set"

[3607d9d8-c895-4d6f-ac16-a14956e0a4b7]
description = "Intersection returns a set of all shared elements -> intersection of a non-empty set and an empty set is an empty set"

[b5120abf-5b5e-41ab-aede-4de2ad85c34e]
description = "Intersection returns a set of all shared elements -> intersection of two sets with no shared elements is an empty set"

[af21ca1b-fac9-499c-81c0-92a591653d49]
description = "Intersection returns a set of all shared elements -> intersection of two sets with shared elements is a set of the shared elements"

[c5e6e2e4-50e9-4bc2-b89f-c518f015b57e]
description = "Difference (or Complement) of a set is a set of all elements that are only in the first set -> difference of two empty sets is an empty set"

[2024cc92-5c26-44ed-aafd-e6ca27d6fcd2]
description = "Difference (or Complement) of a set is a set of all elements that are only in the first set -> difference of empty set and non-empty set is an empty set"

[e79edee7-08aa-4c19-9382-f6820974b43e]
description = "Difference (or Complement) of a set is a set of all elements that are only in the first set -> difference of a non-empty set and an empty set is the non-empty set"

[c5ac673e-d707-4db5-8d69-7082c3a5437e]
description = "Difference (or Complement) of a set is a set of all elements that are only in the first set -> difference of two non-empty sets is a set of elements that are only in the first set"

[20d0a38f-7bb7-4c4a-ac15-90c7392ecf2b]
description = "Difference (or Complement) of a set is a set of all elements that are only in the first set -> difference removes all duplicates in the first set"

[c45aed16-5494-455a-9033-5d4c93589dc6]
description = "Union returns a set of all elements in either set -> union of empty sets is an empty set"

[9d258545-33c2-4fcb-a340-9f8aa69e7a41]
description = "Union returns a set of all elements in either set -> union of an empty set and non-empty set is the non-empty set"

[3aade50c-80c7-4db8-853d-75bac5818b83]
description = "Union returns a set of all elements in either set -> union of a non-empty set and empty set is the non-empty set"

[a00bb91f-c4b4-4844-8f77-c73e2e9df77c]
description = "Union returns a set of all elements in either set -> union of non-empty sets contains all unique elements"
30 changes: 30 additions & 0 deletions exercises/practice/custom-set/custom_set.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class CustomSet
new: (items) =>
error 'Implement the constructor'

add: (item) =>
error 'Implement the add method'

is_empty: =>
error 'Implement the is_empty method'

contains: (item) =>
error 'Implement the contains method'

is_subset: (other) =>
error 'Implement the is_subset method'

is_disjoint: (other) =>
error 'Implement the is_disjoint method'

is_equal: (other) =>
error 'Implement the is_equal method'

intersection: (other) =>
error 'Implement the intersection method'

difference: (other) =>
error 'Implement the difference method'

union: (other) =>
error 'Implement the union method'
Loading
Loading