I am trying to make my own shuffle method instead of the build-in one.
From looking at the debugging program, the recursive_shuffle does not
return when unshuffled.length == 0. What went wrong there?
See code below:
[CODE]
class Array
def shuffle
return self if self.length < 1
recursive_shuffle self, []
end
def recursive_shuffle unshuffled, shuffled
random_index = rand(unshuffled.length)
shuffled << unshuffled.slice!(random_index)
return shuffled if unshuffled.index == 0
recursive_shuffle unshuffled, shuffled
end
this line:
return shuffled if unshuffled.index == 0
I think you mean
return shuffled if unshuffled.length == 0
···
On Fri, Mar 29, 2013 at 10:40 AM, Vincent Stowbunenko <lists@ruby-forum.com>wrote:
I am trying to make my own shuffle method instead of the build-in one.
From looking at the debugging program, the recursive_shuffle does not
return when unshuffled.length == 0. What went wrong there?
See code below:
[CODE]
class Array
def shuffle
return self if self.length < 1
recursive_shuffle self,
end
def recursive_shuffle unshuffled, shuffled
random_index = rand(unshuffled.length)
shuffled << unshuffled.slice!(random_index)
return shuffled if unshuffled.index == 0
recursive_shuffle unshuffled, shuffled
end