fisher-yates


See On Github

Data

Contributor

Generic placeholder thumbnail

by mitogh

in ruby

Tags

shuffle

Source Code

def fisher_yates(arr = [])
  n = arr.length
  arr.each_with_index do |element, index|
    i = rand(n)
    arr[index], arr[i] = arr[i], arr[index]
  end
  arr
end
require './fisher_yates.rb'

describe "#fisher_yates" do
  context "empty array" do
    it{ expect(fisher_yates([])).to eq []}
  end

  context "shuffle array" do
      let(:arr){(1..10).to_a}
      it{ expect((1..10).to_a).not_to eq(fisher_yates arr)}
  end
end