Avatar
Avatar
log5
@swift-5.7.3 @swift-5.2.5 import Foundation func task() -> Bool { let N = 2 let S = ["b", "m"] let T = ["m", "d"] var ans = true var words = Set<String>() var g = [String: String]() for i in 0..<N { g[S[i]] = T[i] words.insert(S[i]) words.insert(T[i]) } var done = Set<String>() var seen = Set<String>() func rec(_ v: String) { seen.insert(v) guard let next = g[v] else { return } if seen.contains(next) { if !done.contains(next) { ans = false return } } rec(next) done.insert(v) } for v in words where !seen.contains(v) { rec(v) } return ans } func task2() { var TRUE = 0, FALSE = 0 for _ in 0..<10000 { switch task() { case true: TRUE += 1 case false: FALSE += 1 } } print("TRUE: \(TRUE), FALSE: \(FALSE)") } for _ in 0..<10 { task2() }
TRUE: 0, FALSE: 10000 TRUE: 0, FALSE: 10000 TRUE: 0, FALSE: 10000 TRUE: 0, FALSE: 10000 TRUE: 0, FALSE: 10000 TRUE: 0, FALSE: 10000 TRUE: 0, FALSE: 10000 TRUE: 0, FALSE: 10000 TRUE: 0, FALSE: 10000 TRUE: 0, FALSE: 10000