Skip to content

Commit f951e8d

Browse files
authored
[Fix]: Mistake in Custom Set exercise test suite (#401)
* Fix custom set exercise Fix a mistake from test suite : "set is equal to a set constructed from an array with duplicates" should be true instead of false Changes made to example file and the test suite to reflect new changes
1 parent 6fd0dce commit f951e8d

File tree

2 files changed

+47
-34
lines changed

2 files changed

+47
-34
lines changed

exercises/practice/custom-set/.meta/CustomSet.example.ps1

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,39 @@
2323
Returns: $true
2424
#>
2525
Class CustomSet {
26-
[Object[]] hidden $Set
26+
[Object[]] $Set
2727

2828
CustomSet() {
2929
$this.Set = @()
3030
}
3131

3232
CustomSet([Object[]]$values) {
33-
if ($values.Count -eq 0) {
34-
$this.Set = @()
35-
}else {
36-
$this.Set += $values | Where-Object { $this.Set -notcontains $_ }
33+
$this.Set = @()
34+
foreach ($val in $values) {
35+
if ($val -notin $this.Set) {
36+
$this.Set += $val
37+
}
3738
}
3839
}
3940

4041
[bool] IsEmpty() {
4142
return $this.Set.Count -eq 0
4243
}
4344

44-
[bool] Contains([Object]$element) {
45+
[bool] Contains([object]$element) {
4546
return $this.Set -contains $element
4647
}
4748

48-
[bool] IsSubset([CustomSet]$otherSet) {
49-
$overlap = Compare-Object $this.Set $otherSet.Set -IncludeEqual -ExcludeDifferent
50-
return $overlap.Count -eq $this.Set.Count
49+
[bool] IsSubset([CustomSet]$other) {
50+
if ($this.IsEmpty()) {
51+
return $true
52+
}
53+
foreach ($element in $this.Set) {
54+
if (-not $other.Contains($element)) {
55+
return $false
56+
}
57+
}
58+
return $true
5159
}
5260

5361
[bool] IsDisjoint([CustomSet]$otherSet) {
@@ -66,11 +74,11 @@ Class CustomSet {
6674
}
6775

6876
[CustomSet] Difference([CustomSet]$otherSet) {
69-
if ($otherSet.Set.Count -eq 0) {
77+
if ($otherSet.IsEmpty()) {
7078
return [CustomSet]::new($this.Set)
7179
}
7280
$difA = $this.Set | Where-Object {$_ -notin $otherSet.Set}
73-
if ($this.Set.Count -eq 0 -or $difA.Count -eq 0) {
81+
if ($this.IsEmpty() -or $difA.Count -eq 0) {
7482
return [CustomSet]::new()
7583
}
7684
return [CustomSet]::new(@($difA))
@@ -81,10 +89,15 @@ Class CustomSet {
8189
return [CustomSet]::new(@($overlap))
8290
}
8391

84-
[bool] Equals([Object]$otherSet) {
85-
if ($otherSet -is [CustomSet]) {
86-
return -not (Compare-Object $this.Set $otherSet.Set)
92+
[bool] Equals($other) {
93+
if ($this.IsEmpty() -and $other.IsEmpty()) {
94+
return $true
95+
}
96+
foreach ($element in $this.Set) {
97+
if (-not $other.Contains($element)) {
98+
return $false
99+
}
87100
}
88-
return $false
101+
return $this.Set.Count -eq $other.Set.Count
89102
}
90103
}

exercises/practice/custom-set/CustomSet.tests.ps1

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ Describe "custom set test cases" {
190190
$set2 = [CustomSet]::new(@(1, 1))
191191
$got = $set1 -eq $set2
192192

193-
$got | Should -BeFalse
193+
$got | Should -BeTrue
194194
}
195195
}
196196

@@ -200,23 +200,23 @@ Describe "custom set test cases" {
200200
$got = $set.Add(1)
201201
$want = [CustomSet]::new(1)
202202

203-
$got | Should -BeExactly $want
203+
$got | Should -Be $want
204204
}
205205

206206
It "add -> add to non-empty set" {
207207
$set = [CustomSet]::new(@(1))
208208
$got = $set.Add(5)
209209
$want = [CustomSet]::new(@(1,5))
210210

211-
$got | Should -BeExactly $want
211+
$got | Should -Be $want
212212
}
213213

214214
It "add -> adding an existing element does not change the set" {
215215
$set = [CustomSet]::new(@(1, 2, 3))
216216
$got = $set.Add(3)
217217
$want = [CustomSet]::new(@(1, 2, 3))
218218

219-
$got | Should -BeExactly $want
219+
$got | Should -Be $want
220220
}
221221

222222
It "difference (or complement) -> difference of two empty sets is an empty set" {
@@ -226,7 +226,7 @@ Describe "custom set test cases" {
226226
$got = $set1.Difference($set2)
227227
$want = [CustomSet]::new()
228228

229-
$got | Should -BeExactly $want
229+
$got | Should -Be $want
230230
}
231231

232232
It "difference (or complement) -> difference of empty set and non-empty set is an empty set" {
@@ -236,7 +236,7 @@ Describe "custom set test cases" {
236236
$got = $set1.Difference($set2)
237237
$want = [CustomSet]::new()
238238

239-
$got | Should -BeExactly $want
239+
$got | Should -Be $want
240240
}
241241

242242
It "difference (or complement) -> difference of a non-empty set and an empty set is the non-empty set" {
@@ -246,7 +246,7 @@ Describe "custom set test cases" {
246246
$got = $set1.Difference($set2)
247247
$want = [CustomSet]::new(@(1, 3, 4))
248248

249-
$got | Should -BeExactly $want
249+
$got | Should -Be $want
250250
}
251251

252252
It "difference (or complement) -> difference of two non-empty sets is a set of elements that are only in the first set" {
@@ -256,7 +256,7 @@ Describe "custom set test cases" {
256256
$got = $set1.Difference($set2)
257257
$want = [CustomSet]::new(@(1, 3))
258258

259-
$got | Should -BeExactly $want
259+
$got | Should -Be $want
260260
}
261261

262262
It "difference (or complement) -> of a set is a set of all elements that are only in the first set removes all duplicates in the first set" {
@@ -266,7 +266,7 @@ Describe "custom set test cases" {
266266
$got = $set1.Difference($set2)
267267
$want = [CustomSet]::new()
268268

269-
$got | Should -BeExactly $want
269+
$got | Should -Be $want
270270
}
271271

272272
It "union -> union of empty sets is an empty set" {
@@ -276,7 +276,7 @@ Describe "custom set test cases" {
276276
$got = $set1.Union($set2)
277277
$want = [CustomSet]::new()
278278

279-
$got | Should -BeExactly $want
279+
$got | Should -Be $want
280280
}
281281

282282
It "union -> union of an empty set and non-empty set is the non-empty set" {
@@ -286,7 +286,7 @@ Describe "custom set test cases" {
286286
$got = $set1.Union($set2)
287287
$want = [CustomSet]::new(@(2, 1))
288288

289-
$got | Should -BeExactly $want
289+
$got | Should -Be $want
290290
}
291291

292292
It "union -> union of a non-empty set and empty set is the non-empty set" {
@@ -296,7 +296,7 @@ Describe "custom set test cases" {
296296
$got = $set1.Union($set2)
297297
$want = [CustomSet]::new(@(3, 5, 7))
298298

299-
$got | Should -BeExactly $want
299+
$got | Should -Be $want
300300
}
301301

302302
It "union -> union of non-empty sets contains all unique elements" {
@@ -306,7 +306,7 @@ Describe "custom set test cases" {
306306
$got = $set1.Union($set2)
307307
$want = [CustomSet]::new(@(3, 5, 7, 1, 8))
308308

309-
$got | Should -BeExactly $want
309+
$got | Should -Be $want
310310
}
311311

312312
It "intersection -> intersection of two empty sets is an empty set" {
@@ -316,7 +316,7 @@ Describe "custom set test cases" {
316316
$got = $set1.Intersection($set2)
317317
$want = [CustomSet]::new()
318318

319-
$got | Should -BeExactly $want
319+
$got | Should -Be $want
320320
}
321321

322322
It "intersection -> intersection of an empty set and non-empty set is an empty set" {
@@ -326,7 +326,7 @@ Describe "custom set test cases" {
326326
$got = $set1.Intersection($set2)
327327
$want = [CustomSet]::new()
328328

329-
$got | Should -BeExactly $want
329+
$got | Should -Be $want
330330
}
331331

332332
It "intersection -> intersection of a non-empty set and an empty set is an empty set" {
@@ -336,7 +336,7 @@ Describe "custom set test cases" {
336336
$got = $set1.Intersection($set2)
337337
$want = [CustomSet]::new()
338338

339-
$got | Should -BeExactly $want
339+
$got | Should -Be $want
340340
}
341341

342342
It "intersection -> intersection of two sets with no shared elements is an empty set" {
@@ -346,7 +346,7 @@ Describe "custom set test cases" {
346346
$got = $set1.Intersection($set2)
347347
$want = [CustomSet]::new()
348348

349-
$got | Should -BeExactly $want
349+
$got | Should -Be $want
350350
}
351351

352352
It "intersection -> intersection of two sets with shared elements is a set of the shared elements" {
@@ -356,7 +356,7 @@ Describe "custom set test cases" {
356356
$got = $set1.Intersection($set2)
357357
$want = [CustomSet]::new(@(4, 8, 5))
358358

359-
$got | Should -BeExactly $want
359+
$got | Should -Be $want
360360
}
361361
}
362362

@@ -368,7 +368,7 @@ Describe "custom set test cases" {
368368
$got = $set1.Intersection($set2).Add(1).Union($set2).Difference($set2)
369369
$want = [CustomSet]::new(@(1))
370370

371-
$got | Should -BeExactly $want
371+
$got | Should -Be $want
372372
}
373373
}
374374
}

0 commit comments

Comments
 (0)