Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.

Commit

Permalink
Merge branch 'michaeleisel-combinations'
Browse files Browse the repository at this point in the history
  • Loading branch information
pNre committed Dec 15, 2014
2 parents 2c9d9cf + 667c322 commit 46c2682
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
35 changes: 35 additions & 0 deletions ExSwift/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,41 @@ internal extension Array {
return result
}

/**
Returns all of the combinations in the array of the given length
:param: length
:returns: Combinations
*/
func combination (length: Int) -> [[Element]] {
if length < 0 || length > self.count {
return []
}
var indexes: [Int] = (0..<length).toArray()
var combinations: [[Element]] = []
var offset = self.count - indexes.count
while true {
var combination: [Element] = []
for index in indexes {
combination.append(self[index])
}
combinations.append(combination)
var i = indexes.count - 1
while i >= 0 && indexes[i] == i + offset {
i--
}
if i < 0 {
break
}
i++
var start = indexes[i-1] + 1
for j in (i-1)..<indexes.count {
indexes[j] = start + j - i + 1
}
}
return combinations
}

/**
Returns the number of elements which meet the condition
Expand Down
12 changes: 12 additions & 0 deletions ExSwift/Range.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ internal extension Range {

}

/**
Returns each element of the range in an array
:returns: Each element of the range in an array
*/
func toArray () -> [T] {
var result: [T] = []
for i in self {
result.append(i)
}
return result
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions ExSwift/Sequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,19 @@ internal extension SequenceOf {
func takeWhile (condition:(T?) -> Bool) -> SequenceOf<T> {
return SequenceOf(TakeWhileSequence(self, condition))
}

/**
Returns each element of the sequence in an array
:returns: Each element of the sequence in an array
*/
func toArray () -> [T] {
var result: [T] = []
for item in self {
result.append(item)
}
return result
}
}

/**
Expand Down
11 changes: 11 additions & 0 deletions ExSwiftTests/ExSwiftArrayTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,15 @@ class ExtensionsArrayTests: XCTestCase {
XCTAssertEqual(array.uniqueBy({$0 % 3}), [1, 2, 3])
XCTAssertEqual(array.uniqueBy({$0 < 3}), [1, 3])
}

func testCombinations() {
XCTAssertEqual(array.combination(-1), [])
XCTAssertEqual(array.combination(0), [[]])
XCTAssertEqual(array.combination(1), [[1], [2], [3], [4], [5]])
XCTAssertEqual(array.combination(2), [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5], [3, 4], [3, 5], [4, 5]])
XCTAssertEqual(array.combination(3), [[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 3, 4], [1, 3, 5], [1, 4, 5], [2, 3, 4], [2, 3, 5], [2, 4, 5], [3, 4, 5]])
XCTAssertEqual(array.combination(4), [[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]])
XCTAssertEqual(array.combination(5), [[1, 2, 3, 4, 5]])
XCTAssertEqual(array.combination(6), [])
}
}

0 comments on commit 46c2682

Please sign in to comment.