-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathSets.scala
159 lines (129 loc) · 5.94 KB
/
Sets.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* scala-exercises - exercises-stdlib
* Copyright (C) 2015-2016 47 Degrees, LLC. <http://www.47deg.com>
*/
package stdlib
import org.scalatest._
/** @param name sets
*
*/
object Sets extends FlatSpec with Matchers with org.scalaexercises.definitions.Section {
/** `Set`s are `Iterable`s that contain no duplicate elements. The operations on sets are summarized in the following table for general sets and in the table after that for mutable sets. They fall into the following categories:
*
* - **Tests**: `contains`, `apply`, `subsetOf`. The `contains` method asks whether a set contains a given element. The `apply` method for a set is the same as `contains`, so `set(elem)` is the same as `set contains elem`. That means sets can also be used as test functions that return true for the elements they contain.
* - **Additions**: `+` and `++`, which add one or more elements to a set, yielding a new set.
* - **Removals**: `-`, `--`, which remove one or more elements from a set, yielding a new set.
* - **Set operations**: union, intersection, and set difference. Each of these operations exists in two forms: alphabetic and symbolic. The alphabetic versions are `intersect`, `union`, and `diff`, whereas the symbolic versions are `&`, `|`, and `&~`. In fact, the `++` that Set inherits from `Traversable` can be seen as yet another alias of `union` or `|`, except that `++` takes a `Traversable` argument whereas `union` and `|` take sets.
*
* Sets can be created easily:
*/
def noDuplicatesSets(res0: Int) {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
mySet.size should be(res0)
}
/** Sets contain distinct values:
*/
def distinctValuesSets(res0: Int) {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Michigan")
mySet.size should be(res0)
}
/** Sets can be added to easily:
*/
def easilyAddedSets(res0: Boolean, res1: Boolean) {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val aNewSet = mySet + "Illinois"
aNewSet.contains("Illinois") should be(res0)
mySet.contains("Illinois") should be(res1)
}
/** Sets may be of mixed type:
*/
def mixedTypeSets(res0: Boolean, res1: Boolean) {
val mySet = Set("Michigan", "Ohio", 12)
mySet.contains(12) should be(res0)
mySet.contains("MI") should be(res1)
}
/** Sets can be checked for member existence:
*/
def checkExistenceSets(res0: Boolean, res1: Boolean) {
val mySet = Set("Michigan", "Ohio", 12)
mySet(12) should be(res0)
mySet("MI") should be(res1)
}
/** Set elements can be removed easily:
*/
def easilyRemovedSets(res0: Boolean, res1: Boolean) {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val aNewSet = mySet - "Michigan"
aNewSet.contains("Michigan") should be(res0)
mySet.contains("Michigan") should be(res1)
}
/** Set elements can be removed in multiple:
*/
def multipleRemovingSets(res0: Boolean, res1: Boolean, res2: Int) {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val aNewSet = mySet -- List("Michigan", "Ohio")
aNewSet.contains("Michigan") should be(res0)
aNewSet.contains("Wisconsin") should be(res1)
aNewSet.size should be(res2)
}
/** Set elements can be removed with a tuple:
* (note -- unary '-' on a tuple with more than one value is deprecated from scala 2.13.0)
*/
def tupleRemovingSets(res0: Boolean, res1: Boolean, res2: Int) {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val aNewSet = mySet - ("Michigan", "Ohio") // Notice: single '-' operator for tuples
// the above operation will work, but result in a warning:
// warning: method - in trait SetOps is deprecated (since 2.13.0): Use &- with an explicit collection argument instead of - with varargs
aNewSet.contains("Michigan") should be(res0)
aNewSet.contains("Wisconsin") should be(res1)
aNewSet.size should be(res2)
}
/** Attempted removal of nonexistent elements from a set is handled gracefully:
*/
def nonexistentRemovalSets(res0: Boolean) {
val mySet = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val aNewSet = mySet - "Minnesota"
aNewSet.equals(mySet) should be(res0)
}
/** Two sets can be intersected easily:
*/
def easilyIntersectedSets(res0: Boolean) {
val mySet1 = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val mySet2 = Set("Wisconsin", "Michigan", "Minnesota")
val aNewSet = mySet1 intersect mySet2
// NOTE: Scala 2.7 used **, deprecated for & or intersect in Scala 2.8
aNewSet.equals(Set("Michigan", "Wisconsin")) should be(res0)
}
/** Two sets can be joined as their union easily:
*/
def easilyJoinedSets(res0: Boolean) {
val mySet1 = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val mySet2 = Set("Wisconsin", "Michigan", "Minnesota")
val aNewSet = mySet1 union mySet2 // NOTE: You can also use the "|" operator
aNewSet.equals(Set("Michigan", "Wisconsin", "Ohio", "Iowa", "Minnesota")) should be(res0)
}
/** A set is either a subset of another set or it isn't:
*/
def subsetSets(res0: Boolean, res1: Boolean) {
val mySet1 = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val mySet2 = Set("Wisconsin", "Michigan", "Minnesota")
val mySet3 = Set("Wisconsin", "Michigan")
mySet2 subsetOf mySet1 should be(res0)
mySet3 subsetOf mySet1 should be(res1)
}
/** The difference between two sets can be obtained easily:
*/
def easilyObtainedDifferencesSets(res0: Boolean) {
val mySet1 = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val mySet2 = Set("Wisconsin", "Michigan")
val aNewSet = mySet1 diff mySet2 // Note: you can use the "&~" operator if you *really* want to.
aNewSet.equals(Set("Ohio", "Iowa")) should be(res0)
}
/** Set equivalency is independent of order:
*/
def equivalencySets(res0: Boolean) {
val mySet1 = Set("Michigan", "Ohio", "Wisconsin", "Iowa")
val mySet2 = Set("Wisconsin", "Michigan", "Ohio", "Iowa")
mySet1.equals(mySet2) should be(res0)
}
}