Skip to content

Commit 9299df4

Browse files
authored
Merge pull request kodecocodes#801 from flohei/chore/swift-4-palindrome
[Swift 4.2] Update Palindromes
2 parents 28f9231 + 93a4d2d commit 9299df4

File tree

9 files changed

+85
-79
lines changed

9 files changed

+85
-79
lines changed

Palindromes/Palindromes.playground/Contents.swift

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,8 @@
11
//: Playground - noun: a place where people can play
22

3-
// last checked with Xcode 9.0b4
4-
#if swift(>=4.0)
5-
print("Hello, Swift 4!")
6-
#endif
7-
83
import Foundation
94

10-
/**
11-
Validate that a string is a plaindrome
12-
- parameter str: The string to validate
13-
- returns: `true` if string is plaindrome, `false` if string is not
14-
*/
15-
func isPalindrome(_ str: String) -> Bool {
16-
let strippedString = str.replacingOccurrences(of: "\\W", with: "", options: .regularExpression, range: nil)
17-
let length = strippedString.characters.count
18-
19-
if length > 1 {
20-
return palindrome(strippedString.lowercased(), left: 0, right: length - 1)
21-
}
22-
return false
23-
}
24-
25-
/**
26-
Compares a strings left side character against right side character following
27-
- parameter str: The string to compare characters of
28-
- parameter left: Index of left side to compare, must be less than or equal to right
29-
- parameter right: Index of right side to compare, must be greater than or equal to left
30-
- returns: `true` if left side and right side have all been compared and they all match, `false` if a left and right aren't equal
31-
*/
32-
private func palindrome(_ str: String, left: Int, right: Int) -> Bool {
33-
if left >= right {
34-
return true
35-
}
36-
37-
let lhs = str[str.index(str.startIndex, offsetBy: left)]
38-
let rhs = str[str.index(str.startIndex, offsetBy: right)]
39-
40-
if lhs != rhs {
41-
return false
42-
}
43-
44-
return palindrome(str, left: left + 1, right: right - 1)
45-
}
46-
47-
//true
5+
// true
486
isPalindrome("A man, a plan, a canal, Panama!")
497
isPalindrome("abbcbba")
508
isPalindrome("racecar")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import Foundation
2+
3+
/**
4+
Validate that a string is a plaindrome
5+
- parameter str: The string to validate
6+
- returns: `true` if string is plaindrome, `false` if string is not
7+
*/
8+
public func isPalindrome(_ str: String) -> Bool {
9+
let strippedString = str.replacingOccurrences(of: "\\W", with: "", options: .regularExpression, range: nil)
10+
let length = strippedString.count
11+
12+
if length > 1 {
13+
return palindrome(strippedString.lowercased(), left: 0, right: length - 1)
14+
}
15+
return false
16+
}
17+
18+
/**
19+
Compares a strings left side character against right side character following
20+
- parameter str: The string to compare characters of
21+
- parameter left: Index of left side to compare, must be less than or equal to right
22+
- parameter right: Index of right side to compare, must be greater than or equal to left
23+
- returns: `true` if left side and right side have all been compared and they all match, `false` if a left and right aren't equal
24+
*/
25+
private func palindrome(_ str: String, left: Int, right: Int) -> Bool {
26+
if left >= right {
27+
return true
28+
}
29+
30+
let lhs = str[str.index(str.startIndex, offsetBy: left)]
31+
let rhs = str[str.index(str.startIndex, offsetBy: right)]
32+
33+
if lhs != rhs {
34+
return false
35+
}
36+
37+
return palindrome(str, left: left + 1, right: right - 1)
38+
}
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>

