Skip to content

Commit 4abaad4

Browse files
committed
Add custom-set
1 parent 0823291 commit 4abaad4

11 files changed

Lines changed: 586 additions & 0 deletions

File tree

bin/add-practice-exercise

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ cat << SPEC_GENERATOR > exercises/practice/${slug}/.meta/spec_generator.moon
110110
"assert.are.equal expected, result"
111111
}
112112
table.concat [indent line, level for line in *lines], '\n'
113+
114+
-- optional:
115+
bonus: [[
116+
pending 'bonus test description', ->
117+
"bonus tests are not generated, there are just a block of text"
118+
"bonus tests are indented 6 spaces"
119+
]]
113120
}
114121
SPEC_GENERATOR
115122

bin/generate-spec

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,19 @@ else
145145

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

148+
if spec_generator.bonus
149+
spec ..= [[
150+
151+
152+
-- The next tests are optional.
153+
-- Set the environment variable ENABLE_OPTIONAL_TESTS to run them:
154+
-- For example, in bash run: ENABLE_OPTIONAL_TESTS=true busted
155+
156+
if os.getenv('ENABLE_OPTIONAL_TESTS') == 'true'
157+
describe 'Bonus tests', ->
158+
]]
159+
spec ..= spec_generator.bonus
160+
148161
spec_path = exercise_directory .. '/' .. snake_name .. '_spec.moon'
149162
write_file spec_path, spec
150163

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,14 @@
634634
"prerequisites": [],
635635
"difficulty": 5
636636
},
637+
{
638+
"slug": "custom-set",
639+
"name": "Custom Set",
640+
"uuid": "142418f9-5d3b-4dd2-8324-3d956faa32e6",
641+
"practices": [],
642+
"prerequisites": [],
643+
"difficulty": 5
644+
},
637645
{
638646
"slug": "flower-field",
639647
"name": "Flower Field",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Instructions
2+
3+
Create a custom set type.
4+
5+
Sometimes it is necessary to define a custom data structure of some type, like a set.
6+
In this exercise you will define your own set.
7+
How it works internally doesn't matter, as long as it behaves like a set of unique elements.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"glennj"
4+
],
5+
"files": {
6+
"solution": [
7+
"custom_set.moon"
8+
],
9+
"test": [
10+
"custom_set_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
14+
]
15+
},
16+
"blurb": "Create a custom set type."
17+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
class CustomSet
2+
new: (items = {}) =>
3+
@data = {}
4+
@add item for item in *items
5+
6+
add: (item) =>
7+
@data[item] = true
8+
9+
is_empty: =>
10+
not next @data
11+
12+
contains: (item) =>
13+
@data[item]
14+
15+
is_subset: (other) =>
16+
for item, _ in pairs @data
17+
return false if not other\contains item
18+
true
19+
20+
is_disjoint: (other) =>
21+
for item, _ in pairs @data
22+
return false if other\contains item
23+
true
24+
25+
-- metamethods can just go into the class
26+
27+
__eq: (other) =>
28+
@is_subset(other) and other\is_subset(@)
29+
30+
__sub: (other) =>
31+
diff = @@!
32+
for item, _ in pairs @data
33+
if not other\contains item
34+
diff\add item
35+
diff
36+
37+
__add: (other) =>
38+
union = @@!
39+
union\add item for item, _ in pairs @data
40+
union\add item for item, _ in pairs other.data
41+
union
42+
43+
__band: (other) =>
44+
intersect = @@!
45+
for item, _ in pairs @data
46+
if other\contains item
47+
intersect\add item
48+
intersect
49+
50+
-- methods implemented using metamethod
51+
52+
is_equal: (other) =>
53+
@ == other
54+
55+
intersection: (other) =>
56+
@ & other
57+
58+
difference: (other) =>
59+
@ - other
60+
61+
union: (other) =>
62+
@ + other
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
int_list = (list) -> "{#{table.concat list, ', '}}"
2+
3+
{
4+
module_name: 'CustomSet',
5+
6+
generate_test: (case, level) ->
7+
local lines
8+
switch case.property
9+
when 'empty'
10+
lines = {
11+
"set = CustomSet #{int_list case.input.set}",
12+
"assert.is.#{case.expected}, set\\is_empty!"
13+
}
14+
when 'contains'
15+
lines = {
16+
"set = CustomSet #{int_list case.input.set}",
17+
"assert.is.#{case.expected}, set\\#{case.property} #{case.input.element}"
18+
}
19+
when 'subset', 'disjoint', 'equal'
20+
lines = {
21+
"set1 = CustomSet #{int_list case.input.set1}",
22+
"set2 = CustomSet #{int_list case.input.set2}",
23+
"assert.is.#{case.expected}, set1\\is_#{case.property} set2"
24+
}
25+
when 'add'
26+
lines = {
27+
"set = CustomSet #{int_list case.input.set}",
28+
"set\\#{case.property} #{case.input.element}"
29+
"expected = CustomSet #{int_list case.expected}"
30+
"assert.is.true set\\is_equal expected"
31+
}
32+
when 'intersection', 'difference', 'union'
33+
lines = {
34+
"set1 = CustomSet #{int_list case.input.set1}",
35+
"set2 = CustomSet #{int_list case.input.set2}",
36+
"result = set1\\#{case.property} set2"
37+
"expected = CustomSet #{int_list case.expected}"
38+
"assert.is.true result\\is_equal expected"
39+
}
40+
table.concat [indent line, level for line in *lines], '\n'
41+
42+
-- bonus tests are a multiline string indented with 6 spaces
43+
bonus: [[
44+
pending 'use the equal operator, are equal', ->
45+
set1 = CustomSet {1, 2, 3, 4}
46+
set2 = CustomSet {4, 3, 1, 2}
47+
assert.is.true set1 == set2
48+
49+
pending 'use the equal operator, not equal', ->
50+
set1 = CustomSet {1, 2, 3, 4}
51+
set2 = CustomSet {4, 3, 1, 20}
52+
assert.is.true set1 != set2
53+
]]
54+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[20c5f855-f83a-44a7-abdd-fe75c6cf022b]
13+
description = "Returns true if the set contains no elements -> sets with no elements are empty"
14+
15+
[d506485d-5706-40db-b7d8-5ceb5acf88d2]
16+
description = "Returns true if the set contains no elements -> sets with elements are not empty"
17+
18+
[759b9740-3417-44c3-8ca3-262b3c281043]
19+
description = "Sets can report if they contain an element -> nothing is contained in an empty set"
20+
21+
[f83cd2d1-2a85-41bc-b6be-80adbff4be49]
22+
description = "Sets can report if they contain an element -> when the element is in the set"
23+
24+
[93423fc0-44d0-4bc0-a2ac-376de8d7af34]
25+
description = "Sets can report if they contain an element -> when the element is not in the set"
26+
27+
[c392923a-637b-4495-b28e-34742cd6157a]
28+
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"
29+
30+
[5635b113-be8c-4c6f-b9a9-23c485193917]
31+
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"
32+
33+
[832eda58-6d6e-44e2-92c2-be8cf0173cee]
34+
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"
35+
36+
[c830c578-8f97-4036-b082-89feda876131]
37+
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"
38+
39+
[476a4a1c-0fd1-430f-aa65-5b70cbc810c5]
40+
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"
41+
42+
[d2498999-3e46-48e4-9660-1e20c3329d3d]
43+
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"
44+
45+
[7d38155e-f472-4a7e-9ad8-5c1f8f95e4cc]
46+
description = "Sets are disjoint if they share no elements -> the empty set is disjoint with itself"
47+
48+
[7a2b3938-64b6-4b32-901a-fe16891998a6]
49+
description = "Sets are disjoint if they share no elements -> empty set is disjoint with non-empty set"
50+
51+
[589574a0-8b48-48ea-88b0-b652c5fe476f]
52+
description = "Sets are disjoint if they share no elements -> non-empty set is disjoint with empty set"
53+
54+
[febeaf4f-f180-4499-91fa-59165955a523]
55+
description = "Sets are disjoint if they share no elements -> sets are not disjoint if they share an element"
56+
57+
[0de20d2f-c952-468a-88c8-5e056740f020]
58+
description = "Sets are disjoint if they share no elements -> sets are disjoint if they share no elements"
59+
60+
[4bd24adb-45da-4320-9ff6-38c044e9dff8]
61+
description = "Sets with the same elements are equal -> empty sets are equal"
62+
63+
[f65c0a0e-6632-4b2d-b82c-b7c6da2ec224]
64+
description = "Sets with the same elements are equal -> empty set is not equal to non-empty set"
65+
66+
[81e53307-7683-4b1e-a30c-7e49155fe3ca]
67+
description = "Sets with the same elements are equal -> non-empty set is not equal to empty set"
68+
69+
[d57c5d7c-a7f3-48cc-a162-6b488c0fbbd0]
70+
description = "Sets with the same elements are equal -> sets with the same elements are equal"
71+
72+
[dd61bafc-6653-42cc-961a-ab071ee0ee85]
73+
description = "Sets with the same elements are equal -> sets with different elements are not equal"
74+
75+
[06059caf-9bf4-425e-aaff-88966cb3ea14]
76+
description = "Sets with the same elements are equal -> set is not equal to larger set with same elements"
77+
78+
[d4a1142f-09aa-4df9-8b83-4437dcf7ec24]
79+
description = "Sets with the same elements are equal -> set is equal to a set constructed from an array with duplicates"
80+
81+
[8a677c3c-a658-4d39-bb88-5b5b1a9659f4]
82+
description = "Unique elements can be added to a set -> add to empty set"
83+
84+
[0903dd45-904d-4cf2-bddd-0905e1a8d125]
85+
description = "Unique elements can be added to a set -> add to non-empty set"
86+
87+
[b0eb7bb7-5e5d-4733-b582-af771476cb99]
88+
description = "Unique elements can be added to a set -> adding an existing element does not change the set"
89+
90+
[893d5333-33b8-4151-a3d4-8f273358208a]
91+
description = "Intersection returns a set of all shared elements -> intersection of two empty sets is an empty set"
92+
93+
[d739940e-def2-41ab-a7bb-aaf60f7d782c]
94+
description = "Intersection returns a set of all shared elements -> intersection of an empty set and non-empty set is an empty set"
95+
96+
[3607d9d8-c895-4d6f-ac16-a14956e0a4b7]
97+
description = "Intersection returns a set of all shared elements -> intersection of a non-empty set and an empty set is an empty set"
98+
99+
[b5120abf-5b5e-41ab-aede-4de2ad85c34e]
100+
description = "Intersection returns a set of all shared elements -> intersection of two sets with no shared elements is an empty set"
101+
102+
[af21ca1b-fac9-499c-81c0-92a591653d49]
103+
description = "Intersection returns a set of all shared elements -> intersection of two sets with shared elements is a set of the shared elements"
104+
105+
[c5e6e2e4-50e9-4bc2-b89f-c518f015b57e]
106+
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"
107+
108+
[2024cc92-5c26-44ed-aafd-e6ca27d6fcd2]
109+
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"
110+
111+
[e79edee7-08aa-4c19-9382-f6820974b43e]
112+
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"
113+
114+
[c5ac673e-d707-4db5-8d69-7082c3a5437e]
115+
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"
116+
117+
[20d0a38f-7bb7-4c4a-ac15-90c7392ecf2b]
118+
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"
119+
120+
[c45aed16-5494-455a-9033-5d4c93589dc6]
121+
description = "Union returns a set of all elements in either set -> union of empty sets is an empty set"
122+
123+
[9d258545-33c2-4fcb-a340-9f8aa69e7a41]
124+
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"
125+
126+
[3aade50c-80c7-4db8-853d-75bac5818b83]
127+
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"
128+
129+
[a00bb91f-c4b4-4844-8f77-c73e2e9df77c]
130+
description = "Union returns a set of all elements in either set -> union of non-empty sets contains all unique elements"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class CustomSet
2+
new: (items) =>
3+
error 'Implement the constructor'
4+
5+
add: (item) =>
6+
error 'Implement the add method'
7+
8+
is_empty: =>
9+
error 'Implement the is_empty method'
10+
11+
contains: (item) =>
12+
error 'Implement the contains method'
13+
14+
is_subset: (other) =>
15+
error 'Implement the is_subset method'
16+
17+
is_disjoint: (other) =>
18+
error 'Implement the is_disjoint method'
19+
20+
is_equal: (other) =>
21+
error 'Implement the is_equal method'
22+
23+
intersection: (other) =>
24+
error 'Implement the intersection method'
25+
26+
difference: (other) =>
27+
error 'Implement the difference method'
28+
29+
union: (other) =>
30+
error 'Implement the union method'

0 commit comments

Comments
 (0)