Created
February 8, 2018 22:22
-
-
Save skooltch84/c1de44109df517614e930e501f4aaaec to your computer and use it in GitHub Desktop.
shuffle arrays in swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
//1. create an array | |
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | |
//2. initialize an empty array | |
var emptyArray = [Int]() | |
//3. create a random number between 0 and the number of items in the array | |
var randomNumber: Int | |
for _ in numbers { | |
randomNumber = Int(arc4random_uniform(UInt32(numbers.count - 1))) | |
//4. randomly add an item from the numbers array to the empty array | |
emptyArray.append(numbers[randomNumber]) | |
//5. remove the item that was added to emptyArray from numbers array | |
numbers.remove(at: randomNumber) | |
//6. print emptyArray | |
//print(emptyArray) | |
} | |
func shuffleArray(arrayToBeShuffled array1: [String]) -> [String]{ //edit code here | |
var oldArray = array1 | |
var newArray = [String]() //edit code here | |
var randomNumber: Int | |
for _ in array1{ | |
randomNumber = Int(arc4random_uniform(UInt32(oldArray.count - 1))) | |
newArray.append(oldArray[randomNumber]) | |
oldArray.remove(at: randomNumber) | |
} | |
return newArray | |
} | |
var even = [2, 4, 6, 8, 10, 12, 14, 16, 18] | |
//shuffleArray(arrayToBeShuffled: even) | |
var words = ["hi", "my", "name", "is", "victor"] | |
shuffleArray(arrayToBeShuffled: words) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment