Skip to content

Commit 6a15d37

Browse files
Revert "Improved formatting in code and markdown"
This reverts commit 13de049.
1 parent 13de049 commit 6a15d37

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

Palindromes/README.markdown

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,45 +25,45 @@ Here is a recursive implementation of this in Swift:
2525

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

31-
if length > 1 {
32-
return palindrome(strippedString.lowercased(), left: 0, right: length - 1)
33-
}
34-
return false
31+
if length > 1 {
32+
return palindrome(strippedString.lowercased(), left: 0, right: length - 1)
33+
}
34+
return false
3535
}
3636

3737
private func palindrome(_ str: String, left: Int, right: Int) -> Bool {
38-
if left >= right {
39-
return true
40-
}
38+
if left >= right {
39+
return true
40+
}
4141

42-
let lhs = str[str.index(str.startIndex, offsetBy: left)]
43-
let rhs = str[str.index(str.startIndex, offsetBy: right)]
42+
let lhs = str[str.index(str.startIndex, offsetBy: left)]
43+
let rhs = str[str.index(str.startIndex, offsetBy: right)]
4444

45-
if lhs != rhs {
46-
return false
47-
}
45+
if lhs != rhs {
46+
return false
47+
}
4848

49-
return palindrome(str, left: left + 1, right: right - 1)
49+
return palindrome(str, left: left + 1, right: right - 1)
5050
}
5151
```
5252

5353
This algorithm has a two-step process.
5454

55-
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.
55+
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.
5656

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

6161
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.
6262

63-
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.
63+
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.
6464
```swift
6565
if left >= right {
66-
return true
66+
return true
6767
}
6868
```
6969
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.
@@ -72,7 +72,7 @@ let lhs = str[str.index(str.startIndex, offsetBy: left)]
7272
let rhs = str[str.index(str.startIndex, offsetBy: right)]
7373

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

8383
Step 1:
84-
85-
`race?C ar -> raceCar -> racecar`
84+
` race?C ar -> raceCar -> racecar``
8685

8786
Step 2:
8887
```
8988
| |
9089
racecar -> r == r
9190
92-
| |
91+
| |
9392
racecar -> a == a
9493
95-
| |
94+
| |
9695
racecar -> c == c
9796
98-
|
97+
|
9998
racecar -> left index == right index -> return true
10099
```
101100

0 commit comments

Comments
 (0)