Department numbers: Difference between revisions
Content added Content deleted
(Add Swift) |
|||
Line 2,242: | Line 2,242: | ||
["police", 4] ["fire", 5] ["sanitation", 3] |
["police", 4] ["fire", 5] ["sanitation", 3] |
||
</pre> |
</pre> |
||
=={{header|Swift}}== |
|||
Functional approach: |
|||
<lang swift>let res = [2, 4, 6].map({x in |
|||
return (1...7) |
|||
.filter({ $0 != x }) |
|||
.map({y -> (Int, Int, Int)? in |
|||
let z = 12 - (x + y) |
|||
guard y != z && 1 <= z && z <= 7 else { |
|||
return nil |
|||
} |
|||
return (x, y, z) |
|||
}).compactMap({ $0 }) |
|||
}).flatMap({ $0 }) |
|||
for result in res { |
|||
print(result) |
|||
}</lang> |
|||
Iterative approach: |
|||
<lang swift>var res = [(Int, Int, Int)]() |
|||
for x in [2, 4, 6] { |
|||
for y in 1...7 where x != y { |
|||
let z = 12 - (x + y) |
|||
guard y != z && 1 <= z && z <= 7 else { |
|||
continue |
|||
} |
|||
res.append((x, y, z)) |
|||
} |
|||
} |
|||
for result in res { |
|||
print(result) |
|||
}</lang> |
|||
{{out}} |
|||
<pre>(2, 3, 7) |
|||
(2, 4, 6) |
|||
(2, 6, 4) |
|||
(2, 7, 3) |
|||
(4, 1, 7) |
|||
(4, 2, 6) |
|||
(4, 3, 5) |
|||
(4, 5, 3) |
|||
(4, 6, 2) |
|||
(4, 7, 1) |
|||
(6, 1, 5) |
|||
(6, 2, 4) |
|||
(6, 4, 2) |
|||
(6, 5, 1)</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |