extension String { func insert(_ value: String, interval: Int) -> String { guard interval > 0 && interval < self.count else { return self } let substrings: [Substring] = stride(from: 0, to: self.count, by: interval).map { index in let startIndex = self.index(self.startIndex, offsetBy: index) let endIndex = self.index(startIndex, offsetBy: interval, limitedBy: self.endIndex) ?? self.endIndex return self[startIndex..<endIndex] } return substrings.joined(separator: value) } }
- extension String {
func insert(_ value: String, n: Int) -> String {guard n > 0 else { return self }var result: String = ""stride(from: 0, to: self.count, by: n).forEach { i inlet startIndex = self.index(self.startIndex, offsetBy: i)let endIndex = self.index(startIndex, offsetBy: n, limitedBy: self.endIndex) ?? self.endIndexresult += (result.isEmpty ? "" : value) + self[startIndex..<endIndex]}return result}- func insert(_ value: String, interval: Int) -> String {
- guard interval > 0 && interval < self.count else { return self }
- let substrings: [Substring] = stride(from: 0, to: self.count, by: interval).map { index in
- let startIndex = self.index(self.startIndex, offsetBy: index)
- let endIndex = self.index(startIndex, offsetBy: interval, limitedBy: self.endIndex) ?? self.endIndex
- return self[startIndex..<endIndex]
- }
- return substrings.joined(separator: value)
- }
- }
import XCTest // XCTest Spec Example: // TODO: replace with your own tests (TDD), these are just how-to examples to get you started class SolutionTest: XCTestCase { static var allTests = [ ("Test Example", testExample), ] func testExample() { XCTAssertEqual("603555121".insert("-", interval: 0), "603555121") XCTAssertEqual("603555121".insert("-", interval: 1), "6-0-3-5-5-5-1-2-1") XCTAssertEqual("603555121".insert("-", interval: 3), "603-555-121") XCTAssertEqual("603555121".insert("-", interval: 4), "6035-5512-1") XCTAssertEqual("603555121".insert("-", interval: 5), "60355-5121") XCTAssertEqual("603555121".insert("-", interval: 10), "603555121") // Extra Tests XCTAssertEqual("12345678901234567890".insert("-", interval: 0), "12345678901234567890") XCTAssertEqual("12345678901234567890".insert("-", interval: 1), "1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0") XCTAssertEqual("12345678901234567890".insert("-", interval: 2), "12-34-56-78-90-12-34-56-78-90") XCTAssertEqual("12345678901234567890".insert("-", interval: 3), "123-456-789-012-345-678-90") XCTAssertEqual("12345678901234567890".insert("-", interval: 5), "12345-67890-12345-67890") XCTAssertEqual("12345678901234567890".insert("-", interval: 10), "1234567890-1234567890") } } XCTMain([ testCase(SolutionTest.allTests) ])
- import XCTest
- // XCTest Spec Example:
- // TODO: replace with your own tests (TDD), these are just how-to examples to get you started
- class SolutionTest: XCTestCase {
- static var allTests = [
- ("Test Example", testExample),
- ]
- func testExample() {
XCTAssertEqual("603555121".insert("-", n: 0), "603555121")XCTAssertEqual("603555121".insert("-", n: 1), "6-0-3-5-5-5-1-2-1")XCTAssertEqual("603555121".insert("-", n: 3), "603-555-121")XCTAssertEqual("603555121".insert("-", n: 4), "6035-5512-1")XCTAssertEqual("603555121".insert("-", n: 5), "60355-5121")XCTAssertEqual("603555121".insert("-", n: 10), "603555121")- XCTAssertEqual("603555121".insert("-", interval: 0), "603555121")
- XCTAssertEqual("603555121".insert("-", interval: 1), "6-0-3-5-5-5-1-2-1")
- XCTAssertEqual("603555121".insert("-", interval: 3), "603-555-121")
- XCTAssertEqual("603555121".insert("-", interval: 4), "6035-5512-1")
- XCTAssertEqual("603555121".insert("-", interval: 5), "60355-5121")
- XCTAssertEqual("603555121".insert("-", interval: 10), "603555121")
- // Extra Tests
XCTAssertEqual("12345678901234567890".insert("-", n: 0), "12345678901234567890")XCTAssertEqual("12345678901234567890".insert("-", n: 1), "1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0")XCTAssertEqual("12345678901234567890".insert("-", n: 2), "12-34-56-78-90-12-34-56-78-90")XCTAssertEqual("12345678901234567890".insert("-", n: 3), "123-456-789-012-345-678-90")XCTAssertEqual("12345678901234567890".insert("-", n: 5), "12345-67890-12345-67890")XCTAssertEqual("12345678901234567890".insert("-", n: 10), "1234567890-1234567890")- XCTAssertEqual("12345678901234567890".insert("-", interval: 0), "12345678901234567890")
- XCTAssertEqual("12345678901234567890".insert("-", interval: 1), "1-2-3-4-5-6-7-8-9-0-1-2-3-4-5-6-7-8-9-0")
- XCTAssertEqual("12345678901234567890".insert("-", interval: 2), "12-34-56-78-90-12-34-56-78-90")
- XCTAssertEqual("12345678901234567890".insert("-", interval: 3), "123-456-789-012-345-678-90")
- XCTAssertEqual("12345678901234567890".insert("-", interval: 5), "12345-67890-12345-67890")
- XCTAssertEqual("12345678901234567890".insert("-", interval: 10), "1234567890-1234567890")
- }
- }
- XCTMain([
- testCase(SolutionTest.allTests)
- ])