Skip to content

Commit 172532c

Browse files
author
318236213
committed
added playground and .swift code
1 parent 75049e1 commit 172532c

File tree

4 files changed

+325
-0
lines changed

4 files changed

+325
-0
lines changed
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
// random function returns a random number between the given range
2+
func randomNum(min: Int, max: Int) -> Int {
3+
return min + Int(arc4random_uniform(UInt32(max - min + 1)))
4+
}
5+
6+
// extension for String generates a random alphanumeric string of length 8
7+
extension String {
8+
static func random(length: Int = 8) -> String {
9+
10+
let base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
11+
var randomString: String = ""
12+
13+
for _ in 0..<length {
14+
let randomValue = arc4random_uniform(UInt32(base.characters.count))
15+
randomString += "\(base[base.startIndex.advancedBy(Int(randomValue))])"
16+
}
17+
18+
return randomString
19+
}
20+
}
21+
22+
// Player data type stores a random name, and a random number of points from 0 - 5000
23+
struct Player : Comparable {
24+
var name: String! = String.random()
25+
var points = randomNum(0, max: 5000)
26+
}
27+
28+
// == operator for struct Player
29+
// Player x is equal to Player y if and only if both players have the same name and number of points
30+
func ==(x: Player, y: Player) -> Bool {
31+
return x.name == y.name && x.points == y.points
32+
}
33+
34+
// < operator for struct Player
35+
// Player x is less than Player y if and only if x has less points than y
36+
func <(x: Player, y: Player) -> Bool {
37+
return x.points < y.points
38+
}
39+
40+
// prints a Player formatted with their name and number of points
41+
func print(player: Player){
42+
print("Player: \(player.name) | Points: \(player.points)")
43+
}
44+
45+
import Foundation
46+
47+
// An Ordered Set is a collection where all items in the set follow an ordering, usually ordered from
48+
// 'least' to 'most'. The way you value, and compare items can be user defined.
49+
public struct OrderedSet<T: Comparable> {
50+
private var internalSet: [T]! = nil
51+
52+
// returns size of Set
53+
public var count: Int {
54+
return internalSet!.count
55+
}
56+
57+
public init(){
58+
internalSet = [T]() // create the internal array on init
59+
}
60+
61+
// inserts an item
62+
// O(n log n)
63+
public mutating func insert(item: T){
64+
if exists(item) {
65+
return // don't add an item if it already exists
66+
}
67+
68+
// if the set is initially empty, we need to simply append the item to internalSet
69+
if count == 0 {
70+
internalSet.append(item)
71+
return
72+
}
73+
74+
for i in 0..<count {
75+
if internalSet[i] > item {
76+
internalSet.insert(item, atIndex: i)
77+
return
78+
}
79+
}
80+
81+
// if an item is larger than any item in the current set, append it to the back.
82+
internalSet.append(item)
83+
}
84+
85+
// removes an item if it exists
86+
public mutating func remove(item: T) {
87+
if !exists(item) {
88+
return
89+
}
90+
91+
internalSet.removeAtIndex(findIndex(item))
92+
}
93+
94+
// returns true if and only if the item exists somewhere in the set
95+
public func exists(item: T) -> Bool {
96+
let index = findIndex(item)
97+
return index != -1 && internalSet[index] == item
98+
}
99+
100+
// returns the index of an item if it exists, otherwise returns -1.
101+
public func findIndex(item: T) -> Int {
102+
var leftBound = 0
103+
var rightBound = count - 1
104+
105+
while leftBound <= rightBound {
106+
let mid = leftBound + ((rightBound - leftBound) / 2)
107+
108+
if internalSet[mid] > item {
109+
rightBound = mid - 1
110+
} else if internalSet[mid] < item {
111+
leftBound = mid + 1
112+
} else {
113+
return mid
114+
}
115+
}
116+
117+
return -1
118+
}
119+
120+
// returns the item at the given index. assertion fails if the index is out of the range
121+
// of [0, count)
122+
public subscript(index: Int) -> T {
123+
assert(index >= 0 && index < count)
124+
return internalSet[index]
125+
}
126+
127+
// returns the 'maximum' or 'largest' value in the set
128+
public func max() -> T! {
129+
return count == 0 ? nil : internalSet[count - 1]
130+
}
131+
132+
// returns the 'minimum' or 'smallest' value in the set
133+
public func min() -> T! {
134+
return count == 0 ? nil : internalSet[0]
135+
}
136+
137+
// returns the k largest element in the set, if k is in the range [1, count]
138+
// returns nil otherwise
139+
public func kLargest(k: Int) -> T! {
140+
return k > count || k <= 0 ? nil : internalSet[count - k]
141+
}
142+
143+
// returns the k smallest element in the set, if k is in the range [1, count]
144+
// returns nil otherwise
145+
public func kSmallest(k: Int) -> T! {
146+
return k > count || k <= 0 ? nil : internalSet[k - 1]
147+
}
148+
}
149+
150+
// Example 1 with type Int
151+
var mySet = OrderedSet<Int>()
152+
153+
// insert random ints into the set
154+
155+
for _ in 0..<50 {
156+
mySet.insert(randomNum(50, max: 500))
157+
}
158+
159+
print(mySet)
160+
161+
print(mySet.max())
162+
print(mySet.min())
163+
164+
// print the 5 largest values
165+
for k in 1..<6 {
166+
print(mySet.kLargest(k))
167+
}
168+
169+
// print the 5 lowest values
170+
for k in 1..<6 {
171+
print(mySet.kSmallest(k))
172+
}
173+
174+
175+
// Example 2 with type Player
176+
var playerSet = OrderedSet<Player>()
177+
178+
// populate with random players.
179+
var anotherPlayer = Player()
180+
for _ in 0..<20 {
181+
playerSet.insert(Player())
182+
}
183+
184+
// we'll look for this player later
185+
playerSet.insert(anotherPlayer)
186+
187+
// print all players in order
188+
for i in 0..<playerSet.count {
189+
print(playerSet[playerSet.count - i - 1])
190+
}
191+
192+
193+
// highest and lowest players:
194+
print(playerSet.max())
195+
print(playerSet.min())
196+
197+
// we'll find our player now
198+
print("'Another Player (\(anotherPlayer.name))' is ranked at level: \(playerSet.count - playerSet.findIndex(anotherPlayer)) with \(anotherPlayer.points) points")
199+
200+
201+
202+
203+
204+
205+
206+
207+
208+
209+
210+
211+
212+
213+
214+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' executeOnSourceChanges='false'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

