Skip to content

Commit 82a6f51

Browse files
colinleachColin LeachColin Leach
authored
sets concept (exercism#815)
* WIP draft of sets concept * removed italics * sets moved from WIP --------- Co-authored-by: Colin Leach <[email protected]> Co-authored-by: Colin Leach <[email protected]>
1 parent 2470121 commit 82a6f51

File tree

5 files changed

+248
-0
lines changed

5 files changed

+248
-0
lines changed

concepts/sets/.meta/config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"authors": [
3+
"colinleach"
4+
],
5+
"contributors": [],
6+
"blurb": "A Set in Julia is an unordered collection of unique entries, with fast algorithms for contains, union, intersection and difference operations."
7+
}

concepts/sets/about.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# About
2+
3+
A [`Set`][set] is a collection of items with the following properties:
4+
5+
- Unordered.
6+
- Entries are unique, so attempts to add duplicates are silently ignored.
7+
- Supports many of the operations common with mathematical sets.
8+
9+
Create them with the `Set()` constructor, using any iterator as the parameter.
10+
11+
```julia-repl
12+
julia> s1 = Set(1:4)
13+
Set{Int64} with 4 elements:
14+
4
15+
2
16+
3
17+
1
18+
```
19+
20+
Add new elements with `push!()` (the same as with arrays), and remove them with `delete!()`.
21+
22+
```julia-repl
23+
julia> push!(s1, 5)
24+
Set{Int64} with 5 elements:
25+
5
26+
4
27+
2
28+
3
29+
1
30+
31+
# Duplicates are ignored
32+
julia> push!(s1, 3)
33+
Set{Int64} with 5 elements:
34+
5
35+
4
36+
2
37+
3
38+
1
39+
40+
julia> delete!(s1, 5)
41+
Set{Int64} with 4 elements:
42+
4
43+
2
44+
3
45+
1
46+
47+
julia> length(s1) # length counts entries, despite the non-sequential type
48+
4
49+
```
50+
51+
## Set operations
52+
53+
As with several other collection types, check membership with the `in` or `` operator (use `\in` then tab for the symbol).
54+
55+
```julia-repl
56+
julia> 3 ∈ s1
57+
true
58+
```
59+
60+
The following operations on pairs of Sets are supported (shortcuts to the operator symbol are shown in parentheses).
61+
62+
- `union(A, B)` or `A ∪ B` (`\cup`): all entries in A or B or both.
63+
- `intersect(A, B)` or `A ∩ B` (`\cap`): all entries common to both A and B.
64+
- `setdiff(A, B)` (no symbol): entries in A but not in B.
65+
- `symdiff(A, B)` (no symbol): entries in either A or B but not both.
66+
- `issubset(A, B)` or `A ⊆ B` (`\subseteq`) or `B ⊇ A` (`\supseteq`): `true` if all entries in A are also in B.
67+
- `issetequal(A, B)` (no symbol): `true` if A and B contain exactly the same entries.
68+
- `isdisjoint(A, B)` (no symbol): `true` if A and B contain no entries in common (so intersect is empty).
69+
70+
```julia-repl
71+
s1 = Set(1:4)
72+
s2 = Set(3:6)
73+
74+
julia> s1 ∪ s2 # union
75+
Set{Int64} with 6 elements:
76+
5
77+
4
78+
6
79+
2
80+
3
81+
1
82+
83+
julia> s1 ∩ s2 # intersect
84+
Set{Int64} with 2 elements:
85+
4
86+
3
87+
88+
julia> setdiff(s1, s2)
89+
Set{Int64} with 2 elements:
90+
2
91+
1
92+
93+
julia> symdiff(s1, s2)
94+
Set{Int64} with 4 elements:
95+
5
96+
6
97+
2
98+
1
99+
100+
julia> s1 ⊇ s2 # issubset
101+
false
102+
```
103+
104+
There are also mutating versions of many of these, with `!` added to the function name.
105+
See the [manual][set] for a full list of functions.
106+
107+
108+
[set]: https://docs.julialang.org/en/v1/base/collections/#Set-Like-Collections
109+
[union]: https://docs.julialang.org/en/v1/base/collections/#Base.union
110+
[intersect]: https://docs.julialang.org/en/v1/base/collections/#Base.intersect
111+
[setdiff]: https://docs.julialang.org/en/v1/base/collections/#Base.setdiff
112+
[symdiff]: https://docs.julialang.org/en/v1/base/collections/#Base.symdiff
113+
[issubset]: https://docs.julialang.org/en/v1/base/collections/#Base.issubset
114+
[issetequal]: https://docs.julialang.org/en/v1/base/collections/#Base.issetequal
115+
[isdisjoint]: https://docs.julialang.org/en/v1/base/collections/#Base.isdisjoint