Palindromes/README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Here is a recursive implementation of this in Swift:
2626
```swift
2727
func isPalindrome(_ str: String) -> Bool {
2828
let strippedString = str.replacingOccurrences(of: "\\W", with: "", options: .regularExpression, range: nil)
29-
let length = strippedString.characters.count
29+
let length = strippedString.count
3030

3131
if length > 1 {
3232
return palindrome(strippedString.lowercased(), left: 0, right: length - 1)

Palindromes/Palindromes.swift renamed to Palindromes/Test/Palindrome.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Foundation
22

33
func isPalindrome(_ str: String) -> Bool {
44
let strippedString = str.replacingOccurrences(of: "\\W", with: "", options: .regularExpression, range: nil)
5-
let length = strippedString.characters.count
5+
let length = strippedString.count
66

77
if length > 1 {
88
return palindrome(strippedString.lowercased(), left: 0, right: length - 1)

Palindromes/Test/Palindromes.swift

Lines changed: 0 additions & 26 deletions
This file was deleted.

Palindromes/Test/Test.xcodeproj/project.pbxproj

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
/* Begin PBXBuildFile section */
1010
9437D8841E0D960A00A38FB8 /* Test.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9437D8831E0D960A00A38FB8 /* Test.swift */; };
11-
9437D88B1E0D969500A38FB8 /* Palindromes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9437D8791E0D948A00A38FB8 /* Palindromes.swift */; };
11+
9437D88B1E0D969500A38FB8 /* Palindrome.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9437D8791E0D948A00A38FB8 /* Palindrome.swift */; };
1212
/* End PBXBuildFile section */
1313

1414
/* Begin PBXFileReference section */
15-
9437D8791E0D948A00A38FB8 /* Palindromes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Palindromes.swift; sourceTree = "<group>"; };
15+
9437D8791E0D948A00A38FB8 /* Palindrome.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Palindrome.swift; sourceTree = "<group>"; };
1616
9437D8811E0D960A00A38FB8 /* Test.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = Test.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
1717
9437D8831E0D960A00A38FB8 /* Test.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Test.swift; sourceTree = "<group>"; };
1818
9437D8851E0D960A00A38FB8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -32,7 +32,7 @@
3232
9437D8651E0D945200A38FB8 = {
3333
isa = PBXGroup;
3434
children = (
35-
9437D8791E0D948A00A38FB8 /* Palindromes.swift */,
35+
9437D8791E0D948A00A38FB8 /* Palindrome.swift */,
3636
9437D8821E0D960A00A38FB8 /* Test */,
3737
9437D86F1E0D945200A38FB8 /* Products */,
3838
);
@@ -82,11 +82,12 @@
8282
isa = PBXProject;
8383
attributes = {
8484
LastSwiftUpdateCheck = 0820;
85-
LastUpgradeCheck = 0820;
85+
LastUpgradeCheck = 1000;
8686
ORGANIZATIONNAME = "Joshua Alvarado";
8787
TargetAttributes = {
8888
9437D8801E0D960A00A38FB8 = {
8989
CreatedOnToolsVersion = 8.2;
90+
LastSwiftMigration = 1000;
9091
ProvisioningStyle = Automatic;
9192
};
9293
};
@@ -123,7 +124,7 @@
123124
isa = PBXSourcesBuildPhase;
124125
buildActionMask = 2147483647;
125126
files = (
126-
9437D88B1E0D969500A38FB8 /* Palindromes.swift in Sources */,
127+
9437D88B1E0D969500A38FB8 /* Palindrome.swift in Sources */,
127128
9437D8841E0D960A00A38FB8 /* Test.swift in Sources */,
128129
);
129130
runOnlyForDeploymentPostprocessing = 0;
@@ -140,15 +141,23 @@
140141
CLANG_CXX_LIBRARY = "libc++";
141142
CLANG_ENABLE_MODULES = YES;
142143
CLANG_ENABLE_OBJC_ARC = YES;
144+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
143145
CLANG_WARN_BOOL_CONVERSION = YES;
146+
CLANG_WARN_COMMA = YES;
144147
CLANG_WARN_CONSTANT_CONVERSION = YES;
148+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
145149
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
146150
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
147151
CLANG_WARN_EMPTY_BODY = YES;
148152
CLANG_WARN_ENUM_CONVERSION = YES;
149153
CLANG_WARN_INFINITE_RECURSION = YES;
150154
CLANG_WARN_INT_CONVERSION = YES;
155+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
156+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
157+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
151158
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
159+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
160+
CLANG_WARN_STRICT_PROTOTYPES = YES;
152161
CLANG_WARN_SUSPICIOUS_MOVE = YES;
153162
CLANG_WARN_UNREACHABLE_CODE = YES;
154163
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@@ -187,15 +196,23 @@
187196
CLANG_CXX_LIBRARY = "libc++";
188197
CLANG_ENABLE_MODULES = YES;
189198
CLANG_ENABLE_OBJC_ARC = YES;
199+
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
190200
CLANG_WARN_BOOL_CONVERSION = YES;
201+
CLANG_WARN_COMMA = YES;
191202
CLANG_WARN_CONSTANT_CONVERSION = YES;
203+
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
192204
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
193205
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
194206
CLANG_WARN_EMPTY_BODY = YES;
195207
CLANG_WARN_ENUM_CONVERSION = YES;
196208
CLANG_WARN_INFINITE_RECURSION = YES;
197209
CLANG_WARN_INT_CONVERSION = YES;
210+
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
211+
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
212+
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
198213
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
214+
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
215+
CLANG_WARN_STRICT_PROTOTYPES = YES;
199216
CLANG_WARN_SUSPICIOUS_MOVE = YES;
200217
CLANG_WARN_UNREACHABLE_CODE = YES;
201218
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
@@ -229,7 +246,8 @@
229246
PRODUCT_NAME = "$(TARGET_NAME)";
230247
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
231248
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
232-
SWIFT_VERSION = 3.0;
249+
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
250+
SWIFT_VERSION = 4.2;
233251
};
234252
name = Debug;
235253
};
@@ -243,7 +261,8 @@
243261
PRODUCT_BUNDLE_IDENTIFIER = self.edu.Test;
244262
PRODUCT_NAME = "$(TARGET_NAME)";
245263
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
246-
SWIFT_VERSION = 3.0;
264+
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
265+
SWIFT_VERSION = 4.2;
247266
};
248267
name = Release;
249268
};
@@ -266,6 +285,7 @@
266285
9437D8881E0D960A00A38FB8 /* Release */,
267286
);
268287
defaultConfigurationIsVisible = 0;
288+
defaultConfigurationName = Release;
269289
};
270290
/* End XCConfigurationList section */
271291
};
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>

Palindromes/Test/Test.xcodeproj/xcshareddata/xcschemes/Test.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 = "0820"
3+
LastUpgradeVersion = "1000"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

0 commit comments

Comments
 (0)