Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9060831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:19:55+00:00 2026-06-16T15:19:55+00:00

I have the code below: def shuffle arr shuffled = [] while arr.length >

  • 0

I have the code below:

def shuffle arr
  shuffled = []
  while arr.length > 0
    randNum = rand(arr.length)
    idx = 0
    unshuf = []
    arr.each do |item|
      if idx == randNum
        shuffled.push item
      else
        unshuf.push item
      end
      idx = idx + 1
    end
    arr = unshuf
  end
  return shuffled
end

puts(shuffle([ 11, 12, 13]))

I am trying to understand the status of the array unshuf as the method shuffle is called. The array unshuf is being reset to an empty array each time before method each is called. Under such implementation, how are the numbers pushed into unshuf retained for the next round of if condition?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:19:57+00:00Added an answer on June 16, 2026 at 3:19 pm
    def shuffle arr
      shuffled = []
    
      while arr.length > 0
        randNum = rand(arr.length)
    
        idx = 0
        unshuf = []
    
        print '--- arr='; p arr # <--------
        arr.each do |item|
          if idx == randNum
            shuffled.push item
          else
            unshuf.push item
          end
          print 'shuffled='; print shuffled; print ', unshuf='; p unshuf # <--------
          idx = idx + 1
        end
    
        arr = unshuf
      end
    
      return shuffled
    end
    
    p shuffle([ 11, 12, 13, 14, 15])
    

    Execution :

    $ ruby -w t.rb 
    --- arr=[11, 12, 13, 14, 15]
    shuffled=[], unshuf=[11]
    shuffled=[], unshuf=[11, 12]
    shuffled=[13], unshuf=[11, 12]
    shuffled=[13], unshuf=[11, 12, 14]
    shuffled=[13], unshuf=[11, 12, 14, 15]
    --- arr=[11, 12, 14, 15]
    shuffled=[13, 11], unshuf=[]
    shuffled=[13, 11], unshuf=[12]
    shuffled=[13, 11], unshuf=[12, 14]
    shuffled=[13, 11], unshuf=[12, 14, 15]
    --- arr=[12, 14, 15]
    shuffled=[13, 11, 12], unshuf=[]
    shuffled=[13, 11, 12], unshuf=[14]
    shuffled=[13, 11, 12], unshuf=[14, 15]
    --- arr=[14, 15]
    shuffled=[13, 11, 12, 14], unshuf=[]
    shuffled=[13, 11, 12, 14], unshuf=[15]
    --- arr=[15]
    shuffled=[13, 11, 12, 14, 15], unshuf=[]
    [13, 11, 12, 14, 15]
    

    Explanation : the content of arr is randomly distributed between shuffled and unshuf. Then unshuf replaces arr and, if there was something in unshuf, the while loop redistributes randomly a little in shuffle, a little in unshuf. And so on until unshuf is empty. So shuffled is progressively filled with elements randomly taken from the parameter arr initially given to the method shuffle, but arr is also progressively emptied by keeping only unshuf elements in arr = unshuf. Of course after having been moved into arr, unshuf must be reset to an empty array to receive the new distribution, while shuffled must keep growing. Hope it’s clear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a registration page with the following code below: in the views: def
Good day! i have a code below: def initial(*args): for arg in args: with
for the code below: def printList(L): if L: print L[0] printList(L[1:]) I can have
Hi I have a code as given below def value(): file=open('C:/Documents and Settings/242481/My Documents/file.csv','r')
I have the code below in my Python script: def cmd_wui(argv, path_to_tx): Run a
I have the below code: def numbers(): '''returns 2 random numbers between 1 and
Right now I have only one condition in my Projects.paginate Code is below def
I have code below: <select id=testSelect> <option value=1>One</option> <option value=2>Two</option> </select> <asp:Button ID=btnTest runat=server
I have code that looks more or less like the code below but it
I have code as below: var s : String = hello world var xml

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.