Skip to content

Commit 71d2c9d

Browse files
authored
Merge branch 'master' into master
2 parents 689efb3 + 1e026d7 commit 71d2c9d

File tree

589 files changed

+185821
-6589
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

589 files changed

+185821
-6589
lines changed

.github/CONTRIBUTING.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Contributing Guidelines
2+
3+
Want to help out with the Swift Algorithm Club? Great! While we don't have strict templates on the format of each contribution, we do have a few guidelines that should be kept in mind:
4+
5+
**Readability**
6+
7+
The `README` file is the cake, and the sample code is the cherry on top. Readablity is really important for us. A good contribution has succinct explanations supported by diagrams. Code is best introduced in chunks, weaved into the explanations where relevant.
8+
9+
**API Design Guidelines**
10+
11+
A good contribution abides to the [Swift API Guidelines](https://swift.org/documentation/api-design-guidelines/). We review the pull requests with this in mind.
12+
13+
**Swift Language Guidelines**
14+
15+
We follow the following Swift [style guide](https://github.com/raywenderlich/swift-style-guide).
16+
17+
## Contribution Categories
18+
19+
### Refinement
20+
21+
Unit tests. Fixes for typos. No contribution is too small. :-)
22+
23+
The repository has over 100 different data structures and algorithms. We're always interested in improvements to existing implementations and better explanations. Suggestions for making the code more Swift-like or to make it fit better with the standard library is most welcomed.
24+
25+
### New Contributions
26+
27+
Before writing about something new, you should do 2 things:
28+
29+
1. Check the main page for existing implementations
30+
2. Check the [pull requests](https://github.com/raywenderlich/swift-algorithm-club/pulls) for "claimed" topics. More info on that below.
31+
32+
If what you have in mind is a new addition, please follow this process when submitting your contribution:
33+
34+
1. Create a pull request to "claim" an algorithm or data structure. This is to avoid having multiple people working on the same thing.
35+
2. Use this [style guide](https://github.com/raywenderlich/swift-style-guide) for writing code (more or less).
36+
3. Write an explanation of how the algorithm works. Include **plenty of examples** for readers to follow along. Pictures are good. Take a look at [the explanation of quicksort](../Quicksort/) to get an idea.
37+
4. Include your name in the explanation, something like *Written by Your Name* at the end of the document.
38+
5. Add a playground and/or unit tests.
39+
40+
For the unit tests:
41+
42+
- Add the unit test project to `.travis.yml` so they will be run on [Travis-CI](https://travis-ci.org/raywenderlich/swift-algorithm-club). Add a line to `.travis.yml` like this:
43+
44+
```
45+
- xctool test -project ./Algorithm/Tests/Tests.xcodeproj -scheme Tests
46+
```
47+
48+
- Configure the Test project's scheme to run on Travis-CI:
49+
- Open **Product -> Scheme -> Manage Schemes...**
50+
- Uncheck **Autocreate schemes**
51+
- Check **Shared**
52+
53+
![Screenshot of scheme settings](../Images/scheme-settings-for-travis.png)
54+
55+
## Want to chat?
56+
57+
This isn't just a repo with a bunch of code... If you want to learn more about how an algorithm works or want to discuss better ways of solving problems, then open a [Github issue](https://github.com/raywenderlich/swift-algorithm-club/issues) and we'll talk!

.github/ISSUE_TEMPLATE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### Category
2+
3+
- [ ] Bug
4+
- [ ] Feature Request
5+
- [ ] Question
6+
7+
### Brief Intro
8+
9+
<!-- Describe the issue briefly.-->
10+
11+
### More Details
12+
13+
<!-- If necessary, start off with the code snippet. Put the code in between ``` to format them into code blocks.-->

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!-- Thanks for contributing to the SAC! Before you submit your pull request, please make sure to check the following boxes by putting an x in the [ ] -->
2+
3+
### Checklist
4+
5+
- [ ] I've looked at the [contribution guidelines](https://github.com/raywenderlich/swift-algorithm-club/blob/master/.github/CONTRIBUTING.md).
6+
- [ ] This pull request is complete and ready for review.
7+
8+
### Description
9+
10+
<!-- In a short paragraph, describe the PR -->
11+

.swiftlint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ disabled_rules:
1717
custom_rules:
1818
smiley_face:
1919
name: "Smiley Face"
20-
regex: "(\:\))"
21-
match_kinds:
20+
regex: '( :\))'
21+
match_kinds:
2222
- comment
2323
- string
2424
message: "A closing parenthesis smiley :) creates a half-hearted smile, and thus is not preferred. Use :]"

.travis.yml

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: objective-c
2-
osx_image: xcode8
2+
osx_image: xcode9
33
# sudo: false
44

55
install:
@@ -8,35 +8,40 @@ install:
88

99
script:
1010

11-
# - xcodebuild test -project ./All-Pairs\ Shortest\ Paths/APSP/APSP.xcodeproj -scheme APSPTests
11+
- xcodebuild test -project ./All-Pairs\ Shortest\ Paths/APSP/APSP.xcodeproj -scheme APSPTests
1212
- xcodebuild test -project ./Array2D/Tests/Tests.xcodeproj -scheme Tests
1313
- xcodebuild test -project ./AVL\ Tree/Tests/Tests.xcodeproj -scheme Tests
1414
- xcodebuild test -project ./Binary\ Search/Tests/Tests.xcodeproj -scheme Tests
15-
# - xcodebuild test -project ./Binary\ Search\ Tree/Solution\ 1/Tests/Tests.xcodeproj -scheme Tests
15+
- xcodebuild test -project ./Boyer-Moore/Tests/Tests.xcodeproj -scheme Tests
16+
- xcodebuild test -project ./Binary\ Search\ Tree/Solution\ 1/Tests/Tests.xcodeproj -scheme Tests
1617
- xcodebuild test -project ./Bloom\ Filter/Tests/Tests.xcodeproj -scheme Tests
17-
# - xcodebuild test -project ./Bounded\ Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
18-
# - xcodebuild test -project ./Breadth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
19-
# - xcodebuild test -project ./Bucket\ Sort/Tests/Tests.xcodeproj -scheme Tests
18+
- xcodebuild test -project ./Bounded\ Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
19+
- xcodebuild test -project ./Breadth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
20+
- xcodebuild test -project ./Bucket\ Sort/Tests/Tests.xcodeproj -scheme Tests
2021
- xcodebuild test -project ./B-Tree/Tests/Tests.xcodeproj -scheme Tests
21-
# - xcodebuild test -project ./Counting\ Sort/Tests/Tests.xcodeproj -scheme Tests
22-
# - xcodebuild test -project ./Depth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
23-
# - xcodebuild test -project ./Graph/Graph.xcodeproj -scheme GraphTests
24-
# - xcodebuild test -project ./Heap/Tests/Tests.xcodeproj -scheme Tests
25-
# - xcodebuild test -project ./Heap\ Sort/Tests/Tests.xcodeproj -scheme Tests
22+
- xcodebuild test -project ./Comb\ Sort/Tests/Tests.xcodeproj -scheme Tests
23+
- xcodebuild test -project ./Counting\ Sort/Tests/Tests.xcodeproj -scheme Tests
24+
- xcodebuild test -project ./Depth-First\ Search/Tests/Tests.xcodeproj -scheme Tests
25+
- xcodebuild test -project ./Graph/Graph.xcodeproj -scheme GraphTests
26+
- xcodebuild test -project ./Heap/Tests/Tests.xcodeproj -scheme Tests
27+
- xcodebuild test -project ./Heap\ Sort/Tests/Tests.xcodeproj -scheme Tests
2628
- xcodebuild test -project ./Insertion\ Sort/Tests/Tests.xcodeproj -scheme Tests
27-
# - xcodebuild test -project ./K-Means/Tests/Tests.xcodeproj -scheme Tests
28-
# - xcodebuild test -project ./Linked\ List/Tests/Tests.xcodeproj -scheme Tests
29+
- xcodebuild test -project ./K-Means/Tests/Tests.xcodeproj -scheme Tests
30+
- xcodebuild test -project ./Linked\ List/Tests/Tests.xcodeproj -scheme Tests
2931
- xcodebuild test -project ./Longest\ Common\ Subsequence/Tests/Tests.xcodeproj -scheme Tests
30-
# - xcodebuild test -project ./Minimum\ Spanning\ Tree\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
31-
# - xcodebuild test -project ./Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
32+
- xcodebuild test -project ./Minimum\ Spanning\ Tree\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
33+
- xcodebuild test -project ./Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests
3234
- xcodebuild test -project ./Queue/Tests/Tests.xcodeproj -scheme Tests
33-
# - xcodebuild test -project ./Quicksort/Tests/Tests.xcodeproj -scheme Tests
34-
# - xcodebuild test -project ./Run-Length\ Encoding/Tests/Tests.xcodeproj -scheme Tests
35-
# - xcodebuild test -project ./Select\ Minimum\ Maximum/Tests/Tests.xcodeproj -scheme Tests
35+
- xcodebuild test -project ./Quicksort/Tests/Tests-Quicksort.xcodeproj -scheme Tests-Quicksort
36+
- xcodebuild test -project ./Radix\ Sort/Tests/Tests.xcodeproj -scheme Tests
37+
- xcodebuild test -project ./Rootish\ Array\ Stack/Tests/Tests.xcodeproj -scheme Tests
38+
- xcodebuild test -project ./Select\ Minimum\ Maximum/Tests/Tests.xcodeproj -scheme Tests
3639
- xcodebuild test -project ./Selection\ Sort/Tests/Tests.xcodeproj -scheme Tests
37-
# - xcodebuild test -project ./Shell\ Sort/Tests/Tests.xcodeproj -scheme Tests
38-
# - xcodebuild test -project ./Shortest\ Path\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
39-
# - xcodebuild test -project ./Single-Source\ Shortest\ Paths\ \(Weighted\)/SSSP.xcodeproj -scheme SSSPTests
40+
- xcodebuild test -project ./Shell\ Sort/Tests/Tests.xcodeproj -scheme Tests
41+
- xcodebuild test -project ./Shortest\ Path\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
42+
- xcodebuild test -project ./Single-Source\ Shortest\ Paths\ \(Weighted\)/SSSP.xcodeproj -scheme SSSPTests
4043
- xcodebuild test -project ./Stack/Tests/Tests.xcodeproj -scheme Tests
4144
- xcodebuild test -project ./Topological\ Sort/Tests/Tests.xcodeproj -scheme Tests
4245
- xcodebuild test -project ./Treap/Treap/Treap.xcodeproj -scheme Tests
46+
- xcodebuild test -project ./Palindromes/Test/Test.xcodeproj -scheme Test
47+
- xcodebuild test -project ./Ternary\ Search\ Tree/Tests/Tests.xcodeproj -scheme Tests

AVL Tree/AVLTree.playground/Contents.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
//: Playground - noun: a place where people can play
22

3+
// last checked with Xcode 9.0b4
4+
#if swift(>=4.0)
5+
print("Hello, Swift 4!")
6+
#endif
7+
38
let tree = AVLTree<Int, String>()
49

510
tree.insert(key: 5, payload: "five")

AVL Tree/AVLTree.playground/Sources/AVLTree.swift

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,11 @@ open class AVLTree<Key: Comparable, Payload> {
9999

100100
extension TreeNode {
101101
public func minimum() -> TreeNode? {
102-
if let leftChild = self.leftChild {
103-
return leftChild.minimum()
104-
}
105-
return self
102+
return leftChild?.minimum() ?? self
106103
}
107104

108105
public func maximum() -> TreeNode? {
109-
if let rightChild = self.rightChild {
110-
return rightChild.maximum()
111-
}
112-
return self
106+
return rightChild?.maximum() ?? self
113107
}
114108
}
115109

@@ -120,11 +114,7 @@ extension AVLTree {
120114
}
121115

122116
public func search(input: Key) -> Payload? {
123-
if let result = search(key: input, node: root) {
124-
return result.payload
125-
} else {
126-
return nil
127-
}
117+
return search(key: input, node: root)?.payload
128118
}
129119

130120
fileprivate func search(key: Key, node: Node?) -> Node? {
@@ -351,11 +341,11 @@ extension AVLTree {
351341
}
352342
} else {
353343
// Handle stem cases
354-
if let replacement = node.leftChild?.maximum() , replacement !== node {
344+
if let replacement = node.leftChild?.maximum(), replacement !== node {
355345
node.key = replacement.key
356346
node.payload = replacement.payload
357347
delete(node: replacement)
358-
} else if let replacement = node.rightChild?.minimum() , replacement !== node {
348+
} else if let replacement = node.rightChild?.minimum(), replacement !== node {
359349
node.key = replacement.key
360350
node.payload = replacement.payload
361351
delete(node: replacement)
@@ -364,7 +354,6 @@ extension AVLTree {
364354
}
365355
}
366356

367-
368357
// MARK: - Debugging
369358

370359
extension TreeNode: CustomDebugStringConvertible {
@@ -385,11 +374,7 @@ extension TreeNode: CustomDebugStringConvertible {
385374

386375
extension AVLTree: CustomDebugStringConvertible {
387376
public var debugDescription: String {
388-
if let root = root {
389-
return root.debugDescription
390-
} else {
391-
return "[]"
392-
}
377+
return root?.debugDescription ?? "[]"
393378
}
394379
}
395380

@@ -409,10 +394,6 @@ extension TreeNode: CustomStringConvertible {
409394

410395
extension AVLTree: CustomStringConvertible {
411396
public var description: String {
412-
if let root = root {
413-
return root.description
414-
} else {
415-
return "[]"
416-
}
397+
return root?.description ?? "[]"
417398
}
418399
}

AVL Tree/AVLTree.swift

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class TreeNode<Key: Comparable, Payload> {
2929
internal var leftChild: Node?
3030
internal var rightChild: Node?
3131
fileprivate var height: Int
32-
weak fileprivate var parent: Node?
32+
fileprivate weak var parent: Node?
3333

3434
public init(key: Key, payload: Payload?, leftChild: Node?, rightChild: Node?, parent: Node?, height: Int) {
3535
self.key = key
@@ -99,17 +99,11 @@ open class AVLTree<Key: Comparable, Payload> {
9999

100100
extension TreeNode {
101101
public func minimum() -> TreeNode? {
102-
if let leftChild = self.leftChild {
103-
return leftChild.minimum()
104-
}
105-
return self
102+
return leftChild?.minimum() ?? self
106103
}
107104

108105
public func maximum() -> TreeNode? {
109-
if let rightChild = self.rightChild {
110-
return rightChild.maximum()
111-
}
112-
return self
106+
return rightChild?.maximum() ?? self
113107
}
114108
}
115109

@@ -120,11 +114,7 @@ extension AVLTree {
120114
}
121115

122116
public func search(input: Key) -> Payload? {
123-
if let result = search(key: input, node: root) {
124-
return result.payload
125-
} else {
126-
return nil
127-
}
117+
return search(key: input, node: root)?.payload
128118
}
129119

130120
fileprivate func search(key: Key, node: Node?) -> Node? {
@@ -351,11 +341,11 @@ extension AVLTree {
351341
}
352342
} else {
353343
// Handle stem cases
354-
if let replacement = node.leftChild?.maximum() , replacement !== node {
344+
if let replacement = node.leftChild?.maximum(), replacement !== node {
355345
node.key = replacement.key
356346
node.payload = replacement.payload
357347
delete(node: replacement)
358-
} else if let replacement = node.rightChild?.minimum() , replacement !== node {
348+
} else if let replacement = node.rightChild?.minimum(), replacement !== node {
359349
node.key = replacement.key
360350
node.payload = replacement.payload
361351
delete(node: replacement)
@@ -364,7 +354,6 @@ extension AVLTree {
364354
}
365355
}
366356

367-
368357
// MARK: - Debugging
369358

370359
extension TreeNode: CustomDebugStringConvertible {
@@ -385,11 +374,7 @@ extension TreeNode: CustomDebugStringConvertible {
385374

386375
extension AVLTree: CustomDebugStringConvertible {
387376
public var debugDescription: String {
388-
if let root = root {
389-
return root.debugDescription
390-
} else {
391-
return "[]"
392-
}
377+
return root?.debugDescription ?? "[]"
393378
}
394379
}
395380

@@ -409,10 +394,6 @@ extension TreeNode: CustomStringConvertible {
409394

410395
extension AVLTree: CustomStringConvertible {
411396
public var description: String {
412-
if let root = root {
413-
return root.description
414-
} else {
415-
return "[]"
416-
}
397+
return root?.description ?? "[]"
417398
}
418399
}

AVL Tree/Images/RotationStep0.jpg

21.7 KB
Loading

AVL Tree/Images/RotationStep1.jpg

12.8 KB
Loading

0 commit comments

Comments
 (0)