From f951e8d0f977c5fccfd42cc0638dce2a17274220 Mon Sep 17 00:00:00 2001 From: glaxxie <86179463+glaxxie@users.noreply.github.com> Date: Sat, 19 Oct 2024 21:24:16 -0500 Subject: [PATCH] [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 --- .../custom-set/.meta/CustomSet.example.ps1 | 43 ++++++++++++------- .../practice/custom-set/CustomSet.tests.ps1 | 38 ++++++++-------- 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/exercises/practice/custom-set/.meta/CustomSet.example.ps1 b/exercises/practice/custom-set/.meta/CustomSet.example.ps1 index 1889db4..47c43c0 100644 --- a/exercises/practice/custom-set/.meta/CustomSet.example.ps1 +++ b/exercises/practice/custom-set/.meta/CustomSet.example.ps1 @@ -23,17 +23,18 @@ Returns: $true #> Class CustomSet { - [Object[]] hidden $Set + [Object[]] $Set CustomSet() { $this.Set = @() } CustomSet([Object[]]$values) { - if ($values.Count -eq 0) { - $this.Set = @() - }else { - $this.Set += $values | Where-Object { $this.Set -notcontains $_ } + $this.Set = @() + foreach ($val in $values) { + if ($val -notin $this.Set) { + $this.Set += $val + } } } @@ -41,13 +42,20 @@ Class CustomSet { return $this.Set.Count -eq 0 } - [bool] Contains([Object]$element) { + [bool] Contains([object]$element) { return $this.Set -contains $element } - [bool] IsSubset([CustomSet]$otherSet) { - $overlap = Compare-Object $this.Set $otherSet.Set -IncludeEqual -ExcludeDifferent - return $overlap.Count -eq $this.Set.Count + [bool] IsSubset([CustomSet]$other) { + if ($this.IsEmpty()) { + return $true + } + foreach ($element in $this.Set) { + if (-not $other.Contains($element)) { + return $false + } + } + return $true } [bool] IsDisjoint([CustomSet]$otherSet) { @@ -66,11 +74,11 @@ Class CustomSet { } [CustomSet] Difference([CustomSet]$otherSet) { - if ($otherSet.Set.Count -eq 0) { + if ($otherSet.IsEmpty()) { return [CustomSet]::new($this.Set) } $difA = $this.Set | Where-Object {$_ -notin $otherSet.Set} - if ($this.Set.Count -eq 0 -or $difA.Count -eq 0) { + if ($this.IsEmpty() -or $difA.Count -eq 0) { return [CustomSet]::new() } return [CustomSet]::new(@($difA)) @@ -81,10 +89,15 @@ Class CustomSet { return [CustomSet]::new(@($overlap)) } - [bool] Equals([Object]$otherSet) { - if ($otherSet -is [CustomSet]) { - return -not (Compare-Object $this.Set $otherSet.Set) + [bool] Equals($other) { + if ($this.IsEmpty() -and $other.IsEmpty()) { + return $true + } + foreach ($element in $this.Set) { + if (-not $other.Contains($element)) { + return $false + } } - return $false + return $this.Set.Count -eq $other.Set.Count } } diff --git a/exercises/practice/custom-set/CustomSet.tests.ps1 b/exercises/practice/custom-set/CustomSet.tests.ps1 index 627d06e..214b362 100644 --- a/exercises/practice/custom-set/CustomSet.tests.ps1 +++ b/exercises/practice/custom-set/CustomSet.tests.ps1 @@ -190,7 +190,7 @@ Describe "custom set test cases" { $set2 = [CustomSet]::new(@(1, 1)) $got = $set1 -eq $set2 - $got | Should -BeFalse + $got | Should -BeTrue } } @@ -200,7 +200,7 @@ Describe "custom set test cases" { $got = $set.Add(1) $want = [CustomSet]::new(1) - $got | Should -BeExactly $want + $got | Should -Be $want } It "add -> add to non-empty set" { @@ -208,7 +208,7 @@ Describe "custom set test cases" { $got = $set.Add(5) $want = [CustomSet]::new(@(1,5)) - $got | Should -BeExactly $want + $got | Should -Be $want } It "add -> adding an existing element does not change the set" { @@ -216,7 +216,7 @@ Describe "custom set test cases" { $got = $set.Add(3) $want = [CustomSet]::new(@(1, 2, 3)) - $got | Should -BeExactly $want + $got | Should -Be $want } It "difference (or complement) -> difference of two empty sets is an empty set" { @@ -226,7 +226,7 @@ Describe "custom set test cases" { $got = $set1.Difference($set2) $want = [CustomSet]::new() - $got | Should -BeExactly $want + $got | Should -Be $want } 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" { $got = $set1.Difference($set2) $want = [CustomSet]::new() - $got | Should -BeExactly $want + $got | Should -Be $want } 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" { $got = $set1.Difference($set2) $want = [CustomSet]::new(@(1, 3, 4)) - $got | Should -BeExactly $want + $got | Should -Be $want } 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" { $got = $set1.Difference($set2) $want = [CustomSet]::new(@(1, 3)) - $got | Should -BeExactly $want + $got | Should -Be $want } 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" { $got = $set1.Difference($set2) $want = [CustomSet]::new() - $got | Should -BeExactly $want + $got | Should -Be $want } It "union -> union of empty sets is an empty set" { @@ -276,7 +276,7 @@ Describe "custom set test cases" { $got = $set1.Union($set2) $want = [CustomSet]::new() - $got | Should -BeExactly $want + $got | Should -Be $want } 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" { $got = $set1.Union($set2) $want = [CustomSet]::new(@(2, 1)) - $got | Should -BeExactly $want + $got | Should -Be $want } 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" { $got = $set1.Union($set2) $want = [CustomSet]::new(@(3, 5, 7)) - $got | Should -BeExactly $want + $got | Should -Be $want } It "union -> union of non-empty sets contains all unique elements" { @@ -306,7 +306,7 @@ Describe "custom set test cases" { $got = $set1.Union($set2) $want = [CustomSet]::new(@(3, 5, 7, 1, 8)) - $got | Should -BeExactly $want + $got | Should -Be $want } It "intersection -> intersection of two empty sets is an empty set" { @@ -316,7 +316,7 @@ Describe "custom set test cases" { $got = $set1.Intersection($set2) $want = [CustomSet]::new() - $got | Should -BeExactly $want + $got | Should -Be $want } It "intersection -> intersection of an empty set and non-empty set is an empty set" { @@ -326,7 +326,7 @@ Describe "custom set test cases" { $got = $set1.Intersection($set2) $want = [CustomSet]::new() - $got | Should -BeExactly $want + $got | Should -Be $want } 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" { $got = $set1.Intersection($set2) $want = [CustomSet]::new() - $got | Should -BeExactly $want + $got | Should -Be $want } It "intersection -> intersection of two sets with no shared elements is an empty set" { @@ -346,7 +346,7 @@ Describe "custom set test cases" { $got = $set1.Intersection($set2) $want = [CustomSet]::new() - $got | Should -BeExactly $want + $got | Should -Be $want } 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" { $got = $set1.Intersection($set2) $want = [CustomSet]::new(@(4, 8, 5)) - $got | Should -BeExactly $want + $got | Should -Be $want } } @@ -368,7 +368,7 @@ Describe "custom set test cases" { $got = $set1.Intersection($set2).Add(1).Union($set2).Difference($set2) $want = [CustomSet]::new(@(1)) - $got | Should -BeExactly $want + $got | Should -Be $want } } }