Skip to content

Commit 3e301e8

Browse files
author
Taras Nikulin
committed
Merge remote-tracking branch 'raywenderlich/master' into dijkstra
2 parents 9a8467f + f1d25e3 commit 3e301e8

File tree

151 files changed

+2400
-1387
lines changed

Some content is hidden

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

151 files changed

+2400
-1387
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 :]"

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

Lines changed: 5 additions & 23 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? {
@@ -385,11 +375,7 @@ extension TreeNode: CustomDebugStringConvertible {
385375

386376
extension AVLTree: CustomDebugStringConvertible {
387377
public var debugDescription: String {
388-
if let root = root {
389-
return root.debugDescription
390-
} else {
391-
return "[]"
392-
}
378+
return root?.debugDescription ?? "[]"
393379
}
394380
}
395381

@@ -409,10 +395,6 @@ extension TreeNode: CustomStringConvertible {
409395

410396
extension AVLTree: CustomStringConvertible {
411397
public var description: String {
412-
if let root = root {
413-
return root.description
414-
} else {
415-
return "[]"
416-
}
398+
return root?.description ?? "[]"
417399
}
418400
}

AVL Tree/AVLTree.swift

Lines changed: 5 additions & 23 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? {
@@ -385,11 +375,7 @@ extension TreeNode: CustomDebugStringConvertible {
385375

386376
extension AVLTree: CustomDebugStringConvertible {
387377
public var debugDescription: String {
388-
if let root = root {
389-
return root.debugDescription
390-
} else {
391-
return "[]"
392-
}
378+
return root?.debugDescription ?? "[]"
393379
}
394380
}
395381

@@ -409,10 +395,6 @@ extension TreeNode: CustomStringConvertible {
409395

410396
extension AVLTree: CustomStringConvertible {
411397
public var description: String {
412-
if let root = root {
413-
return root.description
414-
} else {
415-
return "[]"
416-
}
398+
return root?.description ?? "[]"
417399
}
418400
}

All-Pairs Shortest Paths/APSP/APSP/FloydWarshall.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public struct FloydWarshallResult<T>: APSPResult where T: Hashable {
175175
public func path(fromVertex from: Vertex<T>, toVertex to: Vertex<T>, inGraph graph: AbstractGraph<T>) -> [T]? {
176176

177177
if let path = recursePathFrom(fromVertex: from, toVertex: to, path: [ to ], inGraph: graph) {
178-
let pathValues = path.map() { vertex in
178+
let pathValues = path.map { vertex in
179179
vertex.data
180180
}
181181
return pathValues

Array2D/Array2D.playground/Contents.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ public struct Array2D<T> {
77
public let columns: Int
88
public let rows: Int
99
fileprivate var array: [T]
10-
10+
1111
public init(columns: Int, rows: Int, initialValue: T) {
1212
self.columns = columns
1313
self.rows = rows
1414
array = .init(repeating: initialValue, count: rows*columns)
1515
}
16-
16+
1717
public subscript(column: Int, row: Int) -> T {
1818
get {
1919
precondition(column < columns, "Column \(column) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")

Array2D/Array2D.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ public struct Array2D<T> {
77
public let columns: Int
88
public let rows: Int
99
fileprivate var array: [T]
10-
10+
1111
public init(columns: Int, rows: Int, initialValue: T) {
1212
self.columns = columns
1313
self.rows = rows
1414
array = .init(repeating: initialValue, count: rows*columns)
1515
}
16-
16+
1717
public subscript(column: Int, row: Int) -> T {
1818
get {
1919
precondition(column < columns, "Column \(column) Index is out of range. Array<T>(columns: \(columns), rows:\(rows))")

B-Tree/BTree.playground/Contents.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ bTree[3]
1414

1515
bTree.remove(2)
1616

17-
bTree.traverseKeysInOrder {
18-
key in
17+
bTree.traverseKeysInOrder { key in
1918
print(key)
2019
}
2120

0 commit comments

Comments
 (0)