Created
April 19, 2019 04:29
-
-
Save dbonates/cdf8a59509823cd9e88731b1cd5ba9fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| import XCTest | |
| // experimenting with strings | |
| // remove string at given indexes | |
| extension String { | |
| // just a convenience method to visualize indexes | |
| func printIndexes() { | |
| for (i, char) in enumerated() { | |
| print("\(i): \(char)") | |
| } | |
| } | |
| // easy String subscript | |
| func subString(from: Int, to: Int) -> String { | |
| let startIndex = self.index(self.startIndex, offsetBy: from) | |
| let endIndex = self.index(self.startIndex, offsetBy: to) | |
| return String(self[startIndex..<endIndex]) | |
| } | |
| func remove2(at indexes: [Int]) -> String { | |
| var result = "" | |
| var currentOffset = 0 | |
| for i in indexes { | |
| result.append(subString(from: currentOffset, to: i)) | |
| currentOffset = i + 1 | |
| } | |
| result.append(subString(from: currentOffset, to: self.count)) | |
| return result | |
| } | |
| func remove(at indices: [Int]) -> String { | |
| var output = "" | |
| for (i, char) in Array(str).enumerated() { | |
| if !indices.contains(i) { | |
| output.append(contentsOf: "\(char)") | |
| } | |
| } | |
| return output | |
| } | |
| } | |
| var str = "Pomerode" | |
| class Performancer: XCTestCase { | |
| func testOne() { | |
| measure { | |
| str.remove(at: [1,4,7]) | |
| } | |
| } | |
| func testTwo() { | |
| measure { | |
| str.remove2(at: [1,4,7]) | |
| } | |
| } | |
| } | |
| Performancer.defaultTestSuite.run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment