Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise custom-set tests to comply with problem-spec #404

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
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
70 changes: 39 additions & 31 deletions exercises/custom-set/Sources/CustomSet/CustomSetExample.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
func == <T> (lh: CustomSet<T>, rh: CustomSet<T>) -> Bool {
return lh.contents.keys.sorted { $0.hashValue < $1.hashValue } == rh.contents.keys.sorted { $0.hashValue < $1.hashValue }

}

extension CustomSet where T: Comparable {

var toSortedArray: [Element] { return Array(contents.keys.sorted { $0 < $1 }) }
Expand All @@ -14,15 +9,29 @@ struct CustomSet<T: Hashable>: Equatable {

fileprivate var contents = [Element: Bool]()

var isEmpty: Bool { return contents.count == 0 }
var size: Int { return contents.count }

init<S: Sequence>(_ sequence: S) where S.Iterator.Element == Element {
self.contents = [:]
_ = sequence.map { self.contents[$0] = true }
}
mutating func put(_ item: Element) {
contents[item] = true

static func == <T>(lh: CustomSet<T>, rh: CustomSet<T>) -> Bool {
return lh.size == rh.size &&
lh.contents.keys.sorted { $0.hashValue < $1.hashValue } ==
rh.contents.keys.sorted { $0.hashValue < $1.hashValue }
}

func contains(_ item: Element) -> Bool {
if contents[item] != nil {
return true
}
return false
}

mutating func add(_ item: Element) {
contents[item] = true
}

mutating func delete(_ item: Element) {
Expand All @@ -35,20 +44,22 @@ struct CustomSet<T: Hashable>: Equatable {

}

func intersection(_ item: CustomSet) -> CustomSet {
var temp = [Element: Bool]()
for each in Array(item.contents.keys) {
guard contents[each] != nil else { continue }
temp[each] = true
func intersection(_ other: CustomSet) -> CustomSet {
var intersect = CustomSet([Element]())
for elem in Array(other.contents.keys) {
guard contents[elem] != nil else { continue }
intersect.add(elem)
}
return CustomSet(temp.keys)
return intersect
}
func difference(_ item: CustomSet) -> CustomSet {
var temp = contents
for each in Array(item.contents.keys) {
temp[each] = nil

func difference(_ other: CustomSet) -> CustomSet {
var diff = CustomSet([Element]())
for elem in Array(contents.keys) {
guard other.contents[elem] != true else { continue }
diff.add(elem)
}
return CustomSet(temp.keys)
return diff
}

func union(_ item: CustomSet) -> CustomSet {
Expand All @@ -58,25 +69,22 @@ struct CustomSet<T: Hashable>: Equatable {
}
return CustomSet(temp.keys)
}
func isSupersetOf (_ item: CustomSet) -> Bool {

return item.contents.count == item.contents.filter { self.contents.keys.contains($0.0) }.count

}
func isDisjoint(_ item: CustomSet) -> Bool {

for each in Array(item.contents.keys) {
if contents.keys.contains(each) {
func isSubsetOf(_ other: CustomSet) -> Bool {
for elem in Array(self.contents.keys) {
if !other.contents.keys.contains(elem) {
return false
}
}
return true
}

func containsMember(_ item: Element) -> Bool {
if contents[item] != nil {
return true}
return false
func isDisjoint(_ item: CustomSet) -> Bool {
for elem in Array(item.contents.keys) {
if contents.keys.contains(elem) {
return false
}
}
return true
}

}
Loading