8000 Upgrade to Swift 3. · lexrus/LeetCode.swift@281bffd · GitHub
[go: up one dir, main page]

Skip to content

Commit 281bffd

Browse files
committed
Upgrade to Swift 3.
1 parent 5e07571 commit 281bffd

Some content is hidden

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

42 files changed

+627
-452
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
osx_image: xcode7.3
1+
osx_image: xcode8
22
language: objective-c
33
script:
44
- |
5-
xctool test \
5+
xctool run-tests \
66
-project LeetCode.xcodeproj \
77
-scheme LeetCodeTests \
88
-sdk macosx

LeetCode.xcodeproj/project.pbxproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,11 +221,12 @@
221221
attributes = {
222222
LastSwiftMigration = 0700;
223223
LastSwiftUpdateCheck = 0700;
224-
LastUpgradeCheck = 0700;
224+
LastUpgradeCheck = 0820;
225225
ORGANIZATIONNAME = "Lex Tang";
226226
TargetAttributes = {
227227
D7FB8E081AF899FE00286272 = {
228228
CreatedOnToolsVersion = 6.3.1;
229+
LastSwiftMigration = 0820;
229230
};
230231
};
231232
};
@@ -322,8 +323,10 @@
322323
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
323324
CLANG_WARN_EMPTY_BODY = YES;
324325
CLANG_WARN_ENUM_CONVERSION = YES;
326+
CLANG_WARN_INFINITE_RECURSION = YES;
325327
CLANG_WARN_INT_CONVERSION = YES;
326328
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
329+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
327330
CLANG_WARN_UNREACHABLE_CODE = YES;
328331
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
329332
COPY_PHASE_STRIP = NO;
@@ -365,8 +368,10 @@
365368
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
366369
CLANG_WARN_EMPTY_BODY = YES;
367370
CLANG_WARN_ENUM_CONVERSION = YES;
371+
CLANG_WARN_INFINITE_RECURSION = YES;
368372
CLANG_WARN_INT_CONVERSION = YES;
369373
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
374+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
370375
CLANG_WARN_UNREACHABLE_CODE = YES;
371376
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
372377
COPY_PHASE_STRIP = NO;
@@ -405,6 +410,7 @@
405410
PRODUCT_BUNDLE_IDENTIFIER = "com.LexTang.$(PRODUCT_NAME:rfc1034identifier)";
406411
PRODUCT_NAME = "$(TARGET_NAME)";
407412
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
413+
SWIFT_VERSION = 3.0;
408414
};
409415
name = Debug;
410416
};
@@ -422,7 +428,8 @@
422428
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
423429
PRODUCT_BUNDLE_IDENTIFIER = "com.LexTang.$(PRODUCT_NAME:rfc1034identifier)";
424430
PRODUCT_NAME = "$(TARGET_NAME)";
425-
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
431+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
432+
SWIFT_VERSION = 3.0;
426433
};
427434
name = Release;
428435
};

LeetCode.xcodeproj/xcshareddata/xcschemes/LeetCodeTests.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0700"
3+
LastUpgradeVersion = "0820"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

README.md

Lines changed: 458 additions & 313 deletions
Large diffs are not rendered by default.

Rakefile

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
require 'rubygems'
2-
require 'nokogiri'
3-
require 'open-uri'
2+
require 'net/http'
3+
require 'json'
44

