Skip to content

Instantly share code, notes, and snippets.

View BeauNouvelle's full-sized avatar
👨‍💻
Working

Beau Nouvelle BeauNouvelle

👨‍💻
Working
View GitHub Profile
@BeauNouvelle
BeauNouvelle / BigInt1.swift
Created April 4, 2017 12:56
BigIntDeclaration
struct BigInt {
var value: String
}
@BeauNouvelle
BeauNouvelle / BigInt2.swift
Created April 4, 2017 12:57
Multiplication
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
@BeauNouvelle
BeauNouvelle / BigInt3.swift
Last active April 4, 2017 13:33
BigInt Example of what we want to do
let int1 = BigInt("10")
let int2 = BigInt("5")
let product = int1 * int2
@BeauNouvelle
BeauNouvelle / BigInt4.swift
Last active April 4, 2017 13:33
BigInt example with changes.
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))! }
...
@BeauNouvelle
BeauNouvelle / BigInt5.swift
Last active April 4, 2017 13:34
More changes to BigInt
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: ""))
}
@BeauNouvelle
BeauNouvelle / BigInt6.swift
Last active April 4, 2017 13:35
Operator overloading for bigInt
func * (left: BigInt, right: BigInt) -> BigInt { return left.multiply(right) }
@BeauNouvelle
BeauNouvelle / BigInt.swift
Last active April 22, 2019 13:25
BigInt completed
import UIKit
import Foundation
struct BigInt {
var value: String
func multiply(right: BigInt) -> BigInt {
var leftCharacterArray = value.characters.reversed().map { Int(String($0))! }
var rightCharacterArray = right.value.characters.reversed().map { Int(String($0))! }
@BeauNouvelle
BeauNouvelle / BigIntResult.swift
Created April 4, 2017 13:30
Results of a multiplication with big int
let int1 = BigInt(value: "34757377374573285823949593499549646311234")
let int2 = BigInt(value: "45123498348958923845838948528381283721377123454345")
let product = int1 * int2
// result: 1568374460575699918065222772187576926314538959488859994856690985365041943440429553059611730
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()
}
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
}