From 5ff2d467319c1add508367bb6150fe45d7a65589 Mon Sep 17 00:00:00 2001 From: Robert McGovern Date: Mon, 20 Apr 2020 15:45:09 +0100 Subject: [PATCH] Tweak to post --- _posts/2020-04-20-swift-coding-challenge-2.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_posts/2020-04-20-swift-coding-challenge-2.md b/_posts/2020-04-20-swift-coding-challenge-2.md index d0903a1..e5d1932 100644 --- a/_posts/2020-04-20-swift-coding-challenge-2.md +++ b/_posts/2020-04-20-swift-coding-challenge-2.md @@ -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. -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 ```['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.