55
desc 'Start test with XCTool'
66
task :test do
77
system %{
8-
xctool test \
8+
xctool run-tests \
99
-jobs 4 \
1010
-project LeetCode.xcodeproj \
1111
-scheme LeetCodeTests \
@@ -15,24 +15,28 @@ end
1515

1616
desc 'Sync problems list'
1717
task :sync_problems do
18+
uri = URI('https://leetcode.com/api/problems/algorithms/')
19+
response = Net::HTTP.get(uri)
20+
json = JSON.parse(response)
21+
problems = json['stat_status_pairs']
22+
1823
dir = File.dirname(__FILE__)
19-
@doc = Nokogiri::XML(open("https://leetcode.com/problemset/algorithms/"))
20-
problems = @doc.xpath("//*[@id='problemList']//tbody/tr")
21-
problems = problems.map { |tr|
22-
number = tr.xpath("./td[position()=2]").text.to_i
23-
a = tr.xpath(".//a").first
24-
lock = a.parent.xpath(".//i").last
25-
href = a.attributes["href"].value
26-
title = a.text
27-
difficulty = tr.xpath(".//td[last()]").text
28-
finished = File.exists?("#{dir}/Tests/#{number}.swift")
29-
24+
problems = problems.map { |p|
25+
title = p['stat']['question__title']
26+
questionId = p['stat']['question_id']
27+
questionSlug = p['stat']['question__title_slug']
28+
paidOnly = p['paid_only']
29+
difficulty = p['difficulty']['level']
30+
31+
href = "https://leetcode.com/problems/#{questionSlug}"
32+
finished = File.exists?("#{dir}/Tests/#{questionId}.swift")
33+
3034
"- [#{finished ? 'x' : ' '}] " <<
31-
"#{number} " <<
32-
"#{difficulty == "Hard" ? "😨" : (difficulty == "Medium" ? "😐" : "😎")} " <<
35+
"#{questionId} " <<
36+
"#{difficulty == 3 ? "😨" : (difficulty == 2 ? "😐" : "😎")} " <<
3337
"[#{title}](https://leetcode.com#{href}) " <<
34-
(finished ? "[.swift](./Tests/#{number}.swift)" : "") <<
35-
"#{ lock ? " ㊙️" : ""}"
38+
(finished ? "[.swift](./Tests/#{questionId}.swift)" : "") <<
39+
"#{ paidOnly ? " ㊙️" : ""}"
3640
}
3741
s = File.read("#{dir}/README.md")
3842
s = s.gsub(/^- \[.+$\n/, "__")

Shared/String+LeetCode.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ extension String {
1212

1313
subscript (i: Int) -> Character? {
1414
if i > self.characters.count - 1 {
15-
return .None
15+
return .none
1616
}
17-
let index: Index = self.startIndex.advancedBy(i)
17+
let index: Index = self.characters.index(self.startIndex, offsetBy: i)
1818
return self[index]
1919
}
2020

2121
mutating func popBack() -> String {
22-
self.removeAtIndex(self.startIndex.advancedBy(self.characters.count - 1))
22+
self.remove(at: self.characters.index(self.startIndex, offsetBy: self.characters.count - 1))
2323
return self
2424
}
2525

Shared/TreeNode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TreeNode: CustomDebugStringConvertible {
1919
self.rightNode = right
2020
}
2121

22-
private func printTree(tree: TreeNode) -> String {
22+
fileprivate func printTree(_ tree: TreeNode) -> String {
2323
var s = ""
2424
if let l = tree.leftNode {
2525
s = "l=\(printTree(l))"

Tests/1.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ class TwoSum {
4949

5050
- returns: Indeces of the two numbers
5151
*/
52-
class func twoSum0(array: [Int], target: Int) -> (Int, Int)? {
53-
for (i, item0) in array.enumerate() {
54-
for (j, item1) in array.enumerate() {
52+
class func twoSum0(_ array: [Int], target: Int) -> (Int, Int)? {
53+
for (i, item0) in array.enumerated() {
54+
for (j, item1) in array.enumerated() {
5555
if item0 + item1 == target {
5656
return (i + 1, j + 1)
5757
}
5858
}
5959
}
60-
return .None
60+
return .none
6161
}
6262

6363
/**
@@ -72,9 +72,9 @@ class TwoSum {
7272

7373
- returns: Indeces of the two numbers
7474
*/
75-
class func twoSum1(array: [Int], target: Int) -> (Int, Int)? {
75+
class func twoSum1(_ array: [Int], target: Int) -> (Int, Int)? {
7676
var map = [Int: Int]()
77-
for (i, item0) in array.enumerate() {
77+
for (i, item0) in array.enumerated() {
7878
// found the second one
7979
if let secondOne = map[item0] {
8080
return (secondOne + 1, i + 1)
@@ -83,7 +83,7 @@ class TwoSum {
8383
map[target - item0] = i
8484
}
8585
}
86-
return .None
86+
return .none
8787
}
8888

8989
}

Tests/100.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@ import XCTest
1818

1919
private extension TreeNode {
2020

21-
func isEqualTo(tree: TreeNode) -> Bool {
21+
func isEqualTo(_ tree: TreeNode) -> Bool {
2222
if self.value != tree.value {
2323
return false
2424
}
2525
var isEqual = true
26-
if let l = self.leftNode, tl = tree.leftNode {
26+
if let l = self.leftNode, let tl = tree.leftNode {
2727
if !l.isEqualTo(tl) {
2828
isEqual = false
2929
}
3030
}
31-
if let r = self.rightNode, rl = tree.rightNode {
31+
if let r = self.rightNode, let rl = tree.rightNode {
3232
if !r.isEqualTo(rl) {
3333
isEqual = false
3434
}

Tests/101.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Bonus points if you could solve it both recursively and iteratively.
3232
import Foundation
3333
import XCTest
3434

35-
func isNodesSymmetric(left: TreeNode?, right: TreeNode?) -> Bool {
35+
func isNodesSymmetric(_ left: TreeNode?, right: TreeNode?) -> Bool {
3636
if left == nil && right == nil {
3737
return true
3838
}
@@ -49,7 +49,7 @@ func isNodesSymmetric(left: TreeNode?, right: TreeNode?) -> Bool {
4949
&& isNodesSymmetric(right!.leftNode, right: left!.rightNode)
5050
}
5151

52-
func isSymmetric(root: TreeNode?) -> Bool {
52+
func isSymmetric(_ root: TreeNode?) -> Bool {
5353

5454
if root == nil {
5555
return true

0 commit comments

Comments
 (0)
0