Skip to content

Commit 74685a5

Browse files
committed
merged master
2 parents 71ab3f8 + 0a1ab85 commit 74685a5

File tree

150 files changed

+6976
-1675
lines changed

Some content is hidden

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

150 files changed

+6976
-1675
lines changed

.github/CONTRIBUTING.md

Lines changed: 3 additions & 1 deletion
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 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.
810
911
**API Design Guidelines**
1012

.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: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,51 @@ 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+
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)
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)