-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3dd0674
commit fe93184
Showing
1 changed file
with
54 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,61 @@ | ||
import Cocoa | ||
//: Playground - noun: a place where people can play | ||
|
||
public func palindromeCheck(text: String?) -> Bool { | ||
if let text = text { | ||
let mutableText = text.trimmingCharacters(in: NSCharacterSet.whitespaces).lowercased() | ||
let length: Int = mutableText.characters.count | ||
import Foundation | ||
|
||
/** | ||
Validate that a string is a plaindrome | ||
- parameter str: The string to validate | ||
- returns: `true` if string is plaindrome, `false` if string is not | ||
*/ | ||
func isPalindrome(_ str: String) -> Bool { | ||
let strippedString = str.replacingOccurrences(of: "\\W", with: "", options: .regularExpression, range: nil) | ||
let length = strippedString.characters.count | ||
|
||
if length == 1 || length == 0 { | ||
return true | ||
} else if mutableText[mutableText.startIndex] == mutableText[mutableText.index(mutableText.endIndex, offsetBy: -1)] { | ||
let range = Range<String.Index>(mutableText.index(mutableText.startIndex, offsetBy: 1)..<mutableText.index(mutableText.endIndex, offsetBy: -1)) | ||
return palindromeCheck(text: mutableText.substring(with: range)) | ||
if length > 1 { | ||
return palindrome(strippedString.lowercased(), left: 0, right: length - 1) | ||
} | ||
} | ||
|
||
return false | ||
return false | ||
} | ||
|
||
// Test to check that non-palindromes are handled correctly: | ||
palindromeCheck(text: "owls") | ||
|
||
// Test to check that palindromes are accurately found (regardless of case and whitespace: | ||
palindromeCheck(text: "lol") | ||
palindromeCheck(text: "race car") | ||
palindromeCheck(text: "Race fast Safe car") | ||
|
||
// Test to check that palindromes are found regardless of case: | ||
palindromeCheck(text: "HelloLLEH") | ||
/** | ||
Compares a strings left side character against right side character following | ||
- parameter str: The string to compare characters of | ||
- parameter left: Index of left side to compare, must be less than or equal to right | ||
- parameter right: Index of right side to compare, must be greater than or equal to left | ||
- 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 | ||
*/ | ||
private func palindrome(_ str: String, left: Int, right: Int) -> Bool { | ||
if left >= right { | ||
return true | ||
} | ||
|
||
let lhs = str[str.index(str.startIndex, offsetBy: left)] | ||
let rhs = str[str.index(str.startIndex, offsetBy: right)] | ||
|
||
if lhs != rhs { | ||
return false | ||
} | ||
|
||
return palindrome(str, left: left + 1, right: right - 1) | ||
} | ||
|
||
palindromeCheck(text: "moom") | ||
//true | ||
isPalindrome("A man, a plan, a canal, Panama!") | ||
isPalindrome("abbcbba") | ||
isPalindrome("racecar") | ||
isPalindrome("Madam, I'm Adam") | ||
isPalindrome("Madam in Eden, I'm Adam") | ||
isPalindrome("Never odd or even") | ||
isPalindrome("5885") | ||
isPalindrome("5 8 8 5") | ||
isPalindrome("58 85") | ||
isPalindrome("৯৯") | ||
isPalindrome("In girum imus nocte et consumimur igni") | ||
|
||
// Test that nil and empty Strings return false: | ||
palindromeCheck(text: "") | ||
palindromeCheck(text: nil) | ||
// false | ||
isPalindrome("\\\\") | ||
isPalindrome("desserts") | ||
isPalindrome("😀😀") | ||
isPalindrome("") | ||
isPalindrome("a") | ||
isPalindrome("power") |