Skip to content

Commit

Permalink
Revert "Improved formatting in code and markdown"
Browse files Browse the repository at this point in the history
This reverts commit 13de049.
  • Loading branch information
lostatseajoshua committed Dec 22, 2016
1 parent 13de049 commit 6a15d37
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions Palindromes/README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,45 @@ Here is a recursive implementation of this in Swift:

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

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

private func palindrome(_ str: String, left: Int, right: Int) -> Bool {
if left >= right {
return true
}
if left >= right {
return true
}

let lhs = str[str.index(str.startIndex, offsetBy: left)]
let rhs = str[str.index(str.startIndex, offsetBy: right)]
let lhs = str[str.index(str.startIndex, offsetBy: left)]
let rhs = str[str.index(str.startIndex, offsetBy: right)]

if lhs != rhs {
return false
}
if lhs != rhs {
return false
}

return palindrome(str, left: left + 1, right: right - 1)
return palindrome(str, left: left + 1, right: right - 1)
}
```

This algorithm has a two-step process.

1 - The first step is to pass the string to validate as a palindrome into the `isPalindrome` method. This method first removes occurrences of non-word pattern matches `\W` [Regex reference](http://regexr.com). It is written with two \\ to escape the \ in the String literal.
1. The first step is to pass the string to validate as a palindrome into the `isPalindrome` method. This method first removes occurrences of non-word pattern matches `\W` [Regex reference](http://regexr.com). It is written with two \\ to escape the \ in the String literal.

```swift
let strippedString = str.replacingOccurrences(of: "\\W", with: "", options: .regularExpression, range: nil)
```

The length of the string is then checked to make sure that the string after being stripped of non-word characters is still in a valid length. It is then passed into the next step after being lowercased.

2 - The second step is to pass the string in a recursive method. This method takes a string, a left index, and a right index. The method checks the characters of the string using the indexes to compare each character on both sides. The method checks if the left is greater or equal to the right if so the entire string has been run through without returning false so the string is equal on both sides thus returning true.
2. The second step is to pass the string in a recursive method. This method takes a string, a left index, and a right index. The method checks the characters of the string using the indexes to compare each character on both sides. The method checks if the left is greater or equal to the right if so the entire string has been run through without returning false so the string is equal on both sides thus returning true.
```swift
if left >= right {
return true
return true
}
```
If the check doesn't pass it continues to get the characters at the specified indexes and compare each. If they are not the same the method returns false and exits.
Expand All @@ -72,7 +72,7 @@ let lhs = str[str.index(str.startIndex, offsetBy: left)]
let rhs = str[str.index(str.startIndex, offsetBy: right)]

if lhs != rhs {
return false
return false
}
```
If they are the same the method calls itself again and updates the indexes accordingly to continue to check the rest of the string.
Expand All @@ -81,21 +81,20 @@ return palindrome(str, left: left + 1, right: right - 1)
```

Step 1:

`race?C ar -> raceCar -> racecar`
` race?C ar -> raceCar -> racecar``

Step 2:
```
| |
racecar -> r == r
| |
| |
racecar -> a == a
| |
| |
racecar -> c == c
|
|
racecar -> left index == right index -> return true
```

Expand Down

0 comments on commit 6a15d37

Please sign in to comment.