Skip to content

Commit 0fdd98c

Browse files
authored
Merge pull request kodecocodes#1 from raywenderlich/master
Refreshing since Fork
2 parents 663f902 + 898db6f commit 0fdd98c

File tree

764 files changed

+196362
-9483
lines changed

Some content is hidden

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

764 files changed

+196362
-9483
lines changed

.github/CONTRIBUTING.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
Our repo is all about learning. The `README` file is the cake, and the sample code is the cherry on top. A good contribution has succinct explanations supported by diagrams. Code is best introduced in chunks, weaved into the explanations where relevant.
8+
9+
> When choosing between brievity and performance, err to the side of brievity as long as the time complexity of the particular implementation is the same. You can make a note afterwards suggesting a more performant way of doing things.
10+
11+
**API Design Guidelines**
12+
13+
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.
14+
15+
**Swift Language Guidelines**
16+
17+
We follow the following Swift [style guide](https://github.com/raywenderlich/swift-style-guide).
18+
19+
## Contribution Categories
20+
21+
### Refinement
22+
23+
Unit tests. Fixes for typos. No contribution is too small. :-)
24+
25+
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.
26+
27+
### New Contributions
28+
29+
Before writing about something new, you should do 2 things:
30+
31+
1. Check the main page for existing implementations
32+
2. Check the [pull requests](https://github.com/raywenderlich/swift-algorithm-club/pulls) for "claimed" topics. More info on that below.
33+
34+
If what you have in mind is a new addition, please follow this process when submitting your contribution:
35+
36+
1. Create a pull request to "claim" an algorithm or data structure. This is to avoid having multiple people working on the same thing.
37+
2. Use this [style guide](https://github.com/raywenderlich/swift-style-guide) for writing code (more or less).
38+
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.
39+
4. Include your name in the explanation, something like *Written by Your Name* at the end of the document.
40+
5. Add a playground and/or unit tests.
41+
42+
For the unit tests:
43+
44+
- 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:
45+
46+
```
47+
- xctool test -project ./Algorithm/Tests/Tests.xcodeproj -scheme Tests
48+
```
49+
50+
- Configure the Test project's scheme to run on Travis-CI:
51+
- Open **Product -> Scheme -> Manage Schemes...**
52+
- Uncheck **Autocreate schemes**
53+
- Check **Shared**
54+
55+
![Screenshot of scheme settings](../Images/scheme-settings-for-travis.png)
56+
57+
## Want to chat?
58+
59+
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
### Brief Intro
2+
3+
<!-- Describe the issue briefly.-->
4+
5+
### More Details
6+
7+
<!-- 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: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,54 @@
11
language: objective-c
2-
osx_image: xcode8
2+
osx_image: xcode9
33
# sudo: false
44

55
install:
66

7+
- gem install xcpretty-travis-formatter
78
# - ./install_swiftlint.sh
89

910
script:
1011

11-
# - xcodebuild test -project ./All-Pairs\ Shortest\ Paths/APSP/APSP.xcodeproj -scheme APSPTests
12-
- xcodebuild test -project ./Array2D/Tests/Tests.xcodeproj -scheme Tests
13-
- xcodebuild test -project ./AVL\ Tree/Tests/Tests.xcodeproj -scheme Tests
14-
- xcodebuild test -project ./Binary\ Search/Tests/Tests.xcodeproj -scheme Tests
15-
# - xcodebuild test -project ./Binary\ Search\ Tree/Solution\ 1/Tests/Tests.xcodeproj -scheme Tests
16-
- 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
20-
- 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
26-
- 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 ./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 ./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
36-
- 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 ./Stack/Tests/Tests.xcodeproj -scheme Tests
41-
# - xcodebuild test -project ./Topological\ Sort/Tests/Tests.xcodeproj -scheme Tests
12+
- xcodebuild test -project ./Array2D/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
13+
- xcodebuild test -project ./All-Pairs\ Shortest\ Paths/APSP/APSP.xcodeproj -scheme APSPTests | xcpretty -f `xcpretty-travis-formatter`
14+
- xcodebuild test -project ./AVL\ Tree/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
15+
- xcodebuild test -project ./Binary\ Search/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
16+
- xcodebuild test -project ./Boyer-Moore-Horspool/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
17+
- xcodebuild test -project ./Binary\ Search\ Tree/Solution\ 1/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
18+
- xcodebuild test -project ./Bloom\ Filter/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
19+
- xcodebuild test -project ./Bounded\ Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
20+
- xcodebuild test -project ./Breadth-First\ Search/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
21+
- xcodebuild test -project ./Bucket\ Sort/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
22+
- xcodebuild test -project ./B-Tree/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
23+
- xcodebuild test -project ./Comb\ Sort/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
24+
- xcodebuild test -project ./Convex\ Hull/Convex\ Hull.xcodeproj -scheme Tests -destination 'platform=iOS Simulator,name=iPhone 7,OS=11.0' | xcpretty -f `xcpretty-travis-formatter`
25+
- xcodebuild test -project ./Counting\ Sort/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
26+
- xcodebuild test -project ./Depth-First\ Search/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
27+
- xcodebuild test -project ./Graph/Graph.xcodeproj -scheme GraphTests | xcpretty -f `xcpretty-travis-formatter`
28+
- xcodebuild test -project ./Heap/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
29+
- xcodebuild test -project ./Heap\ Sort/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
30+
- xcodebuild test -project ./Insertion\ Sort/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
31+
- xcodebuild test -project ./K-Means/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
32+
- xcodebuild test -project ./Linked\ List/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
33+
- xcodebuild test -project ./Singly\ Linked\ List/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
34+
- xcodebuild test -project ./Longest\ Common\ Subsequence/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
35+
- xcodebuild test -project ./Minimum\ Spanning\ Tree\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
36+
- xcodebuild test -project ./Priority\ Queue/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
37+
- xcodebuild test -project ./Queue/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
38+
- xcodebuild test -project ./Quicksort/Tests/Tests-Quicksort.xcodeproj -scheme Test-Quicksort | xcpretty -f `xcpretty-travis-formatter`
39+
- xcodebuild test -project ./Radix\ Sort/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
40+
- xcodebuild test -project ./Rootish\ Array\ Stack/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
41+
- xcodebuild test -project ./Select\ Minimum\ Maximum/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
42+
- xcodebuild test -project ./Selection\ Sort/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
43+
- xcodebuild test -project ./Shell\ Sort/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
44+
- xcodebuild test -project ./Shortest\ Path\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
45+
- xcodebuild test -project ./Single-Source\ Shortest\ Paths\ \(Weighted\)/SSSP.xcodeproj -scheme SSSPTests | xcpretty -f `xcpretty-travis-formatter`
46+
- xcodebuild test -project ./Stack/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
47+
- xcodebuild test -project ./Topological\ Sort/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
48+
- xcodebuild test -project ./Treap/Treap/Treap.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
49+
- xcodebuild test -project ./Palindromes/Test/Test.xcodeproj -scheme Test | xcpretty -f `xcpretty-travis-formatter`
50+
- xcodebuild test -project ./Ternary\ Search\ Tree/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
51+
52+
after_success:
53+
54+
- if [[ "$TRAVIS_BRANCH" == "master" ]]; then ./gfm-render.sh; fi
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
extension Collection where Element: Equatable {
3+
4+
/// In a sorted collection, replaces the given index with a successor mapping to a unique element.
5+
///
6+
/// - Parameter index: A valid index of the collection. `index` must be less than `endIndex`
7+
func formUniqueIndex(after index: inout Index) {
8+
var prev = index
9+
repeat {
10+
prev = index
11+
formIndex(after: &index)
12+
} while index < endIndex && self[prev] == self[index]
13+
}
14+
}
15+
16+
extension BidirectionalCollection where Element: Equatable {
17+
18+
/// In a sorted collection, replaces the given index with a predecessor that maps to a unique element.
19+
///
20+
/// - Parameter index: A valid index of the collection. `index` must be greater than `startIndex`.
21+
func formUniqueIndex(before index: inout Index) {
22+
var prev = index
23+
repeat {
24+
prev = index
25+
formIndex(before: &index)
26+
} while index > startIndex && self[prev] == self[index]
27+
}
28+
}
29+
30+
func threeSum<T: BidirectionalCollection>(_ collection: T, target: T.Element) -> [[T.Element]] where T.Element: Numeric & Comparable {
31+
let sorted = collection.sorted()
32+
var ret: [[T.Element]] = []
33+
var l = sorted.startIndex
34+
35+
ThreeSum: while l < sorted.endIndex { defer { sorted.formUniqueIndex(after: &l) }
36+
var m = sorted.index(after: l)
37+
var r = sorted.index(before: sorted.endIndex)
38+
39+
TwoSum: while m < r && r < sorted.endIndex {
40+
let sum = sorted[l] + sorted[m] + sorted[r]
41+
if sum == target {
42+
ret.append([sorted[l], sorted[m], sorted[r]])
43+
sorted.formUniqueIndex(after: &m)
44+
sorted.formUniqueIndex(before: &r)
45+
} else if sum < target {
46+
sorted.formUniqueIndex(after: &m)
47+
} else {
48+
sorted.formUniqueIndex(before: &r)
49+
}
50+
}
51+
}
52+
53+
return ret
54+
}
55+
56+
// Answer: [[-1, 0, 1], [-1, -1, 2]]
57+
threeSum([-1, 0, 1, 2, -1, -4], target: 0)
58+
59+
// Answer: [[-1, -1, 2], [-1, 0, 1]]
60+
threeSum([-1, -1, -1, -1, 2, 1, -4, 0], target: 0)
61+
62+
// Answer: [[-1, -1, 2]]
63+
threeSum([-1, -1, -1, -1, -1, -1, 2], target: 0)
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
extension Collection where Element: Equatable {
2+
3+
/// In a sorted collection, replaces the given index with a successor mapping to a unique element.
4+
///
5+
/// - Parameter index: A valid index of the collection. `index` must be less than `endIndex`
6+
func formUniqueIndex(after index: inout Index) {
7+
var prev = index
8+
repeat {
9+
prev = index
10+
formIndex(after: &index)
11+
} while index < endIndex && self[prev] == self[index]
12+
}
13+
}
14+
15+
extension BidirectionalCollection where Element: Equatable {
16+
17+
/// In a sorted collection, replaces the given index with a predecessor that maps to a unique element.
18+
///
19+
/// - Parameter index: A valid index of the collection. `index` must be greater than `startIndex`.
20+
func formUniqueIndex(before index: inout Index) {
21+
var prev = index
22+
repeat {
23+
prev = index
24+
formIndex(before: &index)
25+
} while index > startIndex && self[prev] == self[index]
26+
}
27+
}
28+
29+
func fourSum<T: BidirectionalCollection>(_ collection: T, target: T.Element) -> [[T.Element]] where T.Element: Numeric & Comparable {
30+
let sorted = collection.sorted()
31+
var ret: [[T.Element]] = []
32+
33+
var l = sorted.startIndex
34+
FourSum: while l < sorted.endIndex { defer { sorted.formUniqueIndex(after: &l) }
35+
var ml = sorted.index(after: l)
36+
37+
ThreeSum: while ml < sorted.endIndex { defer { sorted.formUniqueIndex(after: &ml) }
38+
var mr = sorted.index(after: ml)
39+
var r = sorted.index(before: sorted.endIndex)
40+
41+
TwoSum: while mr < r && r < sorted.endIndex {
42+
let sum = sorted[l] + sorted[ml] + sorted[mr] + sorted[r]
43+
if sum == target {
44+
ret.append([sorted[l], sorted[ml], sorted[mr], sorted[r]])
45+
sorted.formUniqueIndex(after: &mr)
46+
sorted.formUniqueIndex(before: &r)
47+
} else if sum < target {
48+
sorted.formUniqueIndex(after: &mr)
49+
} else {
50+
sorted.formUniqueIndex(before: &r)
51+
}
52+
}
53+
}
54+
}
55+
return ret
56+
}
57+
58+
// answer: [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]
59+
fourSum([1, 0, -1, 0, -2, 2], target: 0)

0 commit comments

Comments
 (0)