Skip to content

Commit 749d7e2

Browse files
authored
Add custom-set (#355)
1 parent 9135353 commit 749d7e2

File tree

7 files changed

+432
-0
lines changed

7 files changed

+432
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,14 @@
641641
"text_formatting"
642642
]
643643
},
644+
{
645+
"slug": "custom-set",
646+
"name": "Custom Set",
647+
"uuid": "560b738f-c20d-43ac-9bc5-4821628adc00",
648+
"practices": [],
649+
"prerequisites": [],
650+
"difficulty": 5
651+
},
644652
{
645653
"slug": "kindergarten-garden",
646654
"name": "Kindergarten Garden",
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+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"custom-set.coffee"
8+
],
9+
"test": [
10+
"custom-set.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
14+
]
15+
},
16+
"blurb": "Create a custom set type."
17+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class CustomSet
2+
constructor: (values) ->
3+
values.sort()
4+
@values = values.filter (value, index) ->
5+
values.indexOf(value) is index
6+
7+
empty: ->
8+
@values.length == 0
9+
10+
contains: (value) ->
11+
@values.indexOf(value) != -1
12+
13+
subset: (other) ->
14+
@values.every (item) -> other.contains item
15+
16+
disjoint: (other) ->
17+
@values.every (item) -> !other.contains item
18+
19+
equal: (other) ->
20+
if @values.length != other.values.length
21+
return false
22+
else
23+
24+
for elt, i in @values
25+
return false if elt != other.values[i]
26+
true
27+
28+
add: (value) ->
29+
if !(@contains value)
30+
@values.push value
31+
@values.sort()
32+
33+
intersection: (other) ->
34+
new CustomSet @values.filter (item) -> other.contains item
35+
36+
difference: (other) ->
37+
new CustomSet @values.filter (item) -> !other.contains item
38+
39+
union: (other) ->
40+
new CustomSet @values.concat other.values
41+
42+
module.exports = CustomSet
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class CustomSet
2+
constructor: (values) ->
3+
4+
empty: ->
5+
6+
contains: (value) ->
7+
8+
subset: (other) ->
9+
10+
disjoint: (other) ->
11+
12+
equall: (other) ->
13+
14+
add: (value) ->
15+
16+
intersection: (other) ->
17+
18+
difference: (other) ->
19+
20+
union: (other) ->
21+
22+
module.exports = CustomSet

0 commit comments

Comments
 (0)