Tweak to post

This commit is contained in:
Robert McGovern 2020-04-20 15:45:09 +01:00
parent 8632192912
commit 5ff2d46731
1 changed files with 2 additions and 2 deletions

View File

@ -12,12 +12,12 @@ Second one within 13 hours, good lord, that would never do.
So this challenge was to test if a string matched against the reverse of itself. Basically a palindrome checker. So this challenge was to test if a string matched against the reverse of itself. Basically a palindrome checker.
This took me less time, partly because my first approach was similar to the approach for challenge 1. I used a copy of the string as a ```Character``` Array with two indexes (one counting up from 0, the other down from the max number of characters), and compared each character of the array against the one at the opposite end of the array. This took me less time, partly because my first approach was similar to the approach for challenge 1. I used a copy of the string as a ``Character`` Array with two indexes (one counting up from 0, the other down from the max number of characters), and compared each character of the array against the one at the opposite end of the array.
So for ```['R', 'A', 'R']```, my solution compared if the character at index ```[0]``` was equal to the one at index ```[2]```, then ```[1]``` to ```[1]```. If at any point there wasn't a match, then it would have returned ```false```, otherwise it would have returned ```true```. So for ```['R', 'A', 'R']```, my solution compared if the character at index ```[0]``` was equal to the one at index ```[2]```, then ```[1]``` to ```[1]```. If at any point there wasn't a match, then it would have returned ```false```, otherwise it would have returned ```true```.
So ```['R', 'A', 'R', 'E']``` would fail on the first check because ```[0]``` and ```[3]``` do not match. So ```['R', 'A', 'R', 'E']``` would fail on the first check because ```[0]``` and ```[3]``` do not match.
However ```['b', 'o', 'y', ' ', 'y', 'o', 'b']``` would return true because ```[0] == [6]```, ```[1] == [5]```, ```[2] == [4]``` and ```[3] == [3]```. However ```['b', 'o', 'y', ' ', 'y', 'o', 'b']``` would return true because ```[0] == [6]```, ```[1] == [5]```, ```[2] == [4]``` and ```[3] == [3]``` doesn't need to be tested because its obviously the same character.
A very mechanical way to do it, and there is a simpler way to do it that makes use of Swift's String class. A very mechanical way to do it, and there is a simpler way to do it that makes use of Swift's String class.