Skip to content

Commit 51eaf0f

Browse files
committed
Initial version
1 parent 065aba7 commit 51eaf0f

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

Package.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import PackageDescription
2+
3+
let package = Package(
4+
name: "swift-array-variations"
5+
)

Sources/swift_array_variations.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
internal func strike(_ array: [Int], from: [Int]) -> [Int] {
2+
var result: Array<Int> = []
3+
4+
for element in from {
5+
if array.index(of: element) == nil {
6+
result.append(element)
7+
}
8+
}
9+
10+
return result
11+
}
12+
13+
extension Array where Element: Equatable {
14+
func variations(class count: Int) -> [[Element]] {
15+
let length = ((self.count - count + 1)...self.count).reduce(1, { a, b in a * b })
16+
let indexes = Array<Int>(0..<self.count)
17+
var repeats = Array<Int>(repeating: length / self.count, count: count)
18+
var divisor = self.count
19+
for i in 1..<count {
20+
divisor -= 1
21+
repeats[i] = repeats[i - 1] / divisor
22+
}
23+
var result = Array<Array<Int>>(repeating: [], count: length)
24+
var k = 0
25+
for i in 0..<count {
26+
k = 0
27+
while k < length {
28+
for number in strike(result[k], from: indexes) {
29+
for _ in 0..<repeats[i] {
30+
result[k].append(number)
31+
k += 1
32+
}
33+
}
34+
}
35+
}
36+
37+
return result.map { variation in variation.map { element in self[element] } }
38+
}
39+
}

Tests/LinuxMain.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import XCTest
2+
@testable import swift_array_variationsTests
3+
4+
XCTMain([
5+
testCase(swift_array_variationsTests.allTests),
6+
])
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import XCTest
2+
@testable import swift_array_variations
3+
4+
class swift_array_variationsTests: XCTestCase {
5+
func test_3_2_Ints() {
6+
let expect = [[0, 1], [0, 2], [1, 0], [1, 2], [2, 0], [2, 1]]
7+
let result = Array<Int>(0...2).variations(class: 2)
8+
9+
XCTAssertEqual(result.count, 6)
10+
XCTAssertEqual(result.count, expect.count)
11+
12+
for i in 0..<6 {
13+
XCTAssertEqual(result[i].count, 2)
14+
for j in 0...1 {
15+
XCTAssertEqual(result[i][j], expect[i][j])
16+
}
17+
}
18+
}
19+
20+
func test_3_3_String() {
21+
let expect = [
22+
["test", "unit-test", "XCTest"],
23+
["test", "XCTest", "unit-test"],
24+
["unit-test", "test", "XCTest"],
25+
["unit-test", "XCTest", "test"],
26+
["XCTest", "test", "unit-test"],
27+
["XCTest", "unit-test", "test"]
28+
]
29+
let result = ["test", "unit-test", "XCTest"].variations(class: 3)
30+
31+
XCTAssertEqual(result.count, 6)
32+
XCTAssertEqual(result.count, expect.count)
33+
34+
for i in 0..<6 {
35+
XCTAssertEqual(result[i].count, 3)
36+
for j in 0...1 {
37+
XCTAssertEqual(result[i][j], expect[i][j])
38+
}
39+
}
40+
}
41+
42+
func test_10_6_Ints() {
43+
let result = Array<Int>(1...10).variations(class: 6)
44+
45+
XCTAssertEqual(result.count, 151200)
46+
47+
for i in 0..<151200 {
48+
XCTAssertEqual(result[i].count, 6)
49+
}
50+
}
51+
52+
static var allTests : [(String, (swift_array_variationsTests) -> () throws -> Void)] {
53+
return [
54+
("test 2-permutation of [0, 1, 2]", test_3_2_Ints),
55+
("test 3-permutation of [\"test\", \"unit-test\", \"XCTest\"]", test_3_3_String),
56+
("test 6-permutation of 10", test_10_6_Ints),
57+
]
58+
}
59+
}

0 commit comments

Comments
 (0)