Difference between revisions of "Code Snippet: Shuffle Strings"

From Coder Merlin
(Created page with "= Shuffle an Array of Strings = == Swift == <syntaxhighlight lang="swift"> let verbs = ["walk", "jump", "run", "swim", "dance"] let shuffledVerbs = verbs.shuffled() for verb...")
 
(Added Python version of shuffling an array!)
 
Line 11: Line 11:


== Python ==
== Python ==
<syntaxhighlight lang="python>
import random
verbs = ["walk", "jump", "run", "swim", "dance"]
random.shuffle(verbs)
print(verbs)
</syntaxhighlight>

Latest revision as of 14:26, 11 September 2019

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder

Shuffle an Array of Strings[edit]

Swift[edit]

let verbs = ["walk", "jump", "run", "swim", "dance"]
let shuffledVerbs = verbs.shuffled()
for verb in shuffledVerbs {
    print(verb)
}

Python[edit]

import random
verbs = ["walk", "jump", "run", "swim", "dance"]
random.shuffle(verbs)
print(verbs)