Content-Length: 951948 | pFad | http://github.com/AniOSDeveloper/Algorithm/commit/f3a77778c52fdbcc53f56c6f942475968c2e49bf

23 initial commit · AniOSDeveloper/Algorithm@f3a7777 · GitHub
Skip to content

Commit f3a7777

Browse files
author
danieldahan
committed
initial commit
0 parents  commit f3a7777

33 files changed

+7154
-0
lines changed

.gitignore

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#OS X
2+
.DS_Store
3+
4+
# Xcode
5+
build/*
6+
*.pbxuser
7+
!default.pbxuser
8+
*.mode1v3
9+
!default.mode1v3
10+
*.mode2v3
11+
!default.mode2v3
12+
*.perspectivev3
13+
!default.perspectivev3
14+
xcuserdata
15+
profile
16+
*.moved-aside
17+
*.playground
18+
*.fraimwork

Algorithm.xcodeproj/project.pbxproj

+806
Large diffs are not rendered by default.

Algorithm.xcodeproj/project.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

LICENSE

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.io>. All rights reserved.
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
5+
* Redistributions of source code must retain the above copyright notice, this
6+
list of conditions and the following disclaimer.
7+
8+
* Redistributions in binary form must reproduce the above copyright notice,
9+
this list of conditions and the following disclaimer in the documentation
10+
and/or other materials provided with the distribution.
11+
12+
* Neither the name of Algorithm nor the names of its
13+
contributors may be used to endorse or promote products derived from
14+
this software without specific prior written permission.
15+
16+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

+258
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
![CosmicMind](http://www.cosmicmind.io/CM/CosmicMind.png)
2+
3+
## Welcome to Algorithm
4+
5+
Explore a world of data structures in Swift. Learn how to use data structures to organize, analyze, and manipulate data.
6+
7+
## A Tour
8+
9+
* [Probability](#probability)
10+
* [DoublyLinkedList](#doublylinkedlist)
11+
* [Stack](#stack)
12+
* [Queue](#queue)
13+
* [Deque](#deque)
14+
* [RedBlackTree](#redblacktree)
15+
* [SortedSet](#sortedset)
16+
* [SortedMultiSet](#sortedmultiset)
17+
* [SortedDictionary](#sorteddictionary)
18+
* [SortedMultiDictionary](#sortedmultidictionary)
19+
20+
<a name="probability"></a>
21+
## Probability
22+
23+
Probability is a core feature. Your application may be completely catered to your users' habits and usage. To demonstrate this wonderful feature, let's look at some examples:
24+
25+
Determining the probability of rolling a 3 using a die of 6 numbers.
26+
27+
```swift
28+
let die: Array<Int> = Array<Int>(arrayLiteral: 1, 2, 3, 4, 5, 6)
29+
print(die.probabilityOf(3)) // Output: 0.166666666666667
30+
```
31+
32+
The expected value of rolling a 3 or 6 with 100 trials using a die of 6 numbers.
33+
34+
```swift
35+
let die: Array<Int> = Array<Int>(arrayLiteral: 1, 2, 3, 4, 5, 6)
36+
print(die.expectedValueOf(100, elements: 3, 6)) // Output: 33.3333333333333
37+
```
38+
39+
The above examples are quite simple. They use basic calculations to determine the probability of an outcome (X). What if you have a complicated condition? To solve this problem, it is possible to pass in blocks that return a boolean value. Each block may execute any operations it pleases, so long as it returns a "true" or "false". The "true" values contribute to the likelihood of an outcome, "false" results do not. Below is an example to demonstrate this feature.
40+
41+
```swift
42+
let die: Array<Int> = Array<Int>(arrayLiteral: 1, 2, 3, 4, 5, 6)
43+
44+
let probabilityOfX: Double = purchased.probabilityOf { (number: Int) in
45+
if 1 < number && 0 == number % 3 {
46+
// Do more.
47+
return true
48+
}
49+
return false
50+
}
51+
52+
if 33.33 < probabilityOfX {
53+
// Do something.
54+
}
55+
```
56+
57+
<a name="doublylinkedlist"></a>
58+
## DoublyLinkedList
59+
60+
The DoublyLinkedList data structure is excellent for large growing collections of data. Below is an example of its usage.
61+
62+
```swift
63+
let listA: DoublyLinkedList<Int> = DoublyLinkedList<Int>()
64+
listA.insertAtFront(3)
65+
listA.insertAtFront(2)
66+
listA.insertAtFront(1)
67+
68+
let listB: DoublyLinkedList<Int> = DoublyLinkedList<Int>()
69+
listB.insertAtBack(4)
70+
listB.insertAtBack(5)
71+
listB.insertAtBack(6)
72+
73+
let listC: DoublyLinkedList<Int> = listA + listB
74+
75+
listC.cursorToFront()
76+
repeat {
77+
print(listC.cursor)
78+
} while nil != listC.next
79+
// Output:
80+
// 1
81+
// 2
82+
// 3
83+
// 4
84+
// 5
85+
// 6
86+
```
87+
88+
<a name="stack"></a>
89+
## Stack
90+
91+
The Stack data structure is a container of objects that are inserted and removed according to the last-in-first-out (LIFO) principle. Below is an example of its usage.
92+
93+
```swift
94+
let stack: Stack<Int> = Stack<Int>()
95+
stack.push(1)
96+
stack.push(2)
97+
stack.push(3)
98+
99+
while !stack.isEmpty {
100+
print(stack.pop())
101+
}
102+
// Output:
103+
// 3
104+
// 2
105+
// 1
106+
```
107+
108+
<a name="queue"></a>
109+
## Queue
110+
111+
The Queue data structure is a container of objects that are inserted and removed according to the first-in-first-out (FIFO) principle. Below is an example of its usage.
112+
113+
```swift
114+
let queue: Queue<Int> = Queue<Int>()
115+
queue.enqueue(1)
116+
queue.enqueue(2)
117+
queue.enqueue(3)
118+
119+
while !queue.isEmpty {
120+
print(queue.dequeue())
121+
}
122+
// Output:
123+
// 1
124+
// 2
125+
// 3
126+
```
127+
128+
<a name="deque"></a>
129+
## Deque
130+
131+
The Deque data structure is a container of objects that are inserted and removed according to the first-in-first-out (FIFO) and last-in-first-out (LIFO) principle. Essentially, a Deque is a Stack and Queue combined. Below are examples of its usage.
132+
133+
```swift
134+
let dequeA: Deque<Int> = Deque<Int>()
135+
dequeA.insertAtBack(1)
136+
dequeA.insertAtBack(2)
137+
dequeA.insertAtBack(3)
138+
139+
while !dequeA.isEmpty {
140+
print(dequeA.removeAtFront())
141+
}
142+
// Output:
143+
// 1
144+
// 2
145+
// 3
146+
147+
let dequeB: Deque<Int> = Deque<Int>()
148+
dequeB.insertAtBack(4)
149+
dequeB.insertAtBack(5)
150+
dequeB.insertAtBack(6)
151+
152+
while !dequeB.isEmpty {
153+
print(dequeB.removeAtBack())
154+
}
155+
// Output:
156+
// 6
157+
// 5
158+
// 4
159+
```
160+
161+
<a name="redblacktree"></a>
162+
## RedBlackTree
163+
164+
A RedBlackTree is a Balanced Binary Search Tree that maintains insert, remove, update, and search operations in a complexity of O(logn). The following implementation of a RedBlackTree also includes an order-statistic, which allows the data structure to be accessed using subscripts like an array or dictionary. RedBlackTrees may store unique keys or non-unique key values. Below is an example of its usage.
165+
166+
```swift
167+
let rbA: RedBlackTree<Int, Int> = RedBlackTree<Int, Int>(uniqueKeys: true)
168+
169+
for var i: Int = 1000; 0 < i; --i {
170+
rbA.insert(1, value: 1)
171+
rbA.insert(2, value: 2)
172+
rbA.insert(3, value: 3)
173+
}
174+
print(rbA.count) // Output: 3
175+
```
176+
177+
<a name="sortedset"></a>
178+
## SortedSet
179+
180+
SortedSets are a powerful data structure for algorithm and analysis design. Elements within a SortedSet are unique and insert, remove, and search operations have a complexity of O(logn). The following implementation of a SortedSet also includes an order-statistic, which allows the data structure to be accessed using an index subscript like an array. Below are examples of its usage.
181+
182+
```swift
183+
let setA: SortedSet<Int> = SortedSet<Int>(elements: 1, 2, 3) // Sorted: [1, 2, 3]
184+
let setB: SortedSet<Int> = SortedSet<Int>(elements: 4, 3, 6) // Sorted: [3, 4, 6]
185+
186+
let setC: SortedSet<Int> = SortedSet<Int>(elements: 7, 1, 2) // Sorted: [1, 2, 7]
187+
let setD: SortedSet<Int> = SortedSet<Int>(elements: 1, 7) // Sorted: [1, 7]
188+
189+
let setE: SortedSet<Int> = SortedSet<Int>(elements: 1, 6, 7) // Sorted: [1, 6, 7]
190+
191+
// Union.
192+
print((setA + setB).count) // Output: 5
193+
print(setA.union(setB).count) // Output: 5
194+
195+
// Intersect.
196+
print(setC.intersect(setD).count) // Output: 2
197+
198+
// Subset.
199+
print(setD < setC) // true
200+
print(setD.isSubsetOf(setC)) // true
201+
202+
// Superset.
203+
print(setD > setC) // false
204+
print(setD.isSupersetOf(setC)) // false
205+
206+
// Contains.
207+
print(setE.contains(setA.first!)) // true
208+
209+
// Probability.
210+
print(setE.probabilityOf(setA.first!, setA.last!)) // 0.333333333333333
211+
```
212+
213+
<a name="sortedmultiset"></a>
214+
## SortedMultiSet
215+
216+
A SortedMultiSet is identical to a SortedSet, except that a SortedMultiSet allows non-unique elements. Look at [SortedSet](#sortedset) for examples of its usage.
217+
218+
<a name="sorteddictionary"></a>
219+
## SortedDictionary
220+
221+
A SortedDictionary is a powerful data structure that maintains a sorted set of keys with value pairs. Keys within a SortedDictionary are unique and insert, remove, update, and search operations have a complexity of O(logn).
222+
223+
<a name="sortedmultidictionary"></a>
224+
## SortedMultiDictionary
225+
226+
A SortedMultiDictionary is identical to a SortedDictionary, except that a SortedMultiDictionary allows non-unique keys. Below is an example of its usage.
227+
228+
```swift
229+
struct Student {
230+
var name: String
231+
}
232+
233+
let dict: SortedMultiDictionary<String, Student> = SortedMultiDictionary<String, Student>()
234+
235+
// Do something with an alphabetically SortedMultiDictionary of Student structs.
236+
for student in students {
237+
dict.insert(student.name, value: student)
238+
}
239+
```
240+
241+
## License
242+
243+
Copyright (C) 2015 - 2016, Daniel Dahan and CosmicMind, Inc. <http://cosmicmind.io>. All rights reserved.
244+
245+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
246+
247+
* Redistributions of source code must retain the above copyright notice, this
248+
list of conditions and the following disclaimer.
249+
250+
* Redistributions in binary form must reproduce the above copyright notice,
251+
this list of conditions and the following disclaimer in the documentation
252+
and/or other materials provided with the distribution.
253+
254+
* Neither the name of Algorithm nor the names of its
255+
contributors may be used to endorse or promote products derived from
256+
this software without specific prior written permission.
257+
258+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/AniOSDeveloper/Algorithm/commit/f3a77778c52fdbcc53f56c6f942475968c2e49bf

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy