Knuth shuffle: Difference between revisions

Line 3,561:
x[fisheryatesknuthshuffle(4)] # e.g. "bar" "baz" "quux" "foo"</lang>
===Pseudo-code-like version===
With the caveat that we lose the ability to handle errors for NULL objects or arrays of length 0, R can give us a solution that is veryso close to the pseudo-code given in the task description that we can match it line-by-line.
<lang r>knuthShuffle<-function(items)
{
Line 3,571:
items
}</lang>
This solution appears to meet the stretch goal of working for anymost 1-dimensional(all?) arraytypes of any typearray. For example, all of the following give sensible outputs:
<lang r>knuthShuffle(15)
knuthShuffle("Alice")
Line 3,577:
knuthShuffle(1:5)
knuthShuffle(array(c(1,2,3)))
knuthShuffle(matrix(1:16,nrow=4))#A 4*4 matrix.
knuthShuffle(list("Alice","Bob",42,pi))
knuthShuffle(list("Alice","Bob",42,pi,c(3.14,exp(1)),c("I'm","a ragged","array","!")))#Output is a list of length 5.</lang>
331

edits