Skip to content

Commit 5f64dc0

Browse files
authored
Merge pull request kodecocodes#273 from TheIronBorn/patch-5
fix for Swift 3
2 parents ca7a01f + bf24ad0 commit 5f64dc0

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Shuffle/README.markdown

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension Array {
1212
var temp = [Element]()
1313
while !isEmpty {
1414
let i = random(count)
15-
let obj = removeAtIndex(i)
15+
let obj = remove(at: i)
1616
temp.append(obj)
1717
}
1818
self = temp
@@ -44,7 +44,7 @@ Here is a much improved version of the shuffle algorithm:
4444
```swift
4545
extension Array {
4646
public mutating func shuffle() {
47-
for i in (count - 1).stride(through: 1, by: -1) {
47+
for i in stride(from: count - 1, through: 1, by: -1) {
4848
let j = random(i + 1)
4949
if i != j {
5050
swap(&self[i], &self[j])
@@ -96,8 +96,8 @@ There is a slight variation on this algorithm that is useful for when you want t
9696
Here is the code:
9797

9898
```swift
99-
public func shuffledArray(n: Int) -> [Int] {
100-
var a = [Int](count: n, repeatedValue: 0)
99+
public func shuffledArray(_ n: Int) -> [Int] {
100+
var a = [Int](repeating: 0, count: n)
101101
for i in 0..<n {
102102
let j = random(i + 1)
103103
if i != j {

0 commit comments

Comments
 (0)