concepts/sets/introduction.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Introduction
2+
3+
A [`Set`][set] is a collection of items with the following properties:
4+
5+
- Unordered.
6+
- Entries are unique, so attempts to add duplicates are silently ignored.
7+
- Supports many of the operations common with mathematical sets.
8+
9+
Create them with the `Set()` constructor, using any iterator as the parameter.
10+
11+
```julia-repl
12+
julia> s1 = Set(1:4)
13+
Set{Int64} with 4 elements:
14+
4
15+
2
16+
3
17+
1
18+
```
19+
20+
Add new elements with `push!()` (the same as with arrays), and remove them with `delete!()`.
21+
22+
```julia-repl
23+
julia> push!(s1, 5)
24+
Set{Int64} with 5 elements:
25+
5
26+
4
27+
2
28+
3
29+
1
30+
31+
# Duplicates are ignored
32+
julia> push!(s1, 3)
33+
Set{Int64} with 5 elements:
34+
5
35+
4
36+
2
37+
3
38+
1
39+
40+
julia> delete!(s1, 5)
41+
Set{Int64} with 4 elements:
42+
4
43+
2
44+
3
45+
1
46+
47+
julia> length(s1) # length counts entries, despite the non-sequential type
48+
4
49+
```
50+
51+
## Set operations
52+
53+
As with several other collection types, check membership with the `in` or `` operator (use `\in` then tab for the symbol).
54+
55+
```julia-repl
56+
julia> 3 ∈ s1
57+
true
58+
```
59+
60+
The following operations on pairs of Sets are supported (shortcuts to the operator symbol are shown in parentheses).
61+
62+
- `union(A, B)` or `A ∪ B` (`\cup`): all entries in A or B or both.
63+
- `intersect(A, B)` or `A ∩ B` (`\cap`): all entries common to both A and B.
64+
- `setdiff(A, B)` (no symbol): entries in A but not in B.
65+
- `symdiff(A, B)` (no symbol): entries in either A or B but not both.
66+
- `issubset(A, B)` or `A ⊆ B` (`\subseteq`) or `B ⊇ A` (`\supseteq`): `true` if all entries in A are also in B.
67+
- `issetequal(A, B)` (no symbol): `true` if A and B contain exactly the same entries.
68+
- `isdisjoint(A, B)` (no symbol): `true` if A and B contain no entries in common (so intersect is empty).
69+
70+
```julia-repl
71+
s1 = Set(1:4)
72+
s2 = Set(3:6)
73+
74+
julia> s1 ∪ s2 # union
75+
Set{Int64} with 6 elements:
76+
5
77+
4
78+
6
79+
2
80+
3
81+
1
82+
83+
julia> s1 ∩ s2 # intersect
84+
Set{Int64} with 2 elements:
85+
4
86+
3
87+
88+
julia> setdiff(s1, s2)
89+
Set{Int64} with 2 elements:
90+
2
91+
1
92+
93+
julia> symdiff(s1, s2)
94+
Set{Int64} with 4 elements:
95+
5
96+
6
97+
2
98+
1
99+
100+
julia> s1 ⊇ s2 # issubset
101+
false
102+
```
103+
104+
There are also mutating versions of many of these, with `!` added to the function name.
105+
See the [manual][set] for a full list of functions.
106+
107+
108+
[set]: https://docs.julialang.org/en/v1/base/collections/#Set-Like-Collections
109+
[union]: https://docs.julialang.org/en/v1/base/collections/#Base.union
110+
[intersect]: https://docs.julialang.org/en/v1/base/collections/#Base.intersect
111+
[setdiff]: https://docs.julialang.org/en/v1/base/collections/#Base.setdiff
112+
[symdiff]: https://docs.julialang.org/en/v1/base/collections/#Base.symdiff
113+
[issubset]: https://docs.julialang.org/en/v1/base/collections/#Base.issubset
114+
[issetequal]: https://docs.julialang.org/en/v1/base/collections/#Base.issetequal
115+
[isdisjoint]: https://docs.julialang.org/en/v1/base/collections/#Base.isdisjoint

concepts/sets/links.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[
2+
{
3+
"url": "https://docs.julialang.org/en/v1/base/collections/#Set-Like-Collections",
4+
"description": "Manual section on Sets and related collections."
5+
}
6+
]

config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,11 @@
11991199
"slug": "vectors",
12001200
"name": "Vectors"
12011201
},
1202+
{
1203+
"uuid": "67dfc465-1a97-4bc1-9177-97e3a3ba55da",
1204+
"slug": "sets",
1205+
"name": "Sets"
1206+
},
12021207
{
12031208
"uuid": "9babe703-1f5f-4f59-a4b2-349b8d461dd8",
12041209
"slug": "conditionals",

0 commit comments

Comments
 (0)