Created
May 1, 2015 19:47
-
-
Save edwardIshaq/a667a51ba8a95962dd30 to your computer and use it in GitHub Desktop.
Random Title generator
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
// Playground - noun: a place where people can play | |
import UIKit | |
var str = "Generate random events titles" | |
let hosts = ["Eddie", "Paul", "Elyse", "MD", "Jon"] | |
let events = ["Wedding", "Engagement","Birthday"] | |
let magnitudes = ["Party", "Bash", "Jam"] | |
extension Array { | |
var uIntCount:UInt { | |
return UInt(self.count) | |
} | |
} | |
let totalUniq = hosts.uIntCount * events.uIntCount * magnitudes.uIntCount | |
struct Permutation { | |
let hostIdx: Int | |
let eventIdx: Int | |
let magnitudeIdx: Int | |
func toString() -> String { | |
return "\(hosts[hostIdx])'s \(events[eventIdx]) \(magnitudes[magnitudeIdx])" | |
} | |
} | |
infix operator >>=- {associativity left} | |
public func >>=-<A,B>(xs:[A], f: A->[B]) -> [B] { | |
var result = [B]() | |
for x in xs { | |
result += f(x) | |
} | |
return result | |
} | |
typealias EventOptions = (hostsCount:UInt, eventsCount:UInt, magnitudesCount: UInt) | |
class UniquePermutationGenerator: GeneratorType { | |
typealias Element = [UInt] | |
let options: EventOptions | |
var mattrix: [[UInt]] | |
init(userOptions: EventOptions) { | |
options = userOptions | |
let caps = [options.hostsCount, options.eventsCount, options.magnitudesCount] | |
let inputArrays = caps.map{ cap in | |
Array(Range<UInt>(start: 0, end: cap).generate()) | |
} | |
mattrix = | |
inputArrays[0] >>=- { x -> [[UInt]] in | |
inputArrays[1] >>=- { y -> [[UInt]] in | |
inputArrays[2] >>=- { z -> [[UInt]] in | |
return [[x,y,z]] | |
}}} | |
} | |
func next() -> Element? { | |
if count(mattrix) == 0 { | |
return nil | |
} | |
let randIdx = Int(arc4random() % UInt32(count(mattrix))) | |
let randPerm = mattrix[randIdx] | |
mattrix.removeAtIndex(randIdx) | |
return randPerm | |
} | |
} | |
func convertToPerm(randArray:[UInt]) -> Permutation { | |
if count(randArray) != 3 { | |
return Permutation(hostIdx: 0, eventIdx: 0, magnitudeIdx: 0) | |
} | |
return Permutation(hostIdx: Int(randArray[0]), eventIdx: Int(randArray[1]), magnitudeIdx: Int(randArray[2])) | |
} | |
func testRandomPermGen() { | |
let titlesPermGene = UniquePermutationGenerator(userOptions: (hosts.uIntCount, events.uIntCount, magnitudes.uIntCount)) | |
titlesPermGene.mattrix.uIntCount | |
titlesPermGene.mattrix | |
var counter = 1 | |
while let perm = titlesPermGene.next() { | |
let uniqeTitle = convertToPerm(perm).toString() | |
println("\(counter++)- \t \(uniqeTitle)") | |
} | |
} | |
testRandomPermGen() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment