Skip to content

Commit d747f76

Browse files
authored
Merge pull request #1 from raywenderlich/master
update
2 parents 01a45e3 + 81bc070 commit d747f76

File tree

442 files changed

+13067
-4075
lines changed

Some content is hidden

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

442 files changed

+13067
-4075
lines changed

.github/CONTRIBUTING.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ Want to help out with the Swift Algorithm Club? Great! While we don't have stric
44

55
**Readability**
66

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.
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 brevity and performance, err to the side of brevity 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.
810

911
**API Design Guidelines**
1012

@@ -20,7 +22,7 @@ We follow the following Swift [style guide](https://github.com/raywenderlich/swi
2022

2123
Unit tests. Fixes for typos. No contribution is too small. :-)
2224

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.
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 are most welcome.
2426

2527
### New Contributions
2628

.github/ISSUE_TEMPLATE.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
### Category
2-
3-
- [ ] Bug
4-
- [ ] Feature Request
5-
- [ ] Question
6-
71
### Brief Intro
82

93
<!-- Describe the issue briefly.-->
104

115
### More Details
126

13-
<!-- If necessary, start off with the code snippet. Put the code in between ``` to format them into code blocks.-->
7+
<!-- If necessary, start off with the code snippet. Put the code in between ``` to format them into code blocks.-->

