This file contains hidden or 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
| import Foundation | |
| @objcMembers class ClassA: NSObject, NSSecureCoding { | |
| var title: String | |
| init(title: String) { | |
| self.title = title | |
| } | |
| func encode(with aCoder: NSCoder) { |
This file contains hidden or 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
| 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] |
This file contains hidden or 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
| 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) | |
| } |
This file contains hidden or 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
| 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) |