Ordered Set/OrderedSet.swift

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// An Ordered Set is a collection where all items in the set follow an ordering, usually ordered from
2+
// 'least' to 'most'. The way you value, and compare items can be user defined.
3+
public struct OrderedSet<T: Comparable> {
4+
private var internalSet: [T]! = nil
5+
6+
// returns size of Set
7+
public var count: Int {
8+
return internalSet!.count
9+
}
10+
11+
public init(){
12+
internalSet = [T]() // create the internal array on init
13+
}
14+
15+
// inserts an item into the set if it doesn't exist
16+
public mutating func insert(item: T){
17+
if exists(item) {
18+
return // don't add an item if it already exists
19+
}
20+
21+
// if the set is initially empty, we need to simply append the item to internalSet
22+
if count == 0 {
23+
internalSet.append(item)
24+
return
25+
}
26+
27+
for i in 0..<count {
28+
if internalSet[i] > item {
29+
internalSet.insert(item, atIndex: i)
30+
return
31+
}
32+
}
33+
34+
// if an item is larger than any item in the current set, append it to the back.
35+
internalSet.append(item)
36+
}
37+
38+
// removes an item if it exists
39+
public mutating func remove(item: T) {
40+
if !exists(item) {
41+
return
42+
}
43+
44+
internalSet.removeAtIndex(findIndex(item))
45+
}
46+
47+
// returns true if and only if the item exists somewhere in the set
48+
public func exists(item: T) -> Bool {
49+
let index = findIndex(item)
50+
return index != -1 && internalSet[index] == item
51+
}
52+
53+
// returns the index of an item if it exists, otherwise returns -1.
54+
public func findIndex(item: T) -> Int {
55+
var leftBound = 0
56+
var rightBound = count - 1
57+
58+
while leftBound <= rightBound {
59+
let mid = leftBound + ((rightBound - leftBound) / 2)
60+
61+
if internalSet[mid] > item {
62+
rightBound = mid - 1
63+
} else if internalSet[mid] < item {
64+
leftBound = mid + 1
65+
} else {
66+
return mid
67+
}
68+
}
69+
70+
return -1
71+
}
72+
73+
// returns the item at the given index. assertion fails if the index is out of the range
74+
// of [0, count)
75+
public subscript(index: Int) -> T {
76+
assert(index >= 0 && index < count)
77+
return internalSet[index]
78+
}
79+
80+
// returns the 'maximum' or 'largest' value in the set
81+
public func max() -> T! {
82+
return count == 0 ? nil : internalSet[count - 1]
83+
}
84+
85+
// returns the 'minimum' or 'smallest' value in the set
86+
public func min() -> T! {
87+
return count == 0 ? nil : internalSet[0]
88+
}
89+
90+
// returns the k largest element in the set, if k is in the range [1, count]
91+
// returns nil otherwise
92+
public func kLargest(k: Int) -> T! {
93+
return k > count || k <= 0 ? nil : internalSet[count - k]
94+
}
95+
96+
// returns the k smallest element in the set, if k is in the range [1, count]
97+
// returns nil otherwise
98+
public func kSmallest(k: Int) -> T! {
99+
return k > count || k <= 0 ? nil : internalSet[k - 1]
100+
}
101+
}

0 commit comments

Comments
 (0)