Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions exercises/practice/custom-set/.meta/CustomSet.example.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,39 @@
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
}
}
}

[bool] IsEmpty() {
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) {
Expand All @@ -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))
Expand All @@ -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
}
}
38 changes: 19 additions & 19 deletions exercises/practice/custom-set/CustomSet.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ Describe "custom set test cases" {
$set2 = [CustomSet]::new(@(1, 1))
$got = $set1 -eq $set2

$got | Should -BeFalse
$got | Should -BeTrue
}
}

Expand All @@ -200,23 +200,23 @@ 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" {
$set = [CustomSet]::new(@(1))
$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" {
$set = [CustomSet]::new(@(1, 2, 3))
$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" {
Expand All @@ -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" {
Expand All @@ -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" {
Expand All @@ -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" {
Expand All @@ -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" {
Expand All @@ -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" {
Expand All @@ -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" {
Expand All @@ -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" {
Expand All @@ -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" {
Expand All @@ -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" {
Expand All @@ -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" {
Expand All @@ -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" {
Expand All @@ -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" {
Expand All @@ -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" {
Expand All @@ -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
}
}

Expand All @@ -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
}
}
}
Expand Down