Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SwiftArchitect/6231cfa962ea4c78b7e03f7127d7491a to your computer and use it in GitHub Desktop.
Save SwiftArchitect/6231cfa962ea4c78b7e03f7127d7491a to your computer and use it in GitHub Desktop.
Swift Xcode implementation of InterviewCake _Product of integers in an array_ using an `Array` extension. ⚠️ Spoiler alert: this is an actual answer to the https://www.interviewcake.com/question/swift/product-of-other-numbers question, so no peeking!
//
// Array+ProductsOfAllIntsExceptAdIndex.swift
//
// Copyright © 2018 Xavier Schott
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import Foundation
extension Array where Element == Int {
func getProductsOfAllIntsExceptAtIndex() -> [Double] {
var result = [Double](repeatElement(0, count: count))
var cumul:Double = 1
for i in (0 ..< count).reversed() {
result[i] = cumul
cumul *= Double(self[i])
}
cumul = 1
for i in 0 ..< count {
result[i] *= cumul
cumul *= Double(self[i])
}
return result
}
}
//
// ProductOfIntegersTests.swift
//
// Copyright © 2018 Xavier Schott
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
import XCTest
class ProductOfIntegersTests: XCTestCase {
func test1734() {
let array = [1, 7, 3, 4]
let expected:[Double] = [84, 12, 28, 21]
let result = array.getProductsOfAllIntsExceptAtIndex()
XCTAssertEqual(expected, result, "\(array) != \(result)")
}
func test17340() {
let array = [1, 7, 3, 4, 0]
let expected:[Double] = [0, 0, 0, 0, 84]
let result = array.getProductsOfAllIntsExceptAtIndex()
XCTAssertEqual(expected, result, "\(array) != \(result)")
}
func testGetProductsOfAllIntsExceptAtIndex() {
var largeArray = [Int](repeatElement(0, count: 16384))
for i in 0 ..< largeArray.count {
largeArray[i] = 1 + Int(arc4random_uniform(2))
}
self.measure { // Time 0.0012 sec
_ = largeArray.getProductsOfAllIntsExceptAtIndex()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment