Skip to content

Commit fe93184

Browse files
Update palindrome playground
1 parent 3dd0674 commit fe93184

File tree

1 file changed

+54
-27
lines changed

1 file changed

+54
-27
lines changed
Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,61 @@
1-
import Cocoa
1+
//: Playground - noun: a place where people can play
22

3-
public func palindromeCheck(text: String?) -> Bool {
4-
if let text = text {
5-
let mutableText = text.trimmingCharacters(in: NSCharacterSet.whitespaces).lowercased()
6-
let length: Int = mutableText.characters.count
3+
import Foundation
4+
5+
/**
6+
Validate that a string is a plaindrome
7+
- parameter str: The string to validate
8+
- returns: `true` if string is plaindrome, `false` if string is not
9+
*/
10+
func isPalindrome(_ str: String) -> Bool {
11+
let strippedString = str.replacingOccurrences(of: "\\W", with: "", options: .regularExpression, range: nil)
12+
let length = strippedString.characters.count
713

8-
if length == 1 || length == 0 {
9-
return true
10-
} else if mutableText[mutableText.startIndex] == mutableText[mutableText.index(mutableText.endIndex, offsetBy: -1)] {
11-
let range = Range<String.Index>(mutableText.index(mutableText.startIndex, offsetBy: 1)..<mutableText.index(mutableText.endIndex, offsetBy: -1))
12-
return palindromeCheck(text: mutableText.substring(with: range))
14+
if length > 1 {
15+
return palindrome(strippedString.lowercased(), left: 0, right: length - 1)
1316
}
14-
}
15-
16-
return false
17+
return false
1718
}
1819

19-
// Test to check that non-palindromes are handled correctly:
20-
palindromeCheck(text: "owls")
21-
22-
// Test to check that palindromes are accurately found (regardless of case and whitespace:
23-
palindromeCheck(text: "lol")
24-
palindromeCheck(text: "race car")
25-
palindromeCheck(text: "Race fast Safe car")
26-
27-
// Test to check that palindromes are found regardless of case:
28-
palindromeCheck(text: "HelloLLEH")
20+
/**
21+
Compares a strings left side character against right side character following
22+
- parameter str: The string to compare characters of
23+
- parameter left: Index of left side to compare, must be less than or equal to right
24+
- parameter right: Index of right side to compare, must be greater than or equal to left
25+
- 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
26+
*/
27+
private func palindrome(_ str: String, left: Int, right: Int) -> Bool {
28+
if left >= right {
29+
return true
30+
}
31+
32+
let lhs = str[str.index(str.startIndex, offsetBy: left)]
33+
let rhs = str[str.index(str.startIndex, offsetBy: right)]
34+
35+
if lhs != rhs {
36+
return false
37+
}
38+
39+
return palindrome(str, left: left + 1, right: right - 1)
40+
}
2941

30-
palindromeCheck(text: "moom")
42+
//true
43+
isPalindrome("A man, a plan, a canal, Panama!")
44+
isPalindrome("abbcbba")
45+
isPalindrome("racecar")
46+
isPalindrome("Madam, I'm Adam")
47+
isPalindrome("Madam in Eden, I'm Adam")
48+
isPalindrome("Never odd or even")
49+
isPalindrome("5885")
50+
isPalindrome("5 8 8 5")
51+
isPalindrome("58 85")
52+
isPalindrome("৯৯")
53+
isPalindrome("In girum imus nocte et consumimur igni")
3154

32-
// Test that nil and empty Strings return false:
33-
palindromeCheck(text: "")
34-
palindromeCheck(text: nil)
55+
// false
56+
isPalindrome("\\\\")
57+
isPalindrome("desserts")
58+
isPalindrome("😀😀")
59+
isPalindrome("")
60+
isPalindrome("a")
61+
isPalindrome("power")

0 commit comments

Comments
 (0)