Skip to content

Commit 5a200e0

Browse files
committed
renaming Palindromes to Palindrome
also removing one implementation, moving the one on root level to the playground to reduce the number of implementations to maintain
1 parent 9f5e81e commit 5a200e0

File tree

6 files changed

+65
-33
lines changed

6 files changed

+65
-33
lines changed
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+
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/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: 10 additions & 6 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
);
@@ -87,6 +87,7 @@
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;
@@ -229,7 +230,8 @@
229230
PRODUCT_NAME = "$(TARGET_NAME)";
230231
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
231232
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
232-
SWIFT_VERSION = 3.0;
233+
SWIFT_SWIFT3_OBJC_INFERENCE = On;
234+
SWIFT_VERSION = 4.2;
233235
};
234236
name = Debug;
235237
};
@@ -243,7 +245,8 @@
243245
PRODUCT_BUNDLE_IDENTIFIER = self.edu.Test;
244246
PRODUCT_NAME = "$(TARGET_NAME)";
245247
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
246-
SWIFT_VERSION = 3.0;
248+
SWIFT_SWIFT3_OBJC_INFERENCE = On;
249+
SWIFT_VERSION = 4.2;
247250
};
248251
name = Release;
249252
};
@@ -266,6 +269,7 @@
266269
9437D8881E0D960A00A38FB8 /* Release */,
267270
);
268271
defaultConfigurationIsVisible = 0;
272+
defaultConfigurationName = Release;
269273
};
270274
/* End XCConfigurationList section */
271275
};
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>

0 commit comments

Comments
 (0)