Last active
October 6, 2017 14:03
-
-
Save olbartek/38db309cc8ab48f10fb0cfaada3cf493 to your computer and use it in GitHub Desktop.
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 UIKit | |
// TASK: | |
var array: [Int] = [1, 2, 3] | |
for currentIndex in 0..<array.count { | |
let randomIndex = Int(arc4random_uniform(UInt32(array.count))) | |
let tmp = array[currentIndex] | |
array[currentIndex] = array[randomIndex] | |
array[randomIndex] = tmp | |
} | |
array | |
// Which outputs have the highest probability? | |
// SOLUTION: | |
var baseArray: [Int] = [1, 2, 3] | |
struct Possibility: CustomStringConvertible, Hashable { | |
private var variations: [[Int]] = [] | |
mutating func appendVariation(_ variation: [Int]) { | |
variations.append(variation) | |
} | |
var lastVariation: [Int] { | |
return variations.last ?? [] | |
} | |
var description: String { | |
return String(describing: lastVariation) | |
} | |
var allVariationsDescription: String { | |
return variations.reduce("", { a, b in "\(a) \(b)" }) | |
} | |
var hashValue: Int { | |
return String(describing: lastVariation).hashValue | |
} | |
static func ==(lhs: Possibility, rhs: Possibility) -> Bool { | |
guard lhs.variations.count == rhs.variations.count else { return false } | |
for i in 0..<lhs.variations.count { | |
if lhs.lastVariation[i] != rhs.lastVariation[i] { return false } | |
} | |
return true | |
} | |
} | |
func arraySwap<T>(_ array: inout [T], indexA: Int, indexB: Int) { | |
let tmp = array[indexA] | |
array[indexA] = array[indexB] | |
array[indexB] = tmp | |
} | |
func performArraySwap(_ randomValuesArray: [Int]) -> Possibility { | |
baseArray = [1, 2, 3] | |
var possibility = Possibility() | |
for x in 0..<3 { | |
arraySwap(&baseArray, indexA: x, indexB: randomValuesArray[x]) | |
possibility.appendVariation(baseArray) | |
} | |
return possibility | |
} | |
func printPossibility(_ i: Int, _ j: Int, _ k: Int) { | |
print("(\(i), \(j), \(k))") | |
} | |
// All possible permutations with repetition | |
func permutationsWithRepetitionFrom<T>(_ elements: [T], taking: Int) -> [[T]] { | |
guard elements.count >= 0 && taking > 0 else { return [[]] } | |
if taking == 1 { | |
return elements.map {[$0]} | |
} | |
var permutations = [[T]]() | |
for element in elements { | |
permutations += permutationsWithRepetitionFrom(elements, taking: taking - 1).map {[element] + $0} | |
} | |
return permutations | |
} | |
let baseArrayIndicies = Array(0..<baseArray.count) | |
var possibilitiesCountDict: [Possibility: Int] = [:] | |
let possiblePermutations = permutationsWithRepetitionFrom(baseArrayIndicies, taking: baseArrayIndicies.count) | |
possiblePermutations.forEach { perm in | |
//printPossibility(perm) | |
let p = performArraySwap(perm) | |
print(p.allVariationsDescription) | |
if let numOfPossibilities = possibilitiesCountDict[p] { | |
possibilitiesCountDict[p] = numOfPossibilities + 1 | |
} else { | |
possibilitiesCountDict[p] = 1 | |
} | |
} | |
print("DICTIONARY:\n") | |
print(possibilitiesCountDict) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment