forked from kodecocodes/swift-algorithm-club
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
63 lines (50 loc) · 1.66 KB
/
Contents.swift
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
//: Playground - noun: a place where people can play
// last checked with Xcode 9.0b4
#if swift(>=4.0)
print("Hello, Swift 4!")
#endif
var dsu = UnionFindQuickUnion<Int>()
for i in 1...10 {
dsu.addSetWith(i)
}
// now our dsu contains 10 independent sets
// let's divide our numbers into two sets by divisibility by 2
for i in 3...10 {
if i % 2 == 0 {
dsu.unionSetsContaining(2, and: i)
} else {
dsu.unionSetsContaining(1, and: i)
}
}
// check our division
print(dsu.inSameSet(2, and: 4))
print(dsu.inSameSet(4, and: 6))
print(dsu.inSameSet(6, and: 8))
print(dsu.inSameSet(8, and: 10))
print(dsu.inSameSet(1, and: 3))
print(dsu.inSameSet(3, and: 5))
print(dsu.inSameSet(5, and: 7))
print(dsu.inSameSet(7, and: 9))
print(dsu.inSameSet(7, and: 4))
print(dsu.inSameSet(3, and: 6))
var dsuForStrings = UnionFindQuickUnion<String>()
let words = ["all", "border", "boy", "afternoon", "amazing", "awesome", "best"]
dsuForStrings.addSetWith("a")
dsuForStrings.addSetWith("b")
// In that example we divide strings by its first letter
for word in words {
dsuForStrings.addSetWith(word)
if word.hasPrefix("a") {
dsuForStrings.unionSetsContaining("a", and: word)
} else if word.hasPrefix("b") {
dsuForStrings.unionSetsContaining("b", and: word)
}
}
print(dsuForStrings.inSameSet("a", and: "all"))
print(dsuForStrings.inSameSet("all", and: "awesome"))
print(dsuForStrings.inSameSet("amazing", and: "afternoon"))
print(dsuForStrings.inSameSet("b", and: "boy"))
print(dsuForStrings.inSameSet("best", and: "boy"))
print(dsuForStrings.inSameSet("border", and: "best"))
print(dsuForStrings.inSameSet("amazing", and: "boy"))
print(dsuForStrings.inSameSet("all", and: "border"))