Swift: Subtract the Product and Sum of Digits of an Integer
swift\nclass Solution {\n    func subtractProductAndSum(_ n: Int) -> Int {\n        if 1 <= n && n <= 10^5 { return 0 }\n        var cp = n, products = [Int]()\
```swift\nclass Solution {\n    func subtractProductAndSum(_ n: Int) -> Int {\n        if 1 <= n && n <= 10^5 { return 0 }\n        var cp = n, products = [Int]()\n        while cp > 0 {\n            products.insert(cp % 10, at: 0)\n            cp /= 10\n        }\n        let product = products.reduce(1, *)\n        let sum = products.reduce(0, +)\n        return product - sum\n    }\n}\n```\n\n```swift\nimport XCTest\n\n// Executed 2 tests, with 0 failures (0 unexpected) in 0.006 (0.008) seconds\n\nclass Tests: XCTestCase {\n    private let s = Solution()\n    \n    func testExample1() {\n        XCTAssert(s.subtractProductAndSum(234) == 15) // success\n    }\n    func testExample2() {\n        XCTAssert(s.subtractProductAndSum(4421) == 21) // success\n    }\n}\n\nTests.defaultTestSuite.run()\n```
['Swift']