.travis.yml

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,52 @@ osx_image: xcode9
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 ./Boyer-Moore/Tests/Tests.xcodeproj -scheme Tests
16-
- xcodebuild test -project ./Binary\ Search\ Tree/Solution\ 1/Tests/Tests.xcodeproj -scheme Tests
17-
- xcodebuild test -project ./Bloom\ Filter/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
21-
- xcodebuild test -project ./B-Tree/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
28-
- xcodebuild test -project ./Insertion\ Sort/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
31-
- xcodebuild test -project ./Longest\ Common\ Subsequence/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
34-
- xcodebuild test -project ./Queue/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
39-
- xcodebuild test -project ./Selection\ Sort/Tests/Tests.xcodeproj -scheme Tests
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
43-
- xcodebuild test -project ./Stack/Tests/Tests.xcodeproj -scheme Tests
44-
- xcodebuild test -project ./Topological\ Sort/Tests/Tests.xcodeproj -scheme Tests
45-
- 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
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+
- xcodebuild test -project ./Karatsuba\ Multiplication/Tests/Tests.xcodeproj -scheme Tests | xcpretty -f `xcpretty-travis-formatter`
52+
53+
after_success:
54+
55+
- if [[ "$TRAVIS_BRANCH" == "master" ]]; then ./gfm-render.sh; fi
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// last checked with Xcode 10.1
2+
#if swift(>=4.2)
3+
print("Hello, Swift 4.2!")
4+
#endif
5+
6+
extension Collection where Element: Equatable {
7+
8+
/// In a sorted collection, replaces the given index with a successor mapping to a unique element.
9+
///
10+
/// - Parameter index: A valid index of the collection. `index` must be less than `endIndex`
11+
func formUniqueIndex(after index: inout Index) {
12+
var prev = index
13+
repeat {
14+
prev = index
15+
formIndex(after: &index)
16+
} while index < endIndex && self[prev] == self[index]
17+
}
18+
}
19+
20+
extension BidirectionalCollection where Element: Equatable {
21+
22+
/// In a sorted collection, replaces the given index with a predecessor that maps to a unique element.
23+
///
24+
/// - Parameter index: A valid index of the collection. `index` must be greater than `startIndex`.
25+
func formUniqueIndex(before index: inout Index) {
26+
var prev = index
27+
repeat {
28+
prev = index
29+
formIndex(before: &index)
30+
} while index > startIndex && self[prev] == self[index]
31+
}
32+
}
33+
34+
func threeSum<T: BidirectionalCollection>(_ collection: T, target: T.Element) -> [[T.Element]] where T.Element: Numeric & Comparable {
35+
let sorted = collection.sorted()
36+
var ret: [[T.Element]] = []
37+
var l = sorted.startIndex
38+
39+
ThreeSum: while l < sorted.endIndex { defer { sorted.formUniqueIndex(after: &l) }
40+
var m = sorted.index(after: l)
41+
var r = sorted.index(before: sorted.endIndex)
42+
43+
TwoSum: while m < r && r < sorted.endIndex {
44+
let sum = sorted[l] + sorted[m] + sorted[r]
45+
if sum == target {
46+
ret.append([sorted[l], sorted[m], sorted[r]])
47+
sorted.formUniqueIndex(after: &m)
48+
sorted.formUniqueIndex(before: &r)
49+
} else if sum < target {
50+
sorted.formUniqueIndex(after: &m)
51+
} else {
52+
sorted.formUniqueIndex(before: &r)
53+
}
54+
}
55+
}
56+
57+
return ret
58+
}
59+
60+
// Answer: [[-1, 0, 1], [-1, -1, 2]]
61+
threeSum([-1, 0, 1, 2, -1, -4], target: 0)
62+
63+
// Answer: [[-1, -1, 2], [-1, 0, 1]]
64+
threeSum([-1, -1, -1, -1, 2, 1, -4, 0], target: 0)
65+
66+
// Answer: [[-1, -1, 2]]
67+
threeSum([-1, -1, -1, -1, -1, -1, 2], target: 0)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// last checked with Xcode 10.1
2+
#if swift(>=4.2)
3+
print("Hello, Swift 4.2!")
4+
#endif
5+
6+
extension Collection where Element: Equatable {
7+
8+
/// In a sorted collection, replaces the given index with a successor mapping to a unique element.
9+
///
10+
/// - Parameter index: A valid index of the collection. `index` must be less than `endIndex`
11+
func formUniqueIndex(after index: inout Index) {
12+
var prev = index
13+
repeat {
14+
prev = index
15+
formIndex(after: &index)
16+
} while index < endIndex && self[prev] == self[index]
17+
}
18+
}
19+
20+
extension BidirectionalCollection where Element: Equatable {
21+
22+
/// In a sorted collection, replaces the given index with a predecessor that maps to a unique element.
23+
///
24+
/// - Parameter index: A valid index of the collection. `index` must be greater than `startIndex`.
25+
func formUniqueIndex(before index: inout Index) {
26+
var prev = index
27+
repeat {
28+
prev = index
29+
formIndex(before: &index)
30+
} while index > startIndex && self[prev] == self[index]
31+
}
32+
}
33+
34+
func fourSum<T: BidirectionalCollection>(_ collection: T, target: T.Element) -> [[T.Element]] where T.Element: Numeric & Comparable {
35+
let sorted = collection.sorted()
36+
var ret: [[T.Element]] = []
37+
38+
var l = sorted.startIndex
39+
FourSum: while l < sorted.endIndex { defer { sorted.formUniqueIndex(after: &l) }
40+
var ml = sorted.index(after: l)
41+
42+
ThreeSum: while ml < sorted.endIndex { defer { sorted.formUniqueIndex(after: &ml) }
43+
var mr = sorted.index(after: ml)
44+
var r = sorted.index(before: sorted.endIndex)
45+
46+
TwoSum: while mr < r && r < sorted.endIndex {
47+
let sum = sorted[l] + sorted[ml] + sorted[mr] + sorted[r]
48+
if sum == target {
49+
ret.append([sorted[l], sorted[ml], sorted[mr], sorted[r]])
50+
sorted.formUniqueIndex(after: &mr)
51+
sorted.formUniqueIndex(before: &r)
52+
} else if sum < target {
53+
sorted.formUniqueIndex(after: &mr)
54+
} else {
55+
sorted.formUniqueIndex(before: &r)
56+
}
57+
}
58+
}
59+
}
60+
return ret
61+
}
62+
63+
// answer: [[-2, -1, 1, 2], [-2, 0, 0, 2], [-1, 0, 0, 1]]
64+
fourSum([1, 0, -1, 0, -2, 2], target: 0)
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'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

0 commit comments

Comments
 (0)