View BigInt1.swift
This file contains 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
struct BigInt { | |
var value: String | |
} |
View BigInt2.swift
This file contains 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
func multiply(left: String, right: String) -> String { | |
var leftCharacterArray = left.characters.reversed().map { Int(String($0))! } | |
var rightCharacterArray = right.characters.reversed().map { Int(String($0))! } | |
var result = [Int](repeating: 0, count: leftCharacterArray.count+rightCharacterArray.count) | |
for leftIndex in 0..<leftCharacterArray.count { | |
for rightIndex in 0..<rightCharacterArray.count { | |
let resultIndex = leftIndex + rightIndex | |
View BigIntResult.swift
This file contains 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
let int1 = BigInt(value: "34757377374573285823949593499549646311234") | |
let int2 = BigInt(value: "45123498348958923845838948528381283721377123454345") | |
let product = int1 * int2 | |
// result: 1568374460575699918065222772187576926314538959488859994856690985365041943440429553059611730 |
View BigInt3.swift
This file contains 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
let int1 = BigInt("10") | |
let int2 = BigInt("5") | |
let product = int1 * int2 |
View BigInt4.swift
This file contains 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
struct BigInt { | |
var value: String | |
func multiply(right: String) -> String { | |
// We'll change this line to use `value` | |
var leftCharacterArray = value.characters.reversed().map { Int(String($0))! } | |
var rightCharacterArray = right.characters.reversed().map { Int(String($0))! } | |
... |
View BigInt5.swift
This file contains 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
func multiply(right: BigInt) -> BigInt { | |
var a1 = value.characters.reversed().map { Int(String($0))! } | |
var a2 = right.value.characters.reversed().map { Int(String($0))! } | |
... | |
... | |
// And change the last line | |
return BigInt(value: result.map { String($0) }.joined(separator: "")) | |
} |
View BigInt6.swift
This file contains 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
func * (left: BigInt, right: BigInt) -> BigInt { return left.multiply(right) } |
View MockSession.swift
This file contains 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
class MockSession: URLSession { | |
var completionHandler: ((Data, URLResponse, Error) -> Void)? | |
static var mockResponse: (data: Data?, URLResponse: URLResponse?, error: Error?) | |
override class var shared: URLSession { | |
return MockSession() | |
} | |
View MockTask.swift
This file contains 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
class MockTask: URLSessionDataTask { | |
typealias Response = (data: Data?, URLResponse: URLResponse?, error: Error?) | |
var mockResponse: Response | |
let completionHandler: ((Data?, URLResponse?, Error?) -> Void)? | |
init(response: Response, completionHandler: ((Data?, URLResponse?, Error?) -> Void)?) { | |
self.mockResponse = response | |
self.completionHandler = completionHandler | |
} |
View MockSessionExample.swift
This file contains 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
func testRetrieveProductsValidResponse() { | |
// we have a locally stored product list in JSON format to test against. | |
let testBundle = Bundle(forClass: type(of: self)) | |
let filepath = testBundle.pathForResource("products", ofType: "txt") | |
let data = Data(contentsOfFile: filepath!) | |
let urlResponse = HTTPURLResponse(url: URL(string: "https://anyurl.doesntmatter.com")!, statusCode: 200, httpVersion: nil, headerFields: nil) | |
// setup our mock response with the above data and fake response. | |
MockSession.mockResponse = (data, urlResponse: urlResponse, error: nil) | |
let requestsClass = RequestManager() | |
// All our network calls are in here. |
OlderNewer