Skip to content

Instantly share code, notes, and snippets.

import Foundation
@objcMembers class ClassA: NSObject, NSSecureCoding {
var title: String
init(title: String) {
self.title = title
}
func encode(with aCoder: NSCoder) {
def sum_of_products(a, n):
running_totals = [0] * n
for x in a:
for i in range(n - 1):
running_totals[i] += running_totals[i + 1] * x
running_totals[-1] += x
return running_totals[0]
import Foundation
func permute<E>(i: Int, array: [E]) -> [E] {
var i = i
var result = array
for j in 0..<array.count {
let k = i % (array.count - j)
i /= (array.count - j)
result.swapAt(j, j + k)
}
def from_permutation_to_disjoints_cycles(perm):
mappings = { a: b for a, b in zip(*perm) }
cycles = []
for a in perm[0]:
b = mappings.pop(a, None)
if b is None:
continue # `a` has already been handled
cycle = [a]
while a != b:
cycle.